All api endpoints done for now
This commit is contained in:
parent
3a5b5545b3
commit
e0a69c4828
@ -19,7 +19,7 @@ namespace Api.Controllers
|
|||||||
public async Task<IActionResult> GetDevices(int userId)
|
public async Task<IActionResult> GetDevices(int userId)
|
||||||
{
|
{
|
||||||
DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context);
|
DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context);
|
||||||
List<Device> devices = await dBAccess.GetDevices(userId);
|
List<Device> devices = await dBAccess.ReadDevices(userId);
|
||||||
if (devices.Count == 0) { return BadRequest(new { error = "There is no devices that belong to this userID" }); }
|
if (devices.Count == 0) { return BadRequest(new { error = "There is no devices that belong to this userID" }); }
|
||||||
return Ok(devices);
|
return Ok(devices);
|
||||||
}
|
}
|
||||||
@ -28,20 +28,26 @@ namespace Api.Controllers
|
|||||||
public async Task<IActionResult> AddDevice([FromBody] Device device, int userId)
|
public async Task<IActionResult> AddDevice([FromBody] Device device, int userId)
|
||||||
{
|
{
|
||||||
DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context);
|
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" }); }
|
if (!success) { return BadRequest(new { error = "This device already exist" }); }
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("logs/{userId}")]
|
[HttpGet("logs/{deviceId}")]
|
||||||
public async Task<IActionResult> GetLogs(int userId)
|
public async Task<IActionResult> GetLogs(int deviceId)
|
||||||
{
|
{
|
||||||
return Ok();
|
DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context);
|
||||||
|
List<TemperatureLogs> 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")]
|
[HttpPut("Edit/{deviceId}")]
|
||||||
public async Task<IActionResult> EditDevice([FromBody] Device device)
|
public async Task<IActionResult> 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();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ namespace Api.Controllers
|
|||||||
public async Task<IActionResult> EditUser([FromBody] User user, int userId)
|
public async Task<IActionResult> EditUser([FromBody] User user, int userId)
|
||||||
{
|
{
|
||||||
DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context);
|
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" }); }
|
if (!success) { return BadRequest(new { error = "User can't be edited" }); }
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ namespace Api.DBAccess
|
|||||||
return new User();
|
return new User();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> EditUser(User user, int userId)
|
public async Task<bool> UpdateUser(User user, int userId)
|
||||||
{
|
{
|
||||||
var profile = await _context.Users.FirstAsync(u => u.Id == userId);
|
var profile = await _context.Users.FirstAsync(u => u.Id == userId);
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ namespace Api.DBAccess
|
|||||||
return await _context.SaveChangesAsync() == 1;
|
return await _context.SaveChangesAsync() == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Device>> GetDevices(int userId)
|
public async Task<List<Device>> ReadDevices(int userId)
|
||||||
{
|
{
|
||||||
var user = await _context.Users.Include(u => u.Devices).FirstOrDefaultAsync(u => u.Id == userId);
|
var user = await _context.Users.Include(u => u.Devices).FirstOrDefaultAsync(u => u.Id == userId);
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ namespace Api.DBAccess
|
|||||||
return devices;
|
return devices;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> AddDevice(Device device, int userId)
|
public async Task<bool> CreateDevice(Device device, int userId)
|
||||||
{
|
{
|
||||||
var user = await _context.Users.Include(u => u.Devices).FirstOrDefaultAsync(u => u.Id == 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;
|
return await _context.SaveChangesAsync() == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<TemperatureLogs>> 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<TemperatureLogs>(); }
|
||||||
|
|
||||||
|
var logs = device.Logs;
|
||||||
|
|
||||||
|
return logs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
125
backend/Api/Migrations/20250319082642_init3.Designer.cs
generated
Normal file
125
backend/Api/Migrations/20250319082642_init3.Designer.cs
generated
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
// <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("20250319082642_init3")]
|
||||||
|
partial class init3
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder.HasAnnotation("ProductVersion", "9.0.3");
|
||||||
|
|
||||||
|
modelBuilder.Entity("Models.Device", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
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("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("Models.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>("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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
28
backend/Api/Migrations/20250319082642_init3.cs
Normal file
28
backend/Api/Migrations/20250319082642_init3.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Api.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class init3 : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.RenameColumn(
|
||||||
|
name: "UnikId",
|
||||||
|
table: "Devices",
|
||||||
|
newName: "ReferenceId");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.RenameColumn(
|
||||||
|
name: "ReferenceId",
|
||||||
|
table: "Devices",
|
||||||
|
newName: "UnikId");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,15 +23,15 @@ namespace Api.Migrations
|
|||||||
.ValueGeneratedOnAdd()
|
.ValueGeneratedOnAdd()
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<string>("ReferenceId")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
b.Property<double>("TempHigh")
|
b.Property<double>("TempHigh")
|
||||||
.HasColumnType("REAL");
|
.HasColumnType("REAL");
|
||||||
|
|
||||||
b.Property<double>("TempLow")
|
b.Property<double>("TempLow")
|
||||||
.HasColumnType("REAL");
|
.HasColumnType("REAL");
|
||||||
|
|
||||||
b.Property<string>("UnikId")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<int?>("UserId")
|
b.Property<int?>("UserId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ namespace Models
|
|||||||
|
|
||||||
public double TempLow { get; set; }
|
public double TempLow { get; set; }
|
||||||
|
|
||||||
public string? UnikId { get; set; }
|
public string? ReferenceId { get; set; }
|
||||||
|
|
||||||
public List<TemperatureLogs>? Logs { get; set; }
|
public List<TemperatureLogs>? Logs { get; set; }
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Binary file not shown.
@ -1,7 +1,6 @@
|
|||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// <auto-generated>
|
// <auto-generated>
|
||||||
// This code was generated by a tool.
|
// 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
|
// Changes to this file may cause incorrect behavior and will be lost if
|
||||||
// the code is regenerated.
|
// the code is regenerated.
|
||||||
@ -14,7 +13,7 @@ using System.Reflection;
|
|||||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Models")]
|
[assembly: System.Reflection.AssemblyCompanyAttribute("Models")]
|
||||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
[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.AssemblyProductAttribute("Models")]
|
||||||
[assembly: System.Reflection.AssemblyTitleAttribute("Models")]
|
[assembly: System.Reflection.AssemblyTitleAttribute("Models")]
|
||||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||||
|
@ -1 +1 @@
|
|||||||
c30361deef8ee2a3715fdf416de7a45b59b6945cd4769141783b0d576db2ee2f
|
439a6f24c00f0a2eea4802134687ecf84955f031778bf6ac98624db0a88ddbae
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
Loading…
Reference in New Issue
Block a user