mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Refactor controller methods to return ActionResult for better error handling
This commit is contained in:
@@ -106,14 +106,14 @@ public class GpusController(MarechaiContext context) : ControllerBase
|
||||
[Authorize(Roles = "Admin,UberAdmin")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task UpdateAsync(GpuDto dto)
|
||||
public async Task<ActionResult> UpdateAsync(GpuDto dto)
|
||||
{
|
||||
string userId = User.FindFirstValue(ClaimTypes.Sid);
|
||||
|
||||
if(userId is null) return;
|
||||
if(userId is null) return Unauthorized();
|
||||
Gpu model = await context.Gpus.FindAsync(dto.Id);
|
||||
|
||||
if(model is null) return;
|
||||
if(model is null) return NotFound();
|
||||
|
||||
model.Name = dto.Name;
|
||||
model.CompanyId = dto.CompanyId;
|
||||
@@ -126,17 +126,19 @@ public class GpusController(MarechaiContext context) : ControllerBase
|
||||
model.Transistors = dto.Transistors;
|
||||
|
||||
await context.SaveChangesWithUserAsync(userId);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin,UberAdmin")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<int> CreateAsync(GpuDto dto)
|
||||
public async Task<ActionResult<int>> CreateAsync(GpuDto dto)
|
||||
{
|
||||
string userId = User.FindFirstValue(ClaimTypes.Sid);
|
||||
|
||||
if(userId is null) return 0;
|
||||
if(userId is null) return Unauthorized();
|
||||
|
||||
var model = new Gpu
|
||||
{
|
||||
@@ -161,17 +163,19 @@ public class GpusController(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();
|
||||
Gpu item = await context.Gpus.FindAsync(id);
|
||||
|
||||
if(item is null) return;
|
||||
if(item is null) return NotFound();
|
||||
|
||||
context.Gpus.Remove(item);
|
||||
|
||||
await context.SaveChangesWithUserAsync(userId);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user