Major rewrite
This commit is contained in:
115
SoilMoistureAPI/Controllers/DeviceController.cs
Normal file
115
SoilMoistureAPI/Controllers/DeviceController.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SoilMoistureAPI.Data;
|
||||
using SoilMoistureAPI.Models;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SoilMoistureAPI.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class DeviceController : ControllerBase
|
||||
{
|
||||
private readonly SoilMoistureContext _context;
|
||||
|
||||
public DeviceController(SoilMoistureContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// POST: api/Device
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<Device>> CreateDevice(Device device)
|
||||
{
|
||||
if (device == null || string.IsNullOrEmpty(device.DeviceId))
|
||||
{
|
||||
return BadRequest("Invalid device data.");
|
||||
}
|
||||
|
||||
// Check if device already exists
|
||||
var existingDevice = await _context.Devices.FindAsync(device.DeviceId);
|
||||
if (existingDevice != null)
|
||||
{
|
||||
return Conflict("Device with the same DeviceId already exists.");
|
||||
}
|
||||
|
||||
_context.Devices.Add(device);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return CreatedAtAction(nameof(GetDevice), new { id = device.DeviceId }, device);
|
||||
}
|
||||
|
||||
// GET: api/Device/{id}
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<Device>> GetDevice(string id)
|
||||
{
|
||||
var device = await _context.Devices
|
||||
.Include(d => d.SoilMoistures)
|
||||
.FirstOrDefaultAsync(d => d.DeviceId == id);
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
// PUT: api/Device/{id}/nickname
|
||||
[HttpPut("{id}/nickname")]
|
||||
public async Task<IActionResult> UpdateNickname(string id, [FromBody] string newNickname)
|
||||
{
|
||||
if (string.IsNullOrEmpty(newNickname))
|
||||
{
|
||||
return BadRequest("Nickname cannot be empty.");
|
||||
}
|
||||
|
||||
var device = await _context.Devices.FindAsync(id);
|
||||
if (device == null)
|
||||
{
|
||||
return NotFound("Device not found.");
|
||||
}
|
||||
|
||||
device.Nickname = newNickname;
|
||||
_context.Entry(device).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
return NoContent(); // 204 No Content
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!DeviceExists(id))
|
||||
{
|
||||
return NotFound("Device not found.");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE: api/Device/{id}
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteDevice(string id)
|
||||
{
|
||||
var device = await _context.Devices.FindAsync(id);
|
||||
if (device == null)
|
||||
{
|
||||
return NotFound("Device not found.");
|
||||
}
|
||||
|
||||
_context.Devices.Remove(device);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return NoContent(); // 204 No Content
|
||||
}
|
||||
|
||||
private bool DeviceExists(string id)
|
||||
{
|
||||
return _context.Devices.Any(e => e.DeviceId == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
96
SoilMoistureAPI/Controllers/SoilMoistureController.cs
Normal file
96
SoilMoistureAPI/Controllers/SoilMoistureController.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SoilMoistureAPI.Data;
|
||||
using SoilMoistureAPI.Models;
|
||||
|
||||
namespace SoilMoistureAPI.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class SoilMoistureController : ControllerBase
|
||||
{
|
||||
private readonly SoilMoistureContext _context;
|
||||
|
||||
public SoilMoistureController(SoilMoistureContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// POST: api/SoilMoisture
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostSoilMoisture([FromBody] SoilMoistureDto data)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
return BadRequest("No data received.");
|
||||
}
|
||||
|
||||
// Basic validation: ensure required fields are provided.
|
||||
if (string.IsNullOrEmpty(data.DeviceId))
|
||||
{
|
||||
ModelState.AddModelError("DeviceId", "The DeviceId field is required.");
|
||||
}
|
||||
// You might also want to validate moisture level here.
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
// Check if a record for this device already exists
|
||||
var existingRecord = await _context.SoilMoistures
|
||||
.Include(sm => sm.Device)
|
||||
.FirstOrDefaultAsync(sm => sm.DeviceId == data.DeviceId);
|
||||
|
||||
if (existingRecord != null)
|
||||
{
|
||||
// Update the existing record
|
||||
existingRecord.MoistureLevel = data.MoistureLevel;
|
||||
existingRecord.Timestamp = DateTime.UtcNow;
|
||||
|
||||
_context.SoilMoistures.Update(existingRecord);
|
||||
await _context.SaveChangesAsync();
|
||||
return Ok(existingRecord);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create a new record
|
||||
var newRecord = new SoilMoisture
|
||||
{
|
||||
DeviceId = data.DeviceId,
|
||||
MoistureLevel = data.MoistureLevel,
|
||||
Timestamp = DateTime.UtcNow,
|
||||
Device = new Device
|
||||
{
|
||||
DeviceId = data.DeviceId,
|
||||
Nickname = "Dave"
|
||||
}
|
||||
};
|
||||
|
||||
_context.SoilMoistures.Add(newRecord);
|
||||
await _context.SaveChangesAsync();
|
||||
return Ok(newRecord);
|
||||
}
|
||||
}
|
||||
|
||||
// GET: api/SoilMoisture/5
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<SoilMoisture>> GetSoilMoisture(int id)
|
||||
{
|
||||
var soilMoisture = await _context.SoilMoistures.FindAsync(id);
|
||||
|
||||
if (soilMoisture == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return soilMoisture;
|
||||
}
|
||||
|
||||
// GET: api/SoilMoisture
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<SoilMoisture>>> GetSoilMoistures()
|
||||
{
|
||||
return await _context.SoilMoistures.ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user