| | 1 | | using AutoMapper; |
| | 2 | | using cat_cafe.Dto; |
| | 3 | | using cat_cafe.Entities; |
| | 4 | | using cat_cafe.Repositories; |
| | 5 | | using cat_cafe.WeSo; |
| | 6 | | using Microsoft.AspNetCore.Mvc; |
| | 7 | | using Microsoft.EntityFrameworkCore; |
| | 8 | |
|
| | 9 | | namespace cat_cafe.Controllers |
| | 10 | | { |
| | 11 | | [Route("api/v{version:apiVersion}/[controller]")] |
| | 12 | | [ApiController] |
| | 13 | | [ApiVersion("1.0")] |
| | 14 | | [ApiVersion("2.0")] |
| | 15 | | public class CatsController : ControllerBase |
| | 16 | | { |
| | 17 | | private readonly CatCafeContext _context; |
| | 18 | | private readonly IMapper _mapper; |
| | 19 | | private readonly ILogger<CatsController> _logger; |
| | 20 | | private readonly WebSocketHandler _webSocketHandler; |
| | 21 | |
|
| 5 | 22 | | public CatsController( |
| 5 | 23 | | CatCafeContext context, |
| 5 | 24 | | IMapper mapper, |
| 5 | 25 | | ILogger<CatsController> logger, |
| 5 | 26 | | WebSocketHandler webSocketHandler |
| 5 | 27 | | ) |
| 5 | 28 | | { |
| 5 | 29 | | _mapper = mapper; |
| 5 | 30 | | _context = context; |
| 5 | 31 | | _logger = logger; |
| 5 | 32 | | _webSocketHandler = webSocketHandler; |
| 5 | 33 | | } |
| | 34 | |
|
| | 35 | | // GET: api/v1/Cats |
| | 36 | | [HttpGet] |
| | 37 | | [MapToApiVersion("1.0")] |
| | 38 | |
|
| | 39 | | public async Task<ActionResult<IEnumerable<CatDto>>> GetCats() |
| 0 | 40 | | { |
| | 41 | | try |
| 0 | 42 | | { |
| 0 | 43 | | var cats = await _context.Cats.ToListAsync(); |
| 0 | 44 | | cats.Add(new Cat { Id = -1, Age = 42, Name = "Hi! I'm the secret V1 cat" }); |
| 0 | 45 | | _logger.LogInformation("Cats retrieved successfully."); |
| 0 | 46 | | return Ok(_mapper.Map<List<CatDto>>(cats)); |
| | 47 | | } |
| 0 | 48 | | catch (Exception ex) |
| 0 | 49 | | { |
| 0 | 50 | | _logger.LogError(ex, "Failed to retrieve all cats."); |
| 0 | 51 | | return BadRequest(ex); |
| | 52 | | } |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | |
|
| | 56 | | // GET: api/v2/Cats |
| | 57 | | [HttpGet] |
| | 58 | | [MapToApiVersion("2.0")] |
| | 59 | |
|
| | 60 | | public async Task<ActionResult<IEnumerable<CatDto>>> GetCatsV2() |
| 1 | 61 | | { |
| | 62 | | try |
| 1 | 63 | | { |
| 1 | 64 | | var cats = await _context.Cats.ToListAsync(); |
| | 65 | |
|
| 1 | 66 | | _logger.LogInformation("Cats retrieved successfully."); |
| 1 | 67 | | return Ok(_mapper.Map<List<CatDto>>(cats)); |
| | 68 | | } |
| 0 | 69 | | catch (Exception ex) |
| 0 | 70 | | { |
| 0 | 71 | | _logger.LogError(ex, "Failed to retrieve all cats."); |
| 0 | 72 | | return BadRequest(ex); |
| | 73 | | } |
| 1 | 74 | | } |
| | 75 | |
|
| | 76 | | // GET: api/v1/Cats/5 |
| | 77 | | [HttpGet("{id}")] |
| | 78 | | public async Task<ActionResult<CatDto>> GetCat(long id) |
| 4 | 79 | | { |
| | 80 | | try |
| 4 | 81 | | { |
| 4 | 82 | | var cat = await _context.Cats.FindAsync(id); |
| | 83 | |
|
| 4 | 84 | | if (cat == null) |
| 1 | 85 | | { |
| 1 | 86 | | _logger.LogInformation("Cat not found."); |
| 1 | 87 | | return NotFound(); |
| | 88 | | } |
| 3 | 89 | | _logger.LogInformation("Cat retrieved successfully."); |
| 3 | 90 | | return Ok(_mapper.Map<CatDto>(cat)); |
| | 91 | | } |
| 0 | 92 | | catch (Exception ex) |
| 0 | 93 | | { |
| 0 | 94 | | _logger.LogError(ex, "Failed to retrieve cat."); |
| 0 | 95 | | return BadRequest(ex); |
| | 96 | | } |
| 4 | 97 | | } |
| | 98 | |
|
| | 99 | | // PUT: api/v1/Cats/5 |
| | 100 | | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 |
| | 101 | | [HttpPut("{id}")] |
| | 102 | | public async Task<IActionResult> PutCat(long id, CatDto catDto) |
| 1 | 103 | | { |
| | 104 | | try |
| 1 | 105 | | { |
| 1 | 106 | | if (id != catDto.Id) |
| 0 | 107 | | { |
| 0 | 108 | | _logger.LogError("No such cat."); |
| 0 | 109 | | return BadRequest(); |
| | 110 | | } |
| | 111 | |
|
| 1 | 112 | | var cat = await _context.Cats |
| 1 | 113 | | .SingleOrDefaultAsync(c => c.Id == id); |
| | 114 | |
|
| 1 | 115 | | if (cat == null) |
| 0 | 116 | | { |
| 0 | 117 | | _logger.LogInformation("Cat not found."); |
| 0 | 118 | | return NotFound(); |
| | 119 | | } |
| | 120 | |
|
| 1 | 121 | | _mapper.Map(catDto, cat); |
| | 122 | |
|
| 1 | 123 | | await _context.SaveChangesAsync(); |
| | 124 | |
|
| 1 | 125 | | _logger.LogInformation("Cat updated successfully."); |
| 1 | 126 | | return NoContent(); |
| | 127 | | } |
| | 128 | |
|
| 0 | 129 | | catch (Exception ex) |
| 0 | 130 | | { |
| 0 | 131 | | _logger.LogError(ex, "Failed to update cat."); |
| 0 | 132 | | return BadRequest(ex); |
| | 133 | | } |
| 1 | 134 | | } |
| | 135 | |
|
| | 136 | | // POST: api/v1/Cats |
| | 137 | | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 |
| | 138 | | [HttpPost] |
| | 139 | | public async Task<ActionResult<CatDto>> PostCat(CatDto catDto) |
| 1 | 140 | | { |
| | 141 | | try |
| 1 | 142 | | { |
| 1 | 143 | | Cat cat = _mapper.Map<Cat>(catDto); |
| 1 | 144 | | _context.Cats.Add(cat); |
| 1 | 145 | | await _context.SaveChangesAsync(); |
| | 146 | |
|
| 1 | 147 | | await _webSocketHandler.BroadcastMessageAsync("entity-created"); |
| 1 | 148 | | _logger.LogInformation("Cat created successfully."); |
| 1 | 149 | | return CreatedAtAction("GetCat", new { id = catDto.Id }, _mapper.Map<CatDto>(cat)); |
| | 150 | |
|
| | 151 | | } |
| 0 | 152 | | catch (Exception ex) |
| 0 | 153 | | { |
| 0 | 154 | | _logger.LogError(ex, "Failed to create cat."); |
| 0 | 155 | | return BadRequest(ex); |
| | 156 | | } |
| 1 | 157 | | } |
| | 158 | |
|
| | 159 | | // DELETE: api/v1/Cats/5 |
| | 160 | | [HttpDelete("{id}")] |
| | 161 | | public async Task<IActionResult> DeleteCat(long id) |
| 1 | 162 | | { |
| | 163 | | try |
| 1 | 164 | | { |
| 1 | 165 | | var cat = await _context.Cats.FindAsync(id); |
| 1 | 166 | | if (cat != null) |
| 1 | 167 | | { |
| 1 | 168 | | _context.Cats.Remove(cat); |
| 1 | 169 | | await _context.SaveChangesAsync(); |
| 1 | 170 | | _logger.LogInformation("Cat deleted successfully."); |
| 1 | 171 | | } |
| | 172 | |
|
| 1 | 173 | | return NoContent(); |
| | 174 | | } |
| 0 | 175 | | catch (Exception ex) |
| 0 | 176 | | { |
| 0 | 177 | | _logger.LogError(ex, "Failed to delete cat."); |
| 0 | 178 | | return BadRequest(ex); |
| | 179 | | } |
| 1 | 180 | | } |
| | 181 | | } |
| | 182 | | } |