|
| 1 | +using MediatR; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Sudoku.Api.Models; |
| 4 | +using Sudoku.Application.Commands; |
| 5 | +using Sudoku.Application.DTOs; |
| 6 | +using Sudoku.Application.Queries; |
| 7 | + |
| 8 | +namespace Sudoku.Api.Controllers; |
| 9 | + |
| 10 | +[Route("api/profiles")] |
| 11 | +[ApiController] |
| 12 | +public class ProfilesController(IMediator mediator) : ControllerBase |
| 13 | +{ |
| 14 | + [HttpPost] |
| 15 | + [ProducesResponseType(typeof(ProfileDto), StatusCodes.Status201Created)] |
| 16 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 17 | + [ProducesResponseType(StatusCodes.Status409Conflict)] |
| 18 | + public async Task<ActionResult<ProfileDto>> CreateProfileAsync([FromBody] CreateProfileRequest request) |
| 19 | + { |
| 20 | + var result = await mediator.Send(new CreateProfileCommand(request.Alias)); |
| 21 | + |
| 22 | + if (!result.IsSuccess) |
| 23 | + { |
| 24 | + if (result.Error!.Contains("already taken", StringComparison.OrdinalIgnoreCase)) |
| 25 | + return Conflict(result.Error); |
| 26 | + return BadRequest(result.Error); |
| 27 | + } |
| 28 | + |
| 29 | + return StatusCode(StatusCodes.Status201Created, result.Value); |
| 30 | + } |
| 31 | + |
| 32 | + [HttpGet("{alias}")] |
| 33 | + [ProducesResponseType(typeof(ProfileDto), StatusCodes.Status200OK)] |
| 34 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 35 | + public async Task<ActionResult<ProfileDto>> GetProfileAsync(string alias) |
| 36 | + { |
| 37 | + var result = await mediator.Send(new GetProfileByAliasQuery(alias)); |
| 38 | + |
| 39 | + if (!result.IsSuccess) |
| 40 | + return BadRequest(result.Error); |
| 41 | + |
| 42 | + if (result.Value == null) |
| 43 | + return NotFound(); |
| 44 | + |
| 45 | + return Ok(result.Value); |
| 46 | + } |
| 47 | + |
| 48 | + [HttpPatch("{alias}")] |
| 49 | + [ProducesResponseType(typeof(ProfileDto), StatusCodes.Status200OK)] |
| 50 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 51 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 52 | + [ProducesResponseType(StatusCodes.Status409Conflict)] |
| 53 | + public async Task<ActionResult<ProfileDto>> UpdateProfileAliasAsync(string alias, [FromBody] UpdateProfileAliasRequest request) |
| 54 | + { |
| 55 | + // First resolve the profile by alias to get profileId |
| 56 | + var getResult = await mediator.Send(new GetProfileByAliasQuery(alias)); |
| 57 | + if (!getResult.IsSuccess) |
| 58 | + return BadRequest(getResult.Error); |
| 59 | + if (getResult.Value == null) |
| 60 | + return NotFound(); |
| 61 | + |
| 62 | + var updateResult = await mediator.Send(new UpdateProfileAliasCommand(getResult.Value.ProfileId, request.NewAlias)); |
| 63 | + |
| 64 | + if (!updateResult.IsSuccess) |
| 65 | + { |
| 66 | + if (updateResult.Error!.Contains("already taken", StringComparison.OrdinalIgnoreCase)) |
| 67 | + return Conflict(updateResult.Error); |
| 68 | + if (updateResult.Error.Contains("not found", StringComparison.OrdinalIgnoreCase)) |
| 69 | + return NotFound(); |
| 70 | + return BadRequest(updateResult.Error); |
| 71 | + } |
| 72 | + |
| 73 | + return Ok(updateResult.Value); |
| 74 | + } |
| 75 | +} |
0 commit comments