creae update devices
This commit is contained in:
@ -16,9 +16,6 @@ namespace Yuna.Website.Server.API
|
||||
app.MapPost("/api/device", CreateDevice)
|
||||
.WithTags("device");
|
||||
|
||||
app.MapDelete("/api/device/{id:long}", () => { })
|
||||
.WithTags("device");
|
||||
|
||||
app.MapGet("/api/device/{deviceId:long}", GetById)
|
||||
.WithTags("device");
|
||||
|
||||
@ -32,6 +29,12 @@ namespace Yuna.Website.Server.API
|
||||
|
||||
app.MapPut("/api/device/{deviceId:long}", AddSkillsToDevice)
|
||||
.WithTags("device");
|
||||
|
||||
app.MapPut("/api/device", Update)
|
||||
.WithTags("device");
|
||||
|
||||
app.MapDelete("/api/device/{deviceId:long}", Delete)
|
||||
.WithTags("device");
|
||||
}
|
||||
|
||||
|
||||
@ -99,10 +102,18 @@ namespace Yuna.Website.Server.API
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IResult> Delete(IDeviceService deviceService, long id)
|
||||
public async Task<IResult> Delete(IDeviceService deviceService, HttpContext context, long deviceId)
|
||||
{
|
||||
var result = await deviceService.Delete(id);
|
||||
if (result is null) return Results.NotFound();
|
||||
var isAdmin = context.GetRoleFromCookie();
|
||||
var userId = context.GetUserIdFromCookie();
|
||||
|
||||
var deviceToDelete = await deviceService.GetById(deviceId);
|
||||
if(deviceToDelete is null) return Results.NotFound();
|
||||
|
||||
if (userId != deviceToDelete.UserId && !isAdmin) return Results.Forbid();
|
||||
|
||||
var result = await deviceService.Delete(deviceId);
|
||||
if (result is null) return Results.Problem(statusCode: 500);
|
||||
return Results.Ok(result);
|
||||
}
|
||||
|
||||
@ -129,5 +140,40 @@ namespace Yuna.Website.Server.API
|
||||
|
||||
return Results.Ok(result);
|
||||
}
|
||||
|
||||
|
||||
public class UpdateDeviceRequest
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public long Id { get; set; }
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public string Description { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("deviceUrl")]
|
||||
public string DeviceUrl { get; set; } = null!;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IResult> Update([FromBody] UpdateDeviceRequest request, HttpContext context, IDeviceService deviceService)
|
||||
{
|
||||
var userId = context.GetUserIdFromCookie();
|
||||
var isAdmin = context.GetRoleFromCookie();
|
||||
|
||||
var device = await deviceService.GetById(request.Id);
|
||||
|
||||
if (device is null) return Results.NotFound();
|
||||
if (device.UserId != userId && !isAdmin) return Results.Forbid();
|
||||
|
||||
device.DeviceUrl = request.DeviceUrl;
|
||||
device.Name = request.Name;
|
||||
device.Description = request.Description;
|
||||
|
||||
var result = await deviceService.Update(device);
|
||||
return Results.Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user