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:
@@ -28,6 +28,7 @@ using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Marechai.Data.Dtos;
|
||||
using Marechai.Database;
|
||||
using Marechai.Database.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@@ -63,29 +64,32 @@ public class StorageByMachineController(MarechaiContext context) : ControllerBas
|
||||
[Authorize(Roles = "Admin,UberAdmin")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task DeleteAsync(long id)
|
||||
public async Task<ActionResult> DeleteAsync(long id)
|
||||
{
|
||||
string userId = User.FindFirstValue(ClaimTypes.Sid);
|
||||
|
||||
if(userId is null) return;
|
||||
if(userId is null) return Unauthorized();
|
||||
StorageByMachine item = await context.StorageByMachine.FindAsync(id);
|
||||
|
||||
if(item is null) return;
|
||||
if(item is null) return NotFound();
|
||||
|
||||
context.StorageByMachine.Remove(item);
|
||||
|
||||
await context.SaveChangesWithUserAsync(userId);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin,UberAdmin")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<long> CreateAsync(int machineId, StorageType type, StorageInterface @interface, long? capacity)
|
||||
public async Task<ActionResult<long>> CreateAsync(int machineId, StorageType type, StorageInterface @interface,
|
||||
long? capacity)
|
||||
{
|
||||
string userId = User.FindFirstValue(ClaimTypes.Sid);
|
||||
|
||||
if(userId is null) return 0;
|
||||
if(userId is null) return Unauthorized();
|
||||
|
||||
var item = new StorageByMachine
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user