|
| 1 | +// Licensed to ICTAce under the MIT license. |
| 2 | + |
| 3 | +namespace ICTAce.FileHub.Features.Files; |
| 4 | + |
| 5 | +[Route("api/ictace/fileHub/files")] |
| 6 | +[ApiController] |
| 7 | +public class ICTAceFileHubFilesController( |
| 8 | + IMediator mediator, |
| 9 | + ILogManager logger, |
| 10 | + IHttpContextAccessor accessor) |
| 11 | + : ModuleControllerBase(logger, accessor) |
| 12 | +{ |
| 13 | + private readonly IMediator _mediator = mediator; |
| 14 | + |
| 15 | + [HttpGet("{id}")] |
| 16 | + [Authorize(Policy = PolicyNames.ViewModule)] |
| 17 | + [ProducesResponseType(typeof(GetFileDto), StatusCodes.Status200OK)] |
| 18 | + [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| 19 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 20 | + public async Task<ActionResult<GetFileDto>> GetAsync( |
| 21 | + int id, |
| 22 | + [FromQuery] int moduleId, |
| 23 | + CancellationToken cancellationToken = default) |
| 24 | + { |
| 25 | + if (!IsAuthorizedEntityId(EntityNames.Module, moduleId)) |
| 26 | + { |
| 27 | + _logger.Log(LogLevel.Error, this, LogFunction.Security, |
| 28 | + "Unauthorized FileHub File Get Attempt Id={Id} in ModuleId={ModuleId}", id, moduleId); |
| 29 | + return Forbid(); |
| 30 | + } |
| 31 | + |
| 32 | + if (id <= 0) |
| 33 | + { |
| 34 | + return BadRequest("Invalid File ID"); |
| 35 | + } |
| 36 | + |
| 37 | + var query = new GetFileRequest |
| 38 | + { |
| 39 | + ModuleId = moduleId, |
| 40 | + Id = id, |
| 41 | + }; |
| 42 | + |
| 43 | + var file = await _mediator.Send(query, cancellationToken).ConfigureAwait(false); |
| 44 | + |
| 45 | + if (file is null) |
| 46 | + { |
| 47 | + _logger.Log(LogLevel.Warning, this, LogFunction.Read, |
| 48 | + "File Not Found Id={Id} in ModuleId={ModuleId}", id, moduleId); |
| 49 | + return NotFound(); |
| 50 | + } |
| 51 | + |
| 52 | + return Ok(file); |
| 53 | + } |
| 54 | + |
| 55 | + [HttpGet("")] |
| 56 | + [Authorize(Policy = PolicyNames.ViewModule)] |
| 57 | + [ProducesResponseType(typeof(PagedResult<ListFileDto>), StatusCodes.Status200OK)] |
| 58 | + [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| 59 | + public async Task<ActionResult<PagedResult<ListFileDto>>> ListAsync( |
| 60 | + [FromQuery] int moduleId, |
| 61 | + [FromQuery] int pageNumber = 1, |
| 62 | + [FromQuery] int pageSize = 10, |
| 63 | + CancellationToken cancellationToken = default) |
| 64 | + { |
| 65 | + if (!IsAuthorizedEntityId(EntityNames.Module, moduleId)) |
| 66 | + { |
| 67 | + _logger.Log(LogLevel.Error, this, LogFunction.Security, |
| 68 | + "Unauthorized File List Attempt ModuleId={ModuleId}", moduleId); |
| 69 | + return Forbid(); |
| 70 | + } |
| 71 | + |
| 72 | + if (pageSize > 100) |
| 73 | + { |
| 74 | + pageSize = 100; |
| 75 | + } |
| 76 | + |
| 77 | + if (pageNumber < 1) |
| 78 | + { |
| 79 | + pageNumber = 1; |
| 80 | + } |
| 81 | + |
| 82 | + var query = new ListFileRequest |
| 83 | + { |
| 84 | + ModuleId = moduleId, |
| 85 | + PageNumber = pageNumber, |
| 86 | + PageSize = pageSize, |
| 87 | + }; |
| 88 | + |
| 89 | + var result = await _mediator.Send(query, cancellationToken).ConfigureAwait(false); |
| 90 | + |
| 91 | + if (result is null) |
| 92 | + { |
| 93 | + return NotFound(); |
| 94 | + } |
| 95 | + |
| 96 | + return Ok(result); |
| 97 | + } |
| 98 | + |
| 99 | + [HttpPost("")] |
| 100 | + [Authorize(Policy = PolicyNames.EditModule)] |
| 101 | + [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] |
| 102 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 103 | + [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| 104 | + public async Task<ActionResult<int>> CreateAsync( |
| 105 | + [FromQuery] int moduleId, |
| 106 | + [FromBody] CreateAndUpdateFileDto dto, |
| 107 | + CancellationToken cancellationToken = default) |
| 108 | + { |
| 109 | + if (!ModelState.IsValid) |
| 110 | + { |
| 111 | + return BadRequest(ModelState); |
| 112 | + } |
| 113 | + |
| 114 | + if (!IsAuthorizedEntityId(EntityNames.Module, moduleId)) |
| 115 | + { |
| 116 | + _logger.Log(LogLevel.Error, this, LogFunction.Security, |
| 117 | + "Unauthorized File Create Attempt ModuleId={ModuleId}", moduleId); |
| 118 | + return Forbid(); |
| 119 | + } |
| 120 | + |
| 121 | + var command = new CreateFileRequest |
| 122 | + { |
| 123 | + ModuleId = moduleId, |
| 124 | + Name = dto.Name, |
| 125 | + FileName = dto.FileName, |
| 126 | + ImageName = dto.ImageName, |
| 127 | + Description = dto.Description, |
| 128 | + FileSize = dto.FileSize, |
| 129 | + Downloads = dto.Downloads, |
| 130 | + }; |
| 131 | + |
| 132 | + var id = await _mediator.Send(command, cancellationToken).ConfigureAwait(false); |
| 133 | + |
| 134 | + return Created( |
| 135 | + Url.Action(nameof(GetAsync), new { id, moduleId = command.ModuleId }) ?? string.Empty, |
| 136 | + id); |
| 137 | + } |
| 138 | + |
| 139 | + [HttpPut("{id}")] |
| 140 | + [Authorize(Policy = PolicyNames.EditModule)] |
| 141 | + [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] |
| 142 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 143 | + [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| 144 | + public async Task<ActionResult<int>> UpdateAsync( |
| 145 | + int id, |
| 146 | + [FromQuery] int moduleId, |
| 147 | + [FromBody] CreateAndUpdateFileDto dto, |
| 148 | + CancellationToken cancellationToken = default) |
| 149 | + { |
| 150 | + if (!ModelState.IsValid) |
| 151 | + { |
| 152 | + return BadRequest(ModelState); |
| 153 | + } |
| 154 | + |
| 155 | + if (!IsAuthorizedEntityId(EntityNames.Module, moduleId)) |
| 156 | + { |
| 157 | + _logger.Log(LogLevel.Error, this, LogFunction.Security, |
| 158 | + "Unauthorized File Update Attempt Id={Id} in ModuleId={ModuleId}", id, moduleId); |
| 159 | + return Forbid(); |
| 160 | + } |
| 161 | + |
| 162 | + var command = new UpdateFileRequest |
| 163 | + { |
| 164 | + Id = id, |
| 165 | + ModuleId = moduleId, |
| 166 | + Name = dto.Name, |
| 167 | + FileName = dto.FileName, |
| 168 | + ImageName = dto.ImageName, |
| 169 | + Description = dto.Description, |
| 170 | + FileSize = dto.FileSize, |
| 171 | + Downloads = dto.Downloads, |
| 172 | + }; |
| 173 | + |
| 174 | + var result = await _mediator.Send(command, cancellationToken).ConfigureAwait(false); |
| 175 | + |
| 176 | + return Ok(result); |
| 177 | + } |
| 178 | + |
| 179 | + [HttpDelete("{id}")] |
| 180 | + [Authorize(Policy = PolicyNames.EditModule)] |
| 181 | + [ProducesResponseType(StatusCodes.Status204NoContent)] |
| 182 | + [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| 183 | + public async Task<IActionResult> DeleteAsync( |
| 184 | + int id, |
| 185 | + [FromQuery] int moduleId, |
| 186 | + CancellationToken cancellationToken = default) |
| 187 | + { |
| 188 | + if (!IsAuthorizedEntityId(EntityNames.Module, moduleId)) |
| 189 | + { |
| 190 | + _logger.Log(LogLevel.Error, this, LogFunction.Security, |
| 191 | + "Unauthorized File Delete Attempt Id={Id} in ModuleId={ModuleId}", id, moduleId); |
| 192 | + return Forbid(); |
| 193 | + } |
| 194 | + |
| 195 | + if (id <= 0) |
| 196 | + { |
| 197 | + return BadRequest("Invalid File ID"); |
| 198 | + } |
| 199 | + |
| 200 | + var command = new DeleteFileRequest |
| 201 | + { |
| 202 | + ModuleId = moduleId, |
| 203 | + Id = id, |
| 204 | + }; |
| 205 | + |
| 206 | + await _mediator.Send(command, cancellationToken).ConfigureAwait(false); |
| 207 | + |
| 208 | + return NoContent(); |
| 209 | + } |
| 210 | +} |
0 commit comments