Skip to content
Snippets Groups Projects
Commit 797e430c authored by Callum Inglis's avatar Callum Inglis
Browse files

Implement DAL, basic models created, ability to write back to database.

parent e8ee598d
No related branches found
No related tags found
No related merge requests found
using Microsoft.AspNetCore.Mvc;
using Public_API.Server.DAL;
using Public_API.Shared.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Public_API.Server.Controllers
{
[Route("[controller]")]
[ApiController]
public class APIController : ControllerBase
{
ApiContext db = new ApiContext();
// GET: api/<APIController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/sensor/logValue/...
[HttpGet("sensor/logValue/{sensorID}/{temperature}/{humidity}/{co2}/{ppm25}")]
public string Get(int sensorID, string temperature, string humidity, string co2, string ppm25)
{
// TODO Error Handling!
SensorValue sensorValue = new SensorValue(sensorID, decimal.Parse(temperature), decimal.Parse(humidity), decimal.Parse(co2), decimal.Parse(ppm25));
db.SensorValue.Add(sensorValue);
db.SaveChanges();
return "Got value from " + sensorID + ". Sorted with ID " + sensorValue.valueID;
}
// POST api/<APIController>
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/<APIController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<APIController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Public_API.Shared.Models;
namespace Public_API.Server.DAL
{
public class ApiContext : DbContext
{
public DbSet<Sensor> Sensor { get; set; }
public DbSet<SensorValue> SensorValue { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=PC-RYZEN-7\\SQLEXPRESS;Initial Catalog=EnviromentalMonitoring;Integrated Security=True");
// TODO @Callum Setup model precision
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Public_API.Shared.Models
{
public class Sensor
{
[Key]
public int sensorID { get; set; }
public decimal locationLong { get; set; }
public decimal locationLat { get; set; }
public string locationCity { get; set; }
public string friendlyName { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Public_API.Shared.Models
{
public class SensorValue
{
[Key]
public int valueID { get; set; }
public DateTime dateTime { get; set; }
public int sensorID { get; set; }
public decimal temperature { get; set; }
public decimal humidity { get; set; }
public decimal co2 { get; set; }
public decimal ppm25 { get; set; }
public SensorValue(int sensorID, decimal temperature, decimal humidity, decimal co2, decimal ppm25)
{
this.sensorID = sensorID;
this.temperature = temperature;
this.humidity = humidity;
this.co2 = co2;
this.ppm25 = ppm25;
this.dateTime = DateTime.Now;
}
}
}
USE [EnviromentalMonitoring]
GO
/****** Object: Table [dbo].[Sensor] Script Date: 19/10/2021 16:22:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Sensor](
[sensorID] [int] IDENTITY(1,1) NOT NULL,
[locationLong] decimal(11, 8) NOT NULL,
[locationLat] decimal(11, 8) NOT NULL,
[locationCity] decimal(11, 8) NOT NULL,
[friendlyName] [nvarchar](50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[SensorValue](
[valueID] [int] IDENTITY(1,1) NOT NULL,
[dateTime] [datetime] NOT NULL,
[sensorID] [int] NOT NULL,
[temperature] [decimal](14, 4) NULL,
[humidity] [decimal](14, 4) NULL,
[co2] [decimal](14, 4) NULL,
[ppm25] [decimal](14, 4) NULL
) ON [PRIMARY]
GO
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment