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