Made some of the device endpoints
This commit is contained in:
parent
13e6dae02a
commit
cf237f74a1
@ -7,9 +7,29 @@ namespace Api.Controllers
|
||||
[Route("api/[controller]")]
|
||||
public class DeviceController : Controller
|
||||
{
|
||||
private readonly DBContext _context;
|
||||
|
||||
public DeviceController(DBContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetDevices(int userId)
|
||||
{
|
||||
DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context);
|
||||
List<Device> devices = await dBAccess.GetDevices(userId);
|
||||
if (devices.Count == 0) { return BadRequest(new { error = "There is no devices that belong to this userID" }); }
|
||||
return Ok(devices);
|
||||
}
|
||||
|
||||
[HttpPost("adddevice/{userId}")]
|
||||
public async Task<IActionResult> AddDevice([FromBody] Device device, int userId)
|
||||
{
|
||||
DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context);
|
||||
bool success = await dBAccess.AddDevice(device, userId);
|
||||
if (!success) { return BadRequest(new { error = "This device already exist" }); }
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ namespace Api.Controllers
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpPost("Login")]
|
||||
public async Task<IActionResult> Login([FromBody] User user)
|
||||
{
|
||||
@ -31,11 +32,11 @@ namespace Api.Controllers
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPut("Edit")]
|
||||
public async Task<IActionResult> EditUser([FromBody] User user)
|
||||
[HttpPut("Edit/{userId}")]
|
||||
public async Task<IActionResult> EditUser([FromBody] User user, int userId)
|
||||
{
|
||||
DBAccess.DBAccess dBAccess = new DBAccess.DBAccess(_context);
|
||||
bool success = await dBAccess.EditUser(user);
|
||||
bool success = await dBAccess.EditUser(user, userId);
|
||||
if (!success) { return BadRequest(new { error = "User can't be edited" }); }
|
||||
return Ok();
|
||||
}
|
||||
|
@ -23,6 +23,10 @@ namespace Api.DBAccess
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (user.Devices == null)
|
||||
{
|
||||
user.Devices = new List<Device>();
|
||||
}
|
||||
|
||||
_context.Users.Add(user);
|
||||
return await _context.SaveChangesAsync() == 1;
|
||||
@ -37,12 +41,12 @@ namespace Api.DBAccess
|
||||
profile.Password = "";
|
||||
return profile;
|
||||
}
|
||||
return new User();
|
||||
return new User();
|
||||
}
|
||||
|
||||
public async Task<bool> EditUser(User user)
|
||||
public async Task<bool> EditUser(User user, int userId)
|
||||
{
|
||||
var profile = await _context.Users.FirstAsync(u => u.Id == user.Id);
|
||||
var profile = await _context.Users.FirstAsync(u => u.Id == userId);
|
||||
|
||||
profile.UserName = user.UserName;
|
||||
|
||||
@ -52,5 +56,29 @@ namespace Api.DBAccess
|
||||
|
||||
return await _context.SaveChangesAsync() == 1;
|
||||
}
|
||||
|
||||
public async Task<List<Device>> GetDevices(int userId)
|
||||
{
|
||||
var user = await _context.Users.Include(u => u.Devices).FirstOrDefaultAsync(u => u.Id == userId);
|
||||
|
||||
if (user == null || user.Devices == null) { return new List<Device>(); }
|
||||
|
||||
var devices = user.Devices;
|
||||
|
||||
return devices;
|
||||
}
|
||||
|
||||
public async Task<bool> AddDevice(Device device, int userId)
|
||||
{
|
||||
var user = await _context.Users.Include(u => u.Devices).FirstOrDefaultAsync(u => u.Id == userId);
|
||||
|
||||
if (user == null || user.Devices == null) { return false; }
|
||||
|
||||
if (device.Logs == null) { device.Logs = new List<TemperatureLogs>(); }
|
||||
|
||||
user.Devices.Add(device);
|
||||
|
||||
return await _context.SaveChangesAsync() == 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user