diff --git a/backend/Api/Controllers/DeviceController.cs b/backend/Api/Controllers/DeviceController.cs index 4bba046..bd4e9ee 100644 --- a/backend/Api/Controllers/DeviceController.cs +++ b/backend/Api/Controllers/DeviceController.cs @@ -19,7 +19,7 @@ namespace Api.Controllers public async Task GetDevices(int userId) { DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context); - List devices = await dBAccess.GetDevices(userId); + List devices = await dBAccess.ReadDevices(userId); if (devices.Count == 0) { return BadRequest(new { error = "There is no devices that belong to this userID" }); } return Ok(devices); } @@ -28,20 +28,26 @@ namespace Api.Controllers public async Task AddDevice([FromBody] Device device, int userId) { DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context); - bool success = await dBAccess.AddDevice(device, userId); + bool success = await dBAccess.CreateDevice(device, userId); if (!success) { return BadRequest(new { error = "This device already exist" }); } return Ok(); } - [HttpGet("logs/{userId}")] - public async Task GetLogs(int userId) + [HttpGet("logs/{deviceId}")] + public async Task GetLogs(int deviceId) { - return Ok(); + DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context); + List logs = await dBAccess.ReadLogs(deviceId); + if (logs.Count == 0) { return BadRequest(new { error = "There is no logs that belong to this deviceId" }); } + return Ok(logs); } - [HttpPut("Edit")] - public async Task EditDevice([FromBody] Device device) + [HttpPut("Edit/{deviceId}")] + public async Task EditDevice([FromBody] Device device, int deviceId) { + DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context); + bool success = await dBAccess.UpdateDevice(device, deviceId); + if (!success) { return BadRequest(new { error = "Device can't be edited" }); } return Ok(); } } diff --git a/backend/Api/Controllers/UserController.cs b/backend/Api/Controllers/UserController.cs index 1422628..10235be 100644 --- a/backend/Api/Controllers/UserController.cs +++ b/backend/Api/Controllers/UserController.cs @@ -36,7 +36,7 @@ namespace Api.Controllers public async Task EditUser([FromBody] User user, int userId) { DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context); - bool success = await dBAccess.EditUser(user, userId); + bool success = await dBAccess.UpdateUser(user, userId); if (!success) { return BadRequest(new { error = "User can't be edited" }); } return Ok(); } diff --git a/backend/Api/DBAccess/DBAccess.cs b/backend/Api/DBAccess/DBAccess.cs index 70331d9..504b7b8 100644 --- a/backend/Api/DBAccess/DBAccess.cs +++ b/backend/Api/DBAccess/DBAccess.cs @@ -44,7 +44,7 @@ namespace Api.DBAccess return new User(); } - public async Task EditUser(User user, int userId) + public async Task UpdateUser(User user, int userId) { var profile = await _context.Users.FirstAsync(u => u.Id == userId); @@ -57,7 +57,7 @@ namespace Api.DBAccess return await _context.SaveChangesAsync() == 1; } - public async Task> GetDevices(int userId) + public async Task> ReadDevices(int userId) { var user = await _context.Users.Include(u => u.Devices).FirstOrDefaultAsync(u => u.Id == userId); @@ -68,7 +68,7 @@ namespace Api.DBAccess return devices; } - public async Task AddDevice(Device device, int userId) + public async Task CreateDevice(Device device, int userId) { var user = await _context.Users.Include(u => u.Devices).FirstOrDefaultAsync(u => u.Id == userId); @@ -80,5 +80,29 @@ namespace Api.DBAccess return await _context.SaveChangesAsync() == 1; } + + public async Task> ReadLogs(int deviceId) + { + var device = await _context.Devices.Include(d => d.Logs).FirstOrDefaultAsync(d => d.Id == deviceId); + + if (device == null || device.Logs == null) { return new List(); } + + var logs = device.Logs; + + return logs; + } + + public async Task UpdateDevice(Device device, int deviceId) + { + var device1 = await _context.Devices.FirstAsync(u => u.Id == deviceId); + + device1.TempLow = device.TempLow; + + device1.TempHigh = device.TempHigh; + + device1.ReferenceId = device.ReferenceId; + + return await _context.SaveChangesAsync() == 1; + } } } diff --git a/backend/Api/Migrations/20250319082642_init3.Designer.cs b/backend/Api/Migrations/20250319082642_init3.Designer.cs new file mode 100644 index 0000000..e4f30c1 --- /dev/null +++ b/backend/Api/Migrations/20250319082642_init3.Designer.cs @@ -0,0 +1,125 @@ +// +using System; +using Api; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Api.Migrations +{ + [DbContext(typeof(DBContext))] + [Migration("20250319082642_init3")] + partial class init3 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "9.0.3"); + + modelBuilder.Entity("Models.Device", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ReferenceId") + .HasColumnType("TEXT"); + + b.Property("TempHigh") + .HasColumnType("REAL"); + + b.Property("TempLow") + .HasColumnType("REAL"); + + b.Property("UserId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Devices"); + }); + + modelBuilder.Entity("Models.TemperatureLogs", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Date") + .HasColumnType("TEXT"); + + b.Property("DeviceId") + .HasColumnType("INTEGER"); + + b.Property("TempHigh") + .HasColumnType("REAL"); + + b.Property("TempLow") + .HasColumnType("REAL"); + + b.Property("Temperature") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("DeviceId"); + + b.ToTable("TemperatureLogs"); + }); + + modelBuilder.Entity("Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Email") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Password") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UserName") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Models.Device", b => + { + b.HasOne("Models.User", null) + .WithMany("Devices") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("Models.TemperatureLogs", b => + { + b.HasOne("Models.Device", null) + .WithMany("Logs") + .HasForeignKey("DeviceId"); + }); + + modelBuilder.Entity("Models.Device", b => + { + b.Navigation("Logs"); + }); + + modelBuilder.Entity("Models.User", b => + { + b.Navigation("Devices"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/Api/Migrations/20250319082642_init3.cs b/backend/Api/Migrations/20250319082642_init3.cs new file mode 100644 index 0000000..7414621 --- /dev/null +++ b/backend/Api/Migrations/20250319082642_init3.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Api.Migrations +{ + /// + public partial class init3 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "UnikId", + table: "Devices", + newName: "ReferenceId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "ReferenceId", + table: "Devices", + newName: "UnikId"); + } + } +} diff --git a/backend/Api/Migrations/DBContextModelSnapshot.cs b/backend/Api/Migrations/DBContextModelSnapshot.cs index 2882c54..babadca 100644 --- a/backend/Api/Migrations/DBContextModelSnapshot.cs +++ b/backend/Api/Migrations/DBContextModelSnapshot.cs @@ -23,15 +23,15 @@ namespace Api.Migrations .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); + b.Property("ReferenceId") + .HasColumnType("TEXT"); + b.Property("TempHigh") .HasColumnType("REAL"); b.Property("TempLow") .HasColumnType("REAL"); - b.Property("UnikId") - .HasColumnType("TEXT"); - b.Property("UserId") .HasColumnType("INTEGER"); diff --git a/backend/Models/Device.cs b/backend/Models/Device.cs index 94dcaa3..a62de68 100644 --- a/backend/Models/Device.cs +++ b/backend/Models/Device.cs @@ -14,7 +14,7 @@ namespace Models public double TempLow { get; set; } - public string? UnikId { get; set; } + public string? ReferenceId { get; set; } public List? Logs { get; set; } } diff --git a/backend/Models/bin/Debug/net8.0/Models.dll b/backend/Models/bin/Debug/net8.0/Models.dll index df317c1..495e381 100644 Binary files a/backend/Models/bin/Debug/net8.0/Models.dll and b/backend/Models/bin/Debug/net8.0/Models.dll differ diff --git a/backend/Models/bin/Debug/net8.0/Models.pdb b/backend/Models/bin/Debug/net8.0/Models.pdb index 3b833ea..029f8e2 100644 Binary files a/backend/Models/bin/Debug/net8.0/Models.pdb and b/backend/Models/bin/Debug/net8.0/Models.pdb differ diff --git a/backend/Models/obj/Debug/net8.0/Models.AssemblyInfo.cs b/backend/Models/obj/Debug/net8.0/Models.AssemblyInfo.cs index 3e559f1..6d1e2aa 100644 --- a/backend/Models/obj/Debug/net8.0/Models.AssemblyInfo.cs +++ b/backend/Models/obj/Debug/net8.0/Models.AssemblyInfo.cs @@ -1,7 +1,6 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -14,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("Models")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b194766464c5b95244bb2810b3fd923d8c01ca83")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+3a5b5545b33eb0ee4938a4ca36601016bc01ed22")] [assembly: System.Reflection.AssemblyProductAttribute("Models")] [assembly: System.Reflection.AssemblyTitleAttribute("Models")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/backend/Models/obj/Debug/net8.0/Models.AssemblyInfoInputs.cache b/backend/Models/obj/Debug/net8.0/Models.AssemblyInfoInputs.cache index 638365d..1c57678 100644 --- a/backend/Models/obj/Debug/net8.0/Models.AssemblyInfoInputs.cache +++ b/backend/Models/obj/Debug/net8.0/Models.AssemblyInfoInputs.cache @@ -1 +1 @@ -c30361deef8ee2a3715fdf416de7a45b59b6945cd4769141783b0d576db2ee2f +439a6f24c00f0a2eea4802134687ecf84955f031778bf6ac98624db0a88ddbae diff --git a/backend/Models/obj/Debug/net8.0/Models.dll b/backend/Models/obj/Debug/net8.0/Models.dll index df317c1..495e381 100644 Binary files a/backend/Models/obj/Debug/net8.0/Models.dll and b/backend/Models/obj/Debug/net8.0/Models.dll differ diff --git a/backend/Models/obj/Debug/net8.0/Models.pdb b/backend/Models/obj/Debug/net8.0/Models.pdb index 3b833ea..029f8e2 100644 Binary files a/backend/Models/obj/Debug/net8.0/Models.pdb and b/backend/Models/obj/Debug/net8.0/Models.pdb differ diff --git a/backend/Models/obj/Debug/net8.0/ref/Models.dll b/backend/Models/obj/Debug/net8.0/ref/Models.dll index 453bf92..71faeef 100644 Binary files a/backend/Models/obj/Debug/net8.0/ref/Models.dll and b/backend/Models/obj/Debug/net8.0/ref/Models.dll differ diff --git a/backend/Models/obj/Debug/net8.0/refint/Models.dll b/backend/Models/obj/Debug/net8.0/refint/Models.dll index 453bf92..71faeef 100644 Binary files a/backend/Models/obj/Debug/net8.0/refint/Models.dll and b/backend/Models/obj/Debug/net8.0/refint/Models.dll differ diff --git a/docs/TempAlarmModelDiagram.png b/docs/TempAlarmModelDiagram.png index 7b647f4..f607443 100644 Binary files a/docs/TempAlarmModelDiagram.png and b/docs/TempAlarmModelDiagram.png differ