Refactor controller methods to return ActionResult for better error handling

This commit is contained in:
2025-11-13 18:52:45 +00:00
parent 505ace535f
commit fc6238aef1
55 changed files with 724 additions and 539 deletions

View File

@@ -102,18 +102,18 @@ public class ScreensController(MarechaiContext context) : ControllerBase
[Authorize(Roles = "Admin,UberAdmin")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task UpdateAsync(ScreenDto dto)
public async Task<ActionResult> UpdateAsync(ScreenDto dto)
{
string userId = User.FindFirstValue(ClaimTypes.Sid);
if(userId is null) return;
if(userId is null) return Unauthorized();
Screen model = await context.Screens.FindAsync(dto.Id);
if(model is null) return;
if(model is null) return NotFound();
Resolution nativeResolution = await context.Resolutions.FindAsync(dto.NativeResolutionId);
if(nativeResolution is null) return;
if(nativeResolution is null) return NotFound();
model.Diagonal = dto.Diagonal;
model.EffectiveColors = dto.EffectiveColors;
@@ -123,17 +123,19 @@ public class ScreensController(MarechaiContext context) : ControllerBase
model.Width = dto.Width;
await context.SaveChangesWithUserAsync(userId);
return Ok();
}
[HttpPost]
[Authorize(Roles = "Admin,UberAdmin")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<int> CreateAsync(ScreenDto dto)
public async Task<ActionResult<int>> CreateAsync(ScreenDto dto)
{
string userId = User.FindFirstValue(ClaimTypes.Sid);
if(userId is null) return 0;
if(userId is null) return Unauthorized();
var model = new Screen
{
@@ -155,17 +157,19 @@ public class ScreensController(MarechaiContext context) : ControllerBase
[Authorize(Roles = "Admin,UberAdmin")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task DeleteAsync(int id)
public async Task<ActionResult> DeleteAsync(int id)
{
string userId = User.FindFirstValue(ClaimTypes.Sid);
if(userId is null) return;
if(userId is null) return Unauthorized();
Screen item = await context.Screens.FindAsync(id);
if(item is null) return;
if(item is null) return NotFound();
context.Screens.Remove(item);
await context.SaveChangesWithUserAsync(userId);
return Ok();
}
}