|
| 1 | +using System.Security.Claims; |
| 2 | +using ErrorOr; |
| 3 | +using Microsoft.AspNetCore.Authorization; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | +using ShoeStore.Application.DTOs; |
| 6 | +using ShoeStore.Application.DTOs.VoucherDtos; |
| 7 | +using ShoeStore.Application.Interface.VoucherInterface; |
| 8 | + |
| 9 | +namespace ShoeStore.Api.Controllers; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Controller for managing vouchers in the system. |
| 13 | +/// Provides endpoints for voucher creation, retrieval, update, and deletion (Admin only). |
| 14 | +/// </summary> |
| 15 | +/// <param name="voucherService">Service for handling voucher logic operations.</param> |
| 16 | +[ApiController] |
| 17 | +[Route("api/admin/vouchers")] |
| 18 | +[Authorize(Roles = "Admin")] |
| 19 | +public class VoucherController(IVoucherService voucherService) : ControllerBase |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Creates a new voucher for the store. |
| 23 | + /// </summary> |
| 24 | + /// <remarks> |
| 25 | + /// Requires Admin role authorization. |
| 26 | + /// The request body should include: |
| 27 | + /// - <c>VoucherName</c>: Name of the voucher |
| 28 | + /// - <c>Discount</c>: Value of the discount |
| 29 | + /// - <c>DiscountType</c>: Type of discount (Percentage/FixedAmount) |
| 30 | + /// - <c>TotalQuantity</c>: Number of vouchers available |
| 31 | + /// - <c>ValidFrom/ValidTo</c>: Expiration dates |
| 32 | + /// </remarks> |
| 33 | + /// <param name="createVoucherDto">Data transfer object containing voucher creation details.</param> |
| 34 | + /// <param name="token">Cancellation token for the request.</param> |
| 35 | + /// <response code="201">Voucher created successfully.</response> |
| 36 | + /// <response code="400">Bad request; invalid voucher data provided.</response> |
| 37 | + /// <response code="401">Unauthorized; user must be authenticated with Admin role.</response> |
| 38 | + /// <response code="500">Internal server error; an unexpected error occurred.</response> |
| 39 | + /// <returns>An action result with status 201 (Created) on success, or an error response.</returns> |
| 40 | + [ProducesResponseType(typeof(object), StatusCodes.Status201Created)] |
| 41 | + [ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)] |
| 42 | + [ProducesResponseType(typeof(object), StatusCodes.Status401Unauthorized)] |
| 43 | + [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] |
| 44 | + [HttpPost] |
| 45 | + public async Task<IActionResult> CreateVoucher([FromBody] CreateVoucherDto createVoucherDto, CancellationToken token) |
| 46 | + { |
| 47 | + var result = await voucherService.CreateVoucherAsync(createVoucherDto, token); |
| 48 | + |
| 49 | + return await result.MatchAsync<IActionResult>( |
| 50 | + async _ => |
| 51 | + { |
| 52 | + var adminEmail = User.FindFirstValue(ClaimTypes.Email) ?? "[email protected]"; |
| 53 | + |
| 54 | + await voucherService.NotifyUserAboutNewVoucherAsync( |
| 55 | + adminEmail: adminEmail, |
| 56 | + voucherName: createVoucherDto.VoucherName ?? "New Discount", |
| 57 | + validTo: createVoucherDto.ValidTo ?? DateTime.UtcNow, |
| 58 | + token: token |
| 59 | + ); |
| 60 | + |
| 61 | + return Created("", new { message = "Voucher created and users notified" }); |
| 62 | + }, |
| 63 | + errors => Task.FromResult<IActionResult>(BadRequest(new |
| 64 | + { |
| 65 | + message = "Failed to create voucher", |
| 66 | + detail = errors[0].Description |
| 67 | + })) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Updates an existing voucher's details. |
| 73 | + /// </summary> |
| 74 | + /// <remarks> |
| 75 | + /// Requires Admin role authorization. |
| 76 | + /// Updates the specified voucher with the provided information. |
| 77 | + /// </remarks> |
| 78 | + /// <param name="voucherGuid">The unique identifier (GUID) of the voucher to update.</param> |
| 79 | + /// <param name="updateVoucherDto">Data transfer object containing updated voucher details.</param> |
| 80 | + /// <param name="token">Cancellation token for the request.</param> |
| 81 | + /// <response code="200">Voucher updated successfully.</response> |
| 82 | + /// <response code="400">Bad request; invalid update data provided.</response> |
| 83 | + /// <response code="401">Unauthorized; user must be authenticated with Admin role.</response> |
| 84 | + /// <response code="404">Not found; the voucher with the specified ID does not exist.</response> |
| 85 | + /// <response code="500">Internal server error; an unexpected error occurred.</response> |
| 86 | + /// <returns>An action result with status 200 (OK) on success, or an error response.</returns> |
| 87 | + [ProducesResponseType(typeof(object), StatusCodes.Status200OK)] |
| 88 | + [ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)] |
| 89 | + [ProducesResponseType(typeof(object), StatusCodes.Status401Unauthorized)] |
| 90 | + [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] |
| 91 | + [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] |
| 92 | + [HttpPut("{voucherGuid}")] |
| 93 | + public async Task<IActionResult> UpdateVoucher(Guid voucherGuid, [FromForm] UpdateVoucherDto updateVoucherDto, |
| 94 | + CancellationToken token) |
| 95 | + { |
| 96 | + var result = await voucherService.UpdateVoucherAsync(voucherGuid, updateVoucherDto, token); |
| 97 | + return result.Match<IActionResult>( |
| 98 | + _ => Ok(new { message = "Voucher updated successfully" }), |
| 99 | + errors => errors[0].Code switch |
| 100 | + { |
| 101 | + "VOUCHER_NOT_FOUND" => NotFound(new |
| 102 | + { |
| 103 | + message = "Voucher not found", |
| 104 | + detail = errors[0].Description |
| 105 | + }), |
| 106 | + _ => BadRequest(new |
| 107 | + { |
| 108 | + message = "Failed to update voucher", |
| 109 | + detail = errors[0].Description |
| 110 | + }) |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Deletes a specific voucher from the system (soft delete). |
| 116 | + /// </summary> |
| 117 | + /// <remarks> |
| 118 | + /// Requires Admin role authorization. |
| 119 | + /// Performs a soft delete by marking the voucher as deleted. |
| 120 | + /// </remarks> |
| 121 | + /// <param name="voucherGuid">The unique identifier (GUID) of the voucher to delete.</param> |
| 122 | + /// <param name="token">Cancellation token for the request.</param> |
| 123 | + /// <response code="200">Voucher deleted successfully.</response> |
| 124 | + /// <response code="401">Unauthorized; user must be authenticated with Admin role.</response> |
| 125 | + /// <response code="404">Not found; the voucher with the specified ID does not exist.</response> |
| 126 | + /// <response code="500">Internal server error; an unexpected error occurred.</response> |
| 127 | + /// <returns>An action result with status 200 (OK) on success, or an error response.</returns> |
| 128 | + [ProducesResponseType(typeof(object), StatusCodes.Status200OK)] |
| 129 | + [ProducesResponseType(typeof(object), StatusCodes.Status401Unauthorized)] |
| 130 | + [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] |
| 131 | + [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] |
| 132 | + [HttpDelete("{voucherGuid}")] |
| 133 | + public async Task<IActionResult> DeleteVoucher(Guid voucherGuid, CancellationToken token) |
| 134 | + { |
| 135 | + var result = await voucherService.DeleteVoucherByGuidAsync(voucherGuid, token); |
| 136 | + return result.Match<IActionResult>( |
| 137 | + _ => Ok(new { message = "Voucher deleted successfully" }), |
| 138 | + errors => errors[0].Code switch |
| 139 | + { |
| 140 | + "VOUCHER_NOT_FOUND" => NotFound(new |
| 141 | + { |
| 142 | + message = "Voucher not found", |
| 143 | + detail = errors[0].Description |
| 144 | + }), |
| 145 | + _ => BadRequest(new |
| 146 | + { |
| 147 | + message = "Failed to delete voucher", |
| 148 | + detail = errors[0].Description |
| 149 | + }) |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Deletes all expired vouchers from the system (soft delete). |
| 155 | + /// </summary> |
| 156 | + /// <remarks> |
| 157 | + /// Requires Admin role authorization. |
| 158 | + /// Identifies and soft deletes all vouchers whose expiration date has passed. |
| 159 | + /// </remarks> |
| 160 | + /// <param name="token">Cancellation token for the request.</param> |
| 161 | + /// <response code="200">Expired vouchers deleted successfully.</response> |
| 162 | + /// <response code="400">Bad request; failed to delete expired vouchers or no expired vouchers found.</response> |
| 163 | + /// <response code="401">Unauthorized; user must be authenticated with Admin role.</response> |
| 164 | + /// <response code="500">Internal server error; an unexpected error occurred.</response> |
| 165 | + /// <returns>An action result with status 200 (OK) on success, or an error response.</returns> |
| 166 | + [ProducesResponseType(typeof(object), StatusCodes.Status200OK)] |
| 167 | + [ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)] |
| 168 | + [ProducesResponseType(typeof(object), StatusCodes.Status401Unauthorized)] |
| 169 | + [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] |
| 170 | + [HttpDelete("expire")] |
| 171 | + public async Task<IActionResult> DeleteExpiredVouchers(CancellationToken token) |
| 172 | + { |
| 173 | + var result = await voucherService.DeleteVoucherExpireAsync(token); |
| 174 | + return result.Match<IActionResult>( |
| 175 | + _ => Ok(new { message = "Expired vouchers deleted successfully" }), |
| 176 | + errors => BadRequest(new |
| 177 | + { |
| 178 | + message = "Failed to delete expired vouchers", |
| 179 | + detail = errors[0].Description |
| 180 | + })); |
| 181 | + } |
| 182 | + |
| 183 | + /// <summary> |
| 184 | + /// Retrieves a paginated list of vouchers for administrative purposes. |
| 185 | + /// </summary> |
| 186 | + /// <remarks> |
| 187 | + /// Requires Admin role authorization. |
| 188 | + /// Provides a list of vouchers with detailed information relevant for administrators. |
| 189 | + /// </remarks> |
| 190 | + /// <param name="token">Cancellation token for the request.</param> |
| 191 | + /// <response code="200">Vouchers retrieved successfully.</response> |
| 192 | + /// <response code="401">Unauthorized; user must be authenticated with Admin role.</response> |
| 193 | + /// <response code="404">Not found; no vouchers found in the system.</response> |
| 194 | + /// <response code="500">Internal server error; an unexpected error occurred.</response> |
| 195 | + /// <returns>An action result containing a paginated list of vouchers on success, or an error response.</returns> |
| 196 | + [ProducesResponseType(typeof(PageResult<ResponseVoucherAdminDto>), StatusCodes.Status200OK)] |
| 197 | + [ProducesResponseType(typeof(object), StatusCodes.Status401Unauthorized)] |
| 198 | + [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] |
| 199 | + [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] |
| 200 | + [HttpGet] |
| 201 | + public async Task<IActionResult> GetVouchersForAdmin(CancellationToken token) |
| 202 | + { |
| 203 | + var result = await voucherService.GetVoucherForAdminAsync(token); |
| 204 | + return result.Match<IActionResult>( |
| 205 | + vouchers => Ok(vouchers), |
| 206 | + errors => errors[0].Code switch |
| 207 | + { |
| 208 | + "NO_VOUCHERS_FOUND" => NotFound(new |
| 209 | + { |
| 210 | + message = "No vouchers found", |
| 211 | + detail = errors[0].Description |
| 212 | + }), |
| 213 | + _ => BadRequest(new |
| 214 | + { |
| 215 | + message = "Failed to retrieve vouchers", |
| 216 | + detail = errors[0].Description |
| 217 | + }) |
| 218 | + }); |
| 219 | + } |
| 220 | +} |
0 commit comments