|
| 1 | +using FPTStella.Application.Common.Interfaces.Services; |
| 2 | +using FPTStella.Contracts.DTOs.Materials; |
| 3 | +using Microsoft.AspNetCore.Http; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | + |
| 6 | +namespace FPTStella.API.Controllers |
| 7 | +{ |
| 8 | + [Route("api/[controller]")] |
| 9 | + [ApiController] |
| 10 | + public class MaterialController : BaseController |
| 11 | + { |
| 12 | + private readonly IMaterialService _materialService; |
| 13 | + public MaterialController(IMaterialService materialService) |
| 14 | + { |
| 15 | + _materialService = materialService; |
| 16 | + } |
| 17 | + [HttpGet] |
| 18 | + public async Task<IActionResult> GetAllMaterials() |
| 19 | + { |
| 20 | + try |
| 21 | + { |
| 22 | + var materials = await _materialService.GetAllMaterialsAsync(); |
| 23 | + return Ok(materials); |
| 24 | + } |
| 25 | + catch (Exception ex) |
| 26 | + { |
| 27 | + return HandleException(ex); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + [HttpGet("{id}")] |
| 32 | + public async Task<IActionResult> GetMaterialById(string id) |
| 33 | + { |
| 34 | + try |
| 35 | + { |
| 36 | + var material = await _materialService.GetMaterialByIdAsync(id); |
| 37 | + return Ok(material); |
| 38 | + } |
| 39 | + catch (KeyNotFoundException ex) |
| 40 | + { |
| 41 | + return NotFound(ex.Message); |
| 42 | + } |
| 43 | + catch (Exception ex) |
| 44 | + { |
| 45 | + return HandleException(ex); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + [HttpGet("name/{name}")] |
| 50 | + public async Task<IActionResult> GetMaterialByName(string name) |
| 51 | + { |
| 52 | + try |
| 53 | + { |
| 54 | + var material = await _materialService.GetMaterialByNameAsync(name); |
| 55 | + if (material == null) |
| 56 | + { |
| 57 | + return NotFound($"Material with name '{name}' not found."); |
| 58 | + } |
| 59 | + return Ok(material); |
| 60 | + } |
| 61 | + catch (Exception ex) |
| 62 | + { |
| 63 | + return HandleException(ex); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + [HttpGet("subject/{subjectId}")] |
| 68 | + public async Task<IActionResult> GetMaterialsBySubjectId(Guid subjectId) |
| 69 | + { |
| 70 | + try |
| 71 | + { |
| 72 | + var materials = await _materialService.GetMaterialsBySubjectIdAsync(subjectId); |
| 73 | + return Ok(materials); |
| 74 | + } |
| 75 | + catch (Exception ex) |
| 76 | + { |
| 77 | + return HandleException(ex); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + [HttpGet("type/{type}")] |
| 82 | + public async Task<IActionResult> GetMaterialsByType(string type) |
| 83 | + { |
| 84 | + try |
| 85 | + { |
| 86 | + var materials = await _materialService.GetMaterialsByTypeAsync(type); |
| 87 | + return Ok(materials); |
| 88 | + } |
| 89 | + catch (Exception ex) |
| 90 | + { |
| 91 | + return HandleException(ex); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Search for materials with filters and pagination |
| 97 | + /// </summary> |
| 98 | + /// <param name="searchTerm">Optional search term</param> |
| 99 | + /// <param name="subjectId">Optional subject ID filter</param> |
| 100 | + /// <param name="materialType">Optional material type filter</param> |
| 101 | + /// <param name="pageNumber">Page number (default: 1)</param> |
| 102 | + /// <param name="pageSize">Page size (default: 10)</param> |
| 103 | + /// <returns>Paged results of materials</returns> |
| 104 | + [HttpGet("search")] |
| 105 | + public async Task<IActionResult> SearchMaterials( |
| 106 | + [FromQuery] string? searchTerm = null, |
| 107 | + [FromQuery] Guid? subjectId = null, |
| 108 | + [FromQuery] string? materialType = null, |
| 109 | + [FromQuery] int pageNumber = 1, |
| 110 | + [FromQuery] int pageSize = 10) |
| 111 | + { |
| 112 | + try |
| 113 | + { |
| 114 | + var results = await _materialService.SearchMaterialsAsync( |
| 115 | + searchTerm, |
| 116 | + subjectId, |
| 117 | + materialType, |
| 118 | + pageNumber, |
| 119 | + pageSize); |
| 120 | + |
| 121 | + return Ok(results); |
| 122 | + } |
| 123 | + catch (Exception ex) |
| 124 | + { |
| 125 | + return HandleException(ex); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + [HttpPost] |
| 130 | + public async Task<IActionResult> CreateMaterial([FromBody] CreateMaterialDto createMaterialDto) |
| 131 | + { |
| 132 | + try |
| 133 | + { |
| 134 | + var material = await _materialService.CreateMaterialAsync(createMaterialDto); |
| 135 | + return CreatedAtAction(nameof(GetMaterialById), new { id = material.Id }, material); |
| 136 | + } |
| 137 | + catch (InvalidOperationException ex) |
| 138 | + { |
| 139 | + return BadRequest(ex.Message); |
| 140 | + } |
| 141 | + catch (KeyNotFoundException ex) |
| 142 | + { |
| 143 | + return NotFound(ex.Message); |
| 144 | + } |
| 145 | + catch (Exception ex) |
| 146 | + { |
| 147 | + return HandleException(ex); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + [HttpPut("{id}")] |
| 152 | + public async Task<IActionResult> UpdateMaterial(string id, [FromBody] UpdateMaterialDto updateMaterialDto) |
| 153 | + { |
| 154 | + try |
| 155 | + { |
| 156 | + var success = await _materialService.UpdateMaterialAsync(id, updateMaterialDto); |
| 157 | + return success ? NoContent() : BadRequest("Failed to update material."); |
| 158 | + } |
| 159 | + catch (KeyNotFoundException ex) |
| 160 | + { |
| 161 | + return NotFound(ex.Message); |
| 162 | + } |
| 163 | + catch (InvalidOperationException ex) |
| 164 | + { |
| 165 | + return BadRequest(ex.Message); |
| 166 | + } |
| 167 | + catch (Exception ex) |
| 168 | + { |
| 169 | + return HandleException(ex); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + [HttpDelete("{id}")] |
| 174 | + public async Task<IActionResult> DeleteMaterial(string id) |
| 175 | + { |
| 176 | + try |
| 177 | + { |
| 178 | + var success = await _materialService.DeleteMaterialAsync(id); |
| 179 | + return success ? NoContent() : BadRequest("Failed to delete material."); |
| 180 | + } |
| 181 | + catch (KeyNotFoundException ex) |
| 182 | + { |
| 183 | + return NotFound(ex.Message); |
| 184 | + } |
| 185 | + catch (Exception ex) |
| 186 | + { |
| 187 | + return HandleException(ex); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + [HttpDelete("subject/{subjectId}")] |
| 192 | + public async Task<IActionResult> DeleteMaterialsBySubjectId(Guid subjectId) |
| 193 | + { |
| 194 | + try |
| 195 | + { |
| 196 | + var success = await _materialService.DeleteMaterialsBySubjectIdAsync(subjectId); |
| 197 | + return success ? NoContent() : BadRequest("Failed to delete materials for subject."); |
| 198 | + } |
| 199 | + catch (Exception ex) |
| 200 | + { |
| 201 | + return HandleException(ex); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments