almost done with devices

This commit is contained in:
LilleBRG 2025-04-01 14:49:51 +02:00
parent 28cffcfb42
commit f1b3c39c02
8 changed files with 94 additions and 20 deletions

View File

@ -1,6 +1,7 @@
using Api.DBAccess; using Api.DBAccess;
using Api.Models; using Api.Models;
using Api.Models.Devices; using Api.Models.Devices;
using Api.Models.Users;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Api.BusinessLogic namespace Api.BusinessLogic
@ -104,5 +105,16 @@ namespace Api.BusinessLogic
return await _dbAccess.UpdateDevice(device, deviceId); return await _dbAccess.UpdateDevice(device, deviceId);
} }
/// <summary>
/// deletes a device
/// </summary>
/// <param name="referenceId">the id used to delete</param>
/// <param name="userId">Used for deleting device from devices list in user</param>
/// <returns>returns OK</returns>
public async Task<IActionResult> DeleteDevice(string referenceId, int userId)
{
return await _dbAccess.DeleteDevice(referenceId, userId);
}
} }
} }

View File

@ -69,5 +69,16 @@ namespace Api.Controllers
{ {
return await _deviceLogic.EditDevice(device, deviceId); return await _deviceLogic.EditDevice(device, deviceId);
} }
// Sends the userId to userLogic
[Authorize]
[HttpDelete("Delete/{referenceId}")]
public async Task<IActionResult> DeleteUser(string referenceId)
{
var claims = HttpContext.User.Claims;
string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
int userId = Convert.ToInt32(userIdString);
return await _deviceLogic.DeleteDevice(referenceId, userId);
}
} }
} }

View File

@ -205,8 +205,6 @@ namespace Api.DBAccess
if (user == null || user.Devices == null) { return new ConflictObjectResult(new { message = "User did not have a device list" }); } if (user == null || user.Devices == null) { return new ConflictObjectResult(new { message = "User did not have a device list" }); }
if (device.Logs == null) { device.Logs = new List<TemperatureLogs>(); }
user.Devices.Add(device); user.Devices.Add(device);
bool saved = await _context.SaveChangesAsync() == 1; bool saved = await _context.SaveChangesAsync() == 1;
@ -223,9 +221,57 @@ namespace Api.DBAccess
} }
// Returns a device according to refenreId // Returns a device according to refenreId
public Device ReadDevice(string refenreId) public Device ReadDevice(string referenceId)
{ {
return _context.Devices.FirstOrDefault(d => d.ReferenceId == refenreId); return _context.Devices.FirstOrDefault(d => d.ReferenceId == referenceId);
}
public async Task<IActionResult> EditDevice(EditDeviceRequest request, string referenceId)
{
var device = await _context.Devices.FirstOrDefaultAsync(d => d.ReferenceId == referenceId);
if (device != null)
{
if (device.Name == "" || device.Name == null)
return new ConflictObjectResult(new { message = "Please enter a name" });
device.Name = request.Name;
device.TempLow = request.TempLow;
device.TempHigh = request.TempHigh;
bool saved = await _context.SaveChangesAsync() >= 0;
if (saved) { return new OkObjectResult(saved); }
return new ConflictObjectResult(new { message = "Could not save to database" });
}
return new ConflictObjectResult(new { message = "Invalid device. May already be deleted" });
}
public async Task<IActionResult> DeleteDevice(string referenceId, int userId)
{
var user = await _context.Users
.Include(u => u.Devices) // Ensure devices are loaded
.FirstOrDefaultAsync(u => u.Id == userId);
if (user == null)
{
return new NotFoundObjectResult(new { message = "User not found" });
}
var device = user.Devices?.FirstOrDefault(d => d.ReferenceId == referenceId);
if (device != null || user.Devices != null)
{
user.Devices.Remove(device);
_context.Devices.Remove(device);
bool saved = await _context.SaveChangesAsync() > 0;
if (saved) return new OkObjectResult(new { message = "Device deleted successfully" });
return new ConflictObjectResult(new { message = "Could not save to database" });
}
return new NotFoundObjectResult(new { message = "Device not found or already deleted" });
} }
// Returns all devices // Returns all devices

View File

@ -2,5 +2,10 @@
{ {
public class EditDeviceRequest public class EditDeviceRequest
{ {
public string Name { get; set; }
public double TempHigh { get; set; }
public double TempLow { get; set; }
} }
} }

View File

@ -65,10 +65,10 @@
<input type="text" placeholder="Enter a name for the placement" id="name" required> <input type="text" placeholder="Enter a name for the placement" id="name" required>
<label for="tempHigh"><b>High Temperature</b></label> <label for="tempHigh"><b>High Temperature</b></label>
<input type="text" placeholder="Edit the high temperature" id="tempHigh" required> <input type="number" placeholder="Edit the high temperature" id="tempHigh" required>
<label for="tempLow"><b>Low Temperature</b></label> <label for="tempLow"><b>Low Temperature</b></label>
<input type="text" placeholder="Edit the low temperature" id="tempLow" required> <input type="number" placeholder="Edit the low temperature" id="tempLow" required>
<div class="clearfix"> <div class="clearfix">
<button type="button" class="cancelbtn">Cancel</button> <button type="button" class="cancelbtn">Cancel</button>

View File

@ -1,3 +1,4 @@
import { add } from "./services/devices.service.js";
import { devices } from "../mockdata/devices.mockdata.js"; import { devices } from "../mockdata/devices.mockdata.js";
import { logout } from "../shared/utils.js"; import { logout } from "../shared/utils.js";
@ -83,8 +84,8 @@ document.getElementById("editbtn").onclick = () => {
const tempHigh = document.getElementById("tempHigh").value; const tempHigh = document.getElementById("tempHigh").value;
const tempLow = document.getElementById("tempLow").value; const tempLow = document.getElementById("tempLow").value;
update(selectedReferenceId, name, tempHigh, tempLow); // Call delete function with referenceId update(name, tempHigh, tempLow, selectedReferenceId); // Call delete function with referenceId
document.getElementById("deleteModal").style.display = "none"; document.getElementById("editModal").style.display = "none";
window.location.reload(); window.location.reload();
} }
}; };

View File

@ -4,17 +4,16 @@ export function getDevices() {
return request("GET", "/device"); return request("GET", "/device");
} }
export function update(ids) { export function add(referenceId) {
fetch(`${address}/get-on-user-id`, { return request("POST", "/device/adddevice", {referenceId: referenceId});
method: "PATCH", }
headers: {
"Content-Type": "application/json" export function deleteDevice(referenceId) {
}, return request("DELETE", "/device", {referenceId: referenceId});
body: JSON.stringify({ ids: ids }) }
})
.then(response => response.json()) export function update(name, temphigh, tempLow, referenceId) {
.then(data => console.log("Success:", data)) return request("PUT", "/device/edit", {name: name, temphigh: temphigh, tempLow: tempLow, referenceId: referenceId});
.catch(error => console.error("Error:", error));
} }
export function getLogsOnDeviceId(id) { export function getLogsOnDeviceId(id) {

View File

@ -30,7 +30,7 @@ body {
} }
/* Full-width input fields */ /* Full-width input fields */
input[type=text], input[type=password], input[type=email] { input[type=text], input[type=password], input[type=email], input[type=number] {
width: 100%; width: 100%;
padding: 12px 20px; padding: 12px 20px;
margin: 8px 0; margin: 8px 0;