diff --git a/backend/Api/BusinessLogic/DeviceLogic.cs b/backend/Api/BusinessLogic/DeviceLogic.cs index dc55b6c..da5e867 100644 --- a/backend/Api/BusinessLogic/DeviceLogic.cs +++ b/backend/Api/BusinessLogic/DeviceLogic.cs @@ -76,19 +76,9 @@ namespace Api.BusinessLogic if (device == null) { return new ConflictObjectResult(new { message = "Could not find device" }); } - var logs = await _dbAccess.ReadLogs(deviceId); + var logs = await _dbAccess.ReadLogs(deviceId, dateTimeRange); - if (logs.Count == 0) { return new ConflictObjectResult(new { message = "Could not find any logs connected to the device" }); } - - if (dateTimeRange.DateTimeStart == dateTimeRange.DateTimeEnd) { return new OkObjectResult(logs); } - - List<TemperatureLogs> rangedLogs = new List<TemperatureLogs>(); - foreach (var log in logs) - { - if (log.Date >= dateTimeRange.DateTimeStart && log.Date <= dateTimeRange.DateTimeEnd) { rangedLogs.Add(log); } - } - - return new OkObjectResult(rangedLogs); + return new OkObjectResult(logs); } /// <summary> diff --git a/backend/Api/DBAccess/DBAccess.cs b/backend/Api/DBAccess/DBAccess.cs index 6885970..3e9dd6a 100644 --- a/backend/Api/DBAccess/DBAccess.cs +++ b/backend/Api/DBAccess/DBAccess.cs @@ -311,16 +311,15 @@ namespace Api.DBAccess /// Returns the logs from the device /// </summary> /// <param name="deviceId">Has the id for the device that the los belong too</param> + /// <param name="range">Return only logs within the specified datetime range</param> /// <returns></returns> - public async Task<List<TemperatureLogs>> ReadLogs(int deviceId) + public async Task<List<TemperatureLogs>> ReadLogs(int deviceId, DateTimeRange range) { - var device = await _context.Devices.Include(d => d.Logs).FirstOrDefaultAsync(d => d.Id == deviceId); - - if (device == null || device.Logs == null) { return new List<TemperatureLogs>(); } - - var logs = device.Logs; - - return logs; + return _context.TemperatureLogs + .Where(log => log.DeviceId == deviceId) + .Where(log => log.Date > range.DateTimeStart) + .Where(log => log.Date < range.DateTimeEnd) + .ToList(); } /// <summary> diff --git a/backend/Api/DBAccess/DBContext.cs b/backend/Api/DBAccess/DBContext.cs index 7f66cdb..df6600e 100644 --- a/backend/Api/DBAccess/DBContext.cs +++ b/backend/Api/DBAccess/DBContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Api.Models; using Api.Models.Users; using Api.Models.Devices; @@ -10,6 +11,8 @@ namespace Api public DbSet<Device> Devices { get; set; } + public DbSet<TemperatureLogs> TemperatureLogs { get; set; } + public DBContext(DbContextOptions<DBContext> options) : base(options) { } } } diff --git a/backend/Api/Migrations/20250402113522_ChangeModelsNamespace.Designer.cs b/backend/Api/Migrations/20250402113522_ChangeModelsNamespace.Designer.cs new file mode 100644 index 0000000..1000ce4 --- /dev/null +++ b/backend/Api/Migrations/20250402113522_ChangeModelsNamespace.Designer.cs @@ -0,0 +1,140 @@ +// <auto-generated /> +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("20250402113522_ChangeModelsNamespace")] + partial class ChangeModelsNamespace + { + /// <inheritdoc /> + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "9.0.3"); + + modelBuilder.Entity("Api.Models.Devices.Device", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<string>("ReferenceId") + .HasColumnType("TEXT"); + + b.Property<double>("TempHigh") + .HasColumnType("REAL"); + + b.Property<double>("TempLow") + .HasColumnType("REAL"); + + b.Property<int?>("UserId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Devices"); + }); + + modelBuilder.Entity("Api.Models.TemperatureLogs", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<DateTime>("Date") + .HasColumnType("TEXT"); + + b.Property<int>("DeviceId") + .HasColumnType("INTEGER"); + + b.Property<double>("TempHigh") + .HasColumnType("REAL"); + + b.Property<double>("TempLow") + .HasColumnType("REAL"); + + b.Property<double>("Temperature") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("DeviceId"); + + b.ToTable("TemperatureLogs"); + }); + + modelBuilder.Entity("Api.Models.Users.User", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("Email") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<string>("Password") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<string>("RefreshToken") + .HasColumnType("TEXT"); + + b.Property<DateTime>("RefreshTokenExpiresAt") + .HasColumnType("TEXT"); + + b.Property<string>("Salt") + .HasColumnType("TEXT"); + + b.Property<string>("UserName") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Api.Models.Devices.Device", b => + { + b.HasOne("Api.Models.Users.User", null) + .WithMany("Devices") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("Api.Models.TemperatureLogs", b => + { + b.HasOne("Api.Models.Devices.Device", null) + .WithMany("Logs") + .HasForeignKey("DeviceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Api.Models.Devices.Device", b => + { + b.Navigation("Logs"); + }); + + modelBuilder.Entity("Api.Models.Users.User", b => + { + b.Navigation("Devices"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/Api/Migrations/20250402113522_ChangeModelsNamespace.cs b/backend/Api/Migrations/20250402113522_ChangeModelsNamespace.cs new file mode 100644 index 0000000..671a740 --- /dev/null +++ b/backend/Api/Migrations/20250402113522_ChangeModelsNamespace.cs @@ -0,0 +1,59 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Api.Migrations +{ + /// <inheritdoc /> + public partial class ChangeModelsNamespace : Migration + { + /// <inheritdoc /> + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_TemperatureLogs_Devices_DeviceId", + table: "TemperatureLogs"); + + migrationBuilder.AlterColumn<int>( + name: "DeviceId", + table: "TemperatureLogs", + type: "INTEGER", + nullable: false, + defaultValue: 0, + oldClrType: typeof(int), + oldType: "INTEGER", + oldNullable: true); + + migrationBuilder.AddForeignKey( + name: "FK_TemperatureLogs_Devices_DeviceId", + table: "TemperatureLogs", + column: "DeviceId", + principalTable: "Devices", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// <inheritdoc /> + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_TemperatureLogs_Devices_DeviceId", + table: "TemperatureLogs"); + + migrationBuilder.AlterColumn<int>( + name: "DeviceId", + table: "TemperatureLogs", + type: "INTEGER", + nullable: true, + oldClrType: typeof(int), + oldType: "INTEGER"); + + migrationBuilder.AddForeignKey( + name: "FK_TemperatureLogs_Devices_DeviceId", + table: "TemperatureLogs", + column: "DeviceId", + principalTable: "Devices", + principalColumn: "Id"); + } + } +} diff --git a/backend/Api/Migrations/DBContextModelSnapshot.cs b/backend/Api/Migrations/DBContextModelSnapshot.cs index 6d96bdd..ebab47a 100644 --- a/backend/Api/Migrations/DBContextModelSnapshot.cs +++ b/backend/Api/Migrations/DBContextModelSnapshot.cs @@ -17,7 +17,7 @@ namespace Api.Migrations #pragma warning disable 612, 618 modelBuilder.HasAnnotation("ProductVersion", "9.0.3"); - modelBuilder.Entity("Api.Models.Device", b => + modelBuilder.Entity("Api.Models.Devices.Device", b => { b.Property<int>("Id") .ValueGeneratedOnAdd() @@ -55,7 +55,7 @@ namespace Api.Migrations b.Property<DateTime>("Date") .HasColumnType("TEXT"); - b.Property<int?>("DeviceId") + b.Property<int>("DeviceId") .HasColumnType("INTEGER"); b.Property<double>("TempHigh") @@ -74,7 +74,7 @@ namespace Api.Migrations b.ToTable("TemperatureLogs"); }); - modelBuilder.Entity("Api.Models.User", b => + modelBuilder.Entity("Api.Models.Users.User", b => { b.Property<int>("Id") .ValueGeneratedOnAdd() @@ -106,26 +106,28 @@ namespace Api.Migrations b.ToTable("Users"); }); - modelBuilder.Entity("Api.Models.Device", b => + modelBuilder.Entity("Api.Models.Devices.Device", b => { - b.HasOne("Api.Models.User", null) + b.HasOne("Api.Models.Users.User", null) .WithMany("Devices") .HasForeignKey("UserId"); }); modelBuilder.Entity("Api.Models.TemperatureLogs", b => { - b.HasOne("Api.Models.Device", null) + b.HasOne("Api.Models.Devices.Device", null) .WithMany("Logs") - .HasForeignKey("DeviceId"); + .HasForeignKey("DeviceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); - modelBuilder.Entity("Api.Models.Device", b => + modelBuilder.Entity("Api.Models.Devices.Device", b => { b.Navigation("Logs"); }); - modelBuilder.Entity("Api.Models.User", b => + modelBuilder.Entity("Api.Models.Users.User", b => { b.Navigation("Devices"); }); diff --git a/backend/Api/Models/TemperatureLogs.cs b/backend/Api/Models/TemperatureLogs.cs index 16911a2..9216987 100644 --- a/backend/Api/Models/TemperatureLogs.cs +++ b/backend/Api/Models/TemperatureLogs.cs @@ -4,6 +4,8 @@ { public int Id { get; set; } + public int DeviceId { get; set; } + public double Temperature { get; set; } public DateTime Date { get; set; }