| | 1 | | using System.Text.Json; |
| | 2 | | using LGDXRobotCloud.API.Authorisation; |
| | 3 | | using LGDXRobotCloud.API.Services.Navigation; |
| | 4 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Commands; |
| | 5 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Responses; |
| | 6 | | using LGDXRobotCloud.Data.Models.Business.Navigation; |
| | 7 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 8 | | using Microsoft.AspNetCore.Authorization; |
| | 9 | | using Microsoft.AspNetCore.Mvc; |
| | 10 | | using LGDXRobotCloud.Utilities.Constants; |
| | 11 | |
|
| | 12 | | namespace LGDXRobotCloud.API.Areas.Navigation.Controllers; |
| | 13 | |
|
| | 14 | | [ApiController] |
| | 15 | | [Area("Navigation")] |
| | 16 | | [Route("[area]/[controller]")] |
| | 17 | | [Authorize(AuthenticationSchemes = LgdxRobotCloudAuthenticationSchemes.ApiKeyOrCertificationScheme)] |
| | 18 | | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
| | 19 | | [ValidateLgdxUserAccess] |
| 0 | 20 | | public class RealmsController( |
| 0 | 21 | | IRealmService realmService |
| 0 | 22 | | ) : ControllerBase |
| | 23 | | { |
| 0 | 24 | | private readonly IRealmService _realmService = realmService ?? throw new ArgumentNullException(nameof(realmService)); |
| | 25 | |
|
| | 26 | | [HttpGet("")] |
| | 27 | | [ProducesResponseType(typeof(IEnumerable<RealmListDto>), StatusCodes.Status200OK)] |
| | 28 | | public async Task<ActionResult<IEnumerable<RealmListDto>>> GetRealms(string? name, int pageNumber = 1, int pageSize = |
| 0 | 29 | | { |
| 0 | 30 | | pageSize = (pageSize > 100) ? 100 : pageSize; |
| 0 | 31 | | var (realms, PaginationHelper) = await _realmService.GetRealmsAsync(name, pageNumber, pageSize); |
| 0 | 32 | | Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(PaginationHelper)); |
| 0 | 33 | | return Ok(realms.ToDto()); |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | [HttpGet("Search")] |
| | 37 | | [ProducesResponseType(typeof(IEnumerable<RealmSearchDto>), StatusCodes.Status200OK)] |
| | 38 | | public async Task<ActionResult<IEnumerable<RealmSearchDto>>> SearchRealms(string? name) |
| 0 | 39 | | { |
| 0 | 40 | | var realms = await _realmService.SearchRealmsAsync(name); |
| 0 | 41 | | return Ok(realms.ToDto()); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | [HttpGet("{id}", Name = "GetRealm")] |
| | 45 | | [ProducesResponseType(typeof(RealmDto), StatusCodes.Status200OK)] |
| | 46 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 47 | | public async Task<ActionResult<RealmDto>> GetRealm(int id) |
| 0 | 48 | | { |
| 0 | 49 | | var realm = await _realmService.GetRealmAsync(id); |
| 0 | 50 | | return Ok(realm.ToDto()); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | [HttpGet("Default")] |
| | 54 | | [ProducesResponseType(typeof(RealmDto), StatusCodes.Status200OK)] |
| | 55 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 56 | | public async Task<ActionResult<RealmDto>> GetDefaultRealm() |
| 0 | 57 | | { |
| 0 | 58 | | var realm = await _realmService.GetDefaultRealmAsync(); |
| 0 | 59 | | return Ok(realm.ToDto()); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | [HttpPost("")] |
| | 63 | | [ProducesResponseType(typeof(RealmDto), StatusCodes.Status201Created)] |
| | 64 | | public async Task<ActionResult> CreateRealm(RealmCreateDto realmCreateDto) |
| 0 | 65 | | { |
| 0 | 66 | | var realm = await _realmService.CreateRealmAsync(realmCreateDto.ToBusinessModel()); |
| 0 | 67 | | return CreatedAtAction(nameof(GetRealm), new { id = realm.Id }, realm.ToDto()); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | [HttpPut("{id}")] |
| | 71 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 72 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 73 | | public async Task<ActionResult> UpdateRealm(int id, RealmUpdateDto realmUpdateDto) |
| 0 | 74 | | { |
| 0 | 75 | | if (!await _realmService.UpdateRealmAsync(id, realmUpdateDto.ToBusinessModel())) |
| 0 | 76 | | { |
| 0 | 77 | | return NotFound(); |
| | 78 | | } |
| 0 | 79 | | return NoContent(); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | [HttpPost("{id}/TestDelete")] |
| | 83 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 84 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 85 | | public async Task<ActionResult> TestDeleteRealm(int id) |
| 0 | 86 | | { |
| 0 | 87 | | await _realmService.TestDeleteRealmAsync(id); |
| 0 | 88 | | return Ok(); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | [HttpDelete("{id}")] |
| | 92 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 93 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 94 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 95 | | public async Task<ActionResult> DeleteRealm(int id) |
| 0 | 96 | | { |
| 0 | 97 | | await _realmService.TestDeleteRealmAsync(id); |
| 0 | 98 | | if (!await _realmService.DeleteRealmAsync(id)) |
| 0 | 99 | | { |
| 0 | 100 | | return NotFound(); |
| | 101 | | } |
| 0 | 102 | | return NoContent(); |
| 0 | 103 | | } |
| | 104 | | } |