Compare commits
No commits in common. "6e1aef1dc24c92cbdc3b32f2c2807b676472eb40" and "5736d00485e40ec9ebe28118dc8f1b2629183343" have entirely different histories.
6e1aef1dc2
...
5736d00485
@ -9,18 +9,19 @@ namespace Api.Controllers
|
||||
[Route("api/[controller]")]
|
||||
public class DeviceController : Controller
|
||||
{
|
||||
private readonly DbAccess _dbAccess;
|
||||
private readonly DBContext _context;
|
||||
|
||||
public DeviceController(DbAccess dbAccess)
|
||||
public DeviceController(DBContext context)
|
||||
{
|
||||
_dbAccess = dbAccess;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetDevices(int userId)
|
||||
{
|
||||
List<Device> devices = await _dbAccess.ReadDevices(userId);
|
||||
DbAccess dBAccess = new DbAccess(_context);
|
||||
List<Device> 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);
|
||||
}
|
||||
@ -29,7 +30,8 @@ namespace Api.Controllers
|
||||
[HttpPost("adddevice/{userId}")]
|
||||
public async Task<IActionResult> AddDevice([FromBody] Device device, int userId)
|
||||
{
|
||||
bool success = await _dbAccess.CreateDevice(device, userId);
|
||||
DbAccess dBAccess = new DbAccess(_context);
|
||||
bool success = await dBAccess.CreateDevice(device, userId);
|
||||
if (!success) { return BadRequest(new { error = "This device already exist" }); }
|
||||
return Ok();
|
||||
}
|
||||
@ -38,7 +40,8 @@ namespace Api.Controllers
|
||||
[HttpGet("logs/{deviceId}")]
|
||||
public async Task<IActionResult> GetLogs(int deviceId)
|
||||
{
|
||||
List<TemperatureLogs> logs = await _dbAccess.ReadLogs(deviceId);
|
||||
DbAccess dBAccess = new 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);
|
||||
}
|
||||
@ -47,7 +50,8 @@ namespace Api.Controllers
|
||||
[HttpPut("Edit/{deviceId}")]
|
||||
public async Task<IActionResult> EditDevice([FromBody] Device device, int deviceId)
|
||||
{
|
||||
bool success = await _dbAccess.UpdateDevice(device, deviceId);
|
||||
DbAccess dBAccess = new DbAccess(_context);
|
||||
bool success = await dBAccess.UpdateDevice(device, deviceId);
|
||||
if (!success) { return BadRequest(new { error = "Device can't be edited" }); }
|
||||
return Ok();
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
using Api.DBAccess;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Api.Controllers
|
||||
{
|
||||
@ -7,17 +6,7 @@ namespace Api.Controllers
|
||||
[Route("api/[controller]")]
|
||||
public class HealthController : Controller
|
||||
{
|
||||
private readonly DbAccess _dbAccess;
|
||||
|
||||
public HealthController(DbAccess dbAccess)
|
||||
{
|
||||
_dbAccess = dbAccess;
|
||||
}
|
||||
|
||||
[HttpGet("API")]
|
||||
public async Task<IActionResult> HealthAPI() { return Ok(true); }
|
||||
|
||||
[HttpGet("DB")]
|
||||
public async Task<IActionResult> HealthDB() { return Ok(_dbAccess.Test()); }
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Health() { return Ok(); }
|
||||
}
|
||||
}
|
||||
|
@ -13,20 +13,21 @@ namespace Api.Controllers
|
||||
[Route("api/[controller]")]
|
||||
public class UserController : Controller
|
||||
{
|
||||
private readonly DbAccess _dbAccess;
|
||||
private readonly DBContext _context;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public UserController(IConfiguration configuration, DbAccess dbAccess)
|
||||
public UserController(DBContext context, IConfiguration configuration)
|
||||
{
|
||||
_dbAccess = dbAccess;
|
||||
_context = context;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
[HttpPost("Login")]
|
||||
public async Task<IActionResult> Login([FromBody] Login login)
|
||||
{
|
||||
User user = await _dbAccess.Login(login);
|
||||
if (user == null || user.Id == 0) { return Unauthorized(new { error = "Invalid username or password" }); }
|
||||
DbAccess dBAccess = new DbAccess(_context);
|
||||
var user = await dBAccess.Login(login);
|
||||
if (user.Id == 0) { return Unauthorized(new { error = "Invalid username or password" }); }
|
||||
var token = GenerateJwtToken(user);
|
||||
return Ok(new { token, user.UserName, user.Id });
|
||||
}
|
||||
@ -34,7 +35,8 @@ namespace Api.Controllers
|
||||
[HttpPost("Create")]
|
||||
public async Task<IActionResult> CreateUser([FromBody] User user)
|
||||
{
|
||||
bool success = await _dbAccess.CreateUser(user);
|
||||
DbAccess dBAccess = new DbAccess(_context);
|
||||
bool success = await dBAccess.CreateUser(user);
|
||||
if (!success) { return BadRequest(new { error = "User can't be created" }); }
|
||||
return Ok();
|
||||
}
|
||||
@ -43,7 +45,8 @@ namespace Api.Controllers
|
||||
[HttpPut("Edit/{userId}")]
|
||||
public async Task<IActionResult> EditUser([FromBody] User user, int userId)
|
||||
{
|
||||
bool success = await _dbAccess.UpdateUser(user, userId);
|
||||
DbAccess dBAccess = new DbAccess(_context);
|
||||
bool success = await dBAccess.UpdateUser(user, userId);
|
||||
if (!success) { return BadRequest(new { error = "User can't be edited" }); }
|
||||
return Ok();
|
||||
}
|
||||
@ -52,7 +55,8 @@ namespace Api.Controllers
|
||||
[HttpDelete("Delete/{userId}")]
|
||||
public async Task<IActionResult> DeleteUser(int userId)
|
||||
{
|
||||
bool success = await _dbAccess.DeleteUser(userId);
|
||||
DbAccess dbAccess = new DbAccess(_context);
|
||||
bool success = await dbAccess.DeleteUser(userId);
|
||||
if (!success) { return BadRequest(new { error = "User can't be deleted" }); }
|
||||
return Ok();
|
||||
}
|
||||
|
@ -46,11 +46,11 @@ namespace Api.DBAccess
|
||||
User user = new User();
|
||||
if (!login.EmailOrUsrn.Contains("@"))
|
||||
{
|
||||
user = await _context.Users.FirstOrDefaultAsync(u => u.UserName == login.EmailOrUsrn);
|
||||
user = await _context.Users.FirstAsync(u => u.UserName == login.EmailOrUsrn);
|
||||
}
|
||||
else
|
||||
{
|
||||
user = await _context.Users.FirstOrDefaultAsync(u => u.Email == login.EmailOrUsrn);
|
||||
user = await _context.Users.FirstAsync(u => u.Email == login.EmailOrUsrn);
|
||||
}
|
||||
|
||||
if (user == null) { return new User(); }
|
||||
@ -66,9 +66,7 @@ namespace Api.DBAccess
|
||||
|
||||
public async Task<bool> UpdateUser(User user, int userId)
|
||||
{
|
||||
var profile = await _context.Users.FirstOrDefaultAsync(u => u.Id == userId);
|
||||
|
||||
if (profile == null) { return false; }
|
||||
var profile = await _context.Users.FirstAsync(u => u.Id == userId);
|
||||
|
||||
profile.UserName = user.UserName;
|
||||
|
||||
@ -84,7 +82,7 @@ namespace Api.DBAccess
|
||||
var user = await _context.Users.Include(u => u.Devices).FirstOrDefaultAsync(u => u.Id == userId);
|
||||
if (user != null)
|
||||
{
|
||||
if (user.Devices != null && user.Devices.Count > 0)
|
||||
if (user.Devices != null && user.Devices.Count > 0)
|
||||
{
|
||||
foreach (var item in user.Devices)
|
||||
{
|
||||
@ -124,7 +122,7 @@ namespace Api.DBAccess
|
||||
|
||||
public async Task<bool> UpdateDevice(Device device, int deviceId)
|
||||
{
|
||||
var device1 = await _context.Devices.FirstOrDefaultAsync(u => u.Id == deviceId);
|
||||
var device1 = await _context.Devices.FirstAsync(u => u.Id == deviceId);
|
||||
|
||||
device1.TempLow = device.TempLow;
|
||||
|
||||
@ -148,11 +146,6 @@ namespace Api.DBAccess
|
||||
return logs;
|
||||
}
|
||||
|
||||
public async Task<bool> Test()
|
||||
{
|
||||
return _context.Database.CanConnect();
|
||||
}
|
||||
|
||||
private static string ComputeHash(string input, HashAlgorithm algorithm, string salt)
|
||||
{
|
||||
Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
|
||||
|
@ -1,5 +1,4 @@
|
||||
using Api.DBAccess;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
@ -23,9 +22,7 @@ namespace Api
|
||||
// Sets the connectionstring to the database so dbcontext can find it
|
||||
services.AddDbContext<DBContext>(options =>
|
||||
options.UseSqlite(_configuration.GetConnectionString("Database")));
|
||||
|
||||
services.AddScoped<DbAccess>();
|
||||
|
||||
|
||||
services.AddControllers();
|
||||
|
||||
services.AddAuthentication(x =>
|
||||
|
Loading…
Reference in New Issue
Block a user