|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Models; |
| 3 | + |
| 4 | +namespace Api.Controllers; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// API controller for managing project boards. |
| 8 | +/// </summary> |
| 9 | +[ApiController] |
| 10 | +[Route("api/[controller]")] |
| 11 | +[Tags("Project Boards")] |
| 12 | +public class ProjectBoardsController : ControllerBase |
| 13 | +{ |
| 14 | + // In-memory storage for demo purposes |
| 15 | + internal static readonly List<ProjectBoard> Boards = new(); |
| 16 | + private static int _nextBoardId = 1; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Retrieves all project boards. |
| 20 | + /// </summary> |
| 21 | + /// <returns>A collection of all project boards.</returns> |
| 22 | + /// <response code="200">Returns the list of project boards.</response> |
| 23 | + [HttpGet] |
| 24 | + [ProducesResponseType(StatusCodes.Status200OK)] |
| 25 | + public ActionResult<IEnumerable<ProjectBoard>> GetAllProjectBoards() |
| 26 | + { |
| 27 | + return Ok(Boards); |
| 28 | + } |
| 29 | + |
| 30 | + // <snippet_1> |
| 31 | + /// <summary> |
| 32 | + /// Retrieves a specific project board by ID. |
| 33 | + /// </summary> |
| 34 | + /// <param name="id">The ID of the project board to retrieve.</param> |
| 35 | + /// <returns>The requested project board.</returns> |
| 36 | + /// <response code="200">Returns the requested project board.</response> |
| 37 | + /// <response code="404">If the project board is not found.</response> |
| 38 | + [HttpGet("{id}")] |
| 39 | + [ProducesResponseType(StatusCodes.Status200OK)] |
| 40 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 41 | + public ActionResult<ProjectBoard> GetProjectBoardById(int id) |
| 42 | + { |
| 43 | + var board = Boards.FirstOrDefault(b => b.Id == id); |
| 44 | + if (board == null) |
| 45 | + { |
| 46 | + return NotFound(); |
| 47 | + } |
| 48 | + return Ok(board); |
| 49 | + } |
| 50 | + // </snippet_1> |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Creates a new project board. |
| 54 | + /// </summary> |
| 55 | + /// <param name="board">The project board to create.</param> |
| 56 | + /// <returns>The newly created project board.</returns> |
| 57 | + /// <response code="201">Returns the newly created project board.</response> |
| 58 | + /// <response code="400">If the project board data is invalid.</response> |
| 59 | + [HttpPost] |
| 60 | + [ProducesResponseType(StatusCodes.Status201Created)] |
| 61 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 62 | + public ActionResult<ProjectBoard> CreateProjectBoard(ProjectBoard board) |
| 63 | + { |
| 64 | + board.Id = _nextBoardId++; |
| 65 | + board.CreatedAt = DateTime.UtcNow; |
| 66 | + Boards.Add(board); |
| 67 | + |
| 68 | + return CreatedAtAction(nameof(GetProjectBoardById), new { id = board.Id }, board); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Updates an existing project board. |
| 73 | + /// </summary> |
| 74 | + /// <param name="id">The ID of the project board to update.</param> |
| 75 | + /// <param name="updatedBoard">The updated project board data.</param> |
| 76 | + /// <returns>No content if successful.</returns> |
| 77 | + /// <response code="204">If the update was successful.</response> |
| 78 | + /// <response code="400">If the project board data is invalid.</response> |
| 79 | + /// <response code="404">If the project board is not found.</response> |
| 80 | + [HttpPut("{id}")] |
| 81 | + [ProducesResponseType(StatusCodes.Status204NoContent)] |
| 82 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 83 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 84 | + public IActionResult UpdateProjectBoard(int id, ProjectBoard updatedBoard) |
| 85 | + { |
| 86 | + var board = Boards.FirstOrDefault(b => b.Id == id); |
| 87 | + if (board == null) |
| 88 | + { |
| 89 | + return NotFound(); |
| 90 | + } |
| 91 | + |
| 92 | + board.Name = updatedBoard.Name; |
| 93 | + board.Description = updatedBoard.Description; |
| 94 | + |
| 95 | + return NoContent(); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Deletes a project board. |
| 100 | + /// </summary> |
| 101 | + /// <param name="id">The ID of the project board to delete.</param> |
| 102 | + /// <returns>No content if successful.</returns> |
| 103 | + /// <response code="204">If the deletion was successful.</response> |
| 104 | + /// <response code="404">If the project board is not found.</response> |
| 105 | + [HttpDelete("{id}")] |
| 106 | + [ProducesResponseType(StatusCodes.Status204NoContent)] |
| 107 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 108 | + public IActionResult DeleteProjectBoard(int id) |
| 109 | + { |
| 110 | + var board = Boards.FirstOrDefault(b => b.Id == id); |
| 111 | + if (board == null) |
| 112 | + { |
| 113 | + return NotFound(); |
| 114 | + } |
| 115 | + |
| 116 | + Boards.Remove(board); |
| 117 | + return NoContent(); |
| 118 | + } |
| 119 | +} |
0 commit comments