| | 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 CustomersController : ControllerBase |
| | 14 | | { |
| | 15 | | private readonly CatCafeContext _context; |
| | 16 | | private readonly IMapper _mapper; |
| | 17 | | private readonly ILogger<CustomersController> _logger; |
| | 18 | |
|
| 5 | 19 | | public CustomersController(CatCafeContext context, IMapper mapper, ILogger<CustomersController> logger) |
| 5 | 20 | | { |
| 5 | 21 | | _context = context; |
| 5 | 22 | | _mapper = mapper; |
| 5 | 23 | | _logger = logger; |
| 5 | 24 | | } |
| | 25 | |
|
| | 26 | | // GET: api/v1/Customers |
| | 27 | | [HttpGet] |
| | 28 | | public async Task<ActionResult<IEnumerable<CustomerDto>>> GetCustomers() |
| 1 | 29 | | { |
| | 30 | | try |
| 1 | 31 | | { |
| 1 | 32 | | var customers = await _context.Customers.ToListAsync(); |
| 1 | 33 | | _logger.LogInformation("Customers retrieved successfully."); |
| 1 | 34 | | return Ok(_mapper.Map<List<CustomerDto>>(customers)); |
| | 35 | | } |
| 0 | 36 | | catch (Exception ex) |
| 0 | 37 | | { |
| 0 | 38 | | _logger.LogError(ex, "Failed to retrieve customers."); |
| 0 | 39 | | return BadRequest(ex); |
| | 40 | | } |
| 1 | 41 | | } |
| | 42 | |
|
| | 43 | | // GET: api/v1/Customers/5 |
| | 44 | | [HttpGet("{id}")] |
| | 45 | | public async Task<ActionResult<CustomerDto>> GetCustomer(long id) |
| 4 | 46 | | { |
| | 47 | | try |
| 4 | 48 | | { |
| 4 | 49 | | var customer = await _context.Customers.FindAsync(id); |
| | 50 | |
|
| 4 | 51 | | if (customer == null) |
| 1 | 52 | | { |
| 1 | 53 | | _logger.LogInformation("Customer not found."); |
| 1 | 54 | | return NotFound(); |
| | 55 | | } |
| | 56 | |
|
| 3 | 57 | | _logger.LogInformation("Customer retrieved successfully."); |
| 3 | 58 | | return Ok(_mapper.Map<CustomerDto>(customer)); |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | catch (Exception ex) |
| 0 | 62 | | { |
| 0 | 63 | | _logger.LogError(ex, "Failed to retrieve customer."); |
| 0 | 64 | | return BadRequest(ex); |
| | 65 | | } |
| 4 | 66 | | } |
| | 67 | |
|
| | 68 | | // PUT: api/v1/Customers/5 |
| | 69 | | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 |
| | 70 | | [HttpPut("{id}")] |
| | 71 | | public async Task<IActionResult> PutCustomer(long id, CustomerDto customerDto) |
| 1 | 72 | | { |
| | 73 | | try |
| 1 | 74 | | { |
| 1 | 75 | | if (id != customerDto.Id) |
| 0 | 76 | | { |
| 0 | 77 | | _logger.LogError("No such customer."); |
| 0 | 78 | | return BadRequest(); |
| | 79 | | } |
| | 80 | |
|
| 1 | 81 | | var customer = await _context.Customers |
| 1 | 82 | | .SingleOrDefaultAsync(c => c.Id == id); |
| | 83 | |
|
| 1 | 84 | | if (customer == null) |
| 0 | 85 | | { |
| 0 | 86 | | _logger.LogInformation("Customer not found."); |
| 0 | 87 | | return NotFound(); |
| | 88 | | } |
| | 89 | |
|
| 1 | 90 | | _mapper.Map(customerDto, customer); |
| | 91 | |
|
| 1 | 92 | | await _context.SaveChangesAsync(); |
| | 93 | |
|
| 1 | 94 | | _logger.LogInformation("Customer updated successfully."); |
| 1 | 95 | | return NoContent(); |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | catch (Exception ex) |
| 0 | 99 | | { |
| 0 | 100 | | _logger.LogError(ex, "Failed to update customer."); |
| 0 | 101 | | return BadRequest(ex); |
| | 102 | | } |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | // POST: api/v1/Customers |
| | 106 | | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 |
| | 107 | | [HttpPost] |
| | 108 | | public async Task<ActionResult<CustomerDto>> PostCustomer(CustomerDto customerDto) |
| 1 | 109 | | { |
| | 110 | | try |
| 1 | 111 | | { |
| 1 | 112 | | Customer customer = _mapper.Map<Customer>(customerDto); |
| 1 | 113 | | _context.Customers.Add(customer); |
| 1 | 114 | | await _context.SaveChangesAsync(); |
| | 115 | |
|
| 1 | 116 | | _logger.LogInformation("Customer created successfully."); |
| 1 | 117 | | return CreatedAtAction("GetCustomer", new { id = customer.Id }, _mapper.Map<CustomerDto>(customer)); |
| | 118 | | } |
| 0 | 119 | | catch (Exception ex) |
| 0 | 120 | | { |
| 0 | 121 | | _logger.LogError(ex, "Failed to create customer."); |
| 0 | 122 | | return BadRequest(ex); |
| | 123 | | } |
| | 124 | |
|
| 1 | 125 | | } |
| | 126 | |
|
| | 127 | | // DELETE: api/v1/Customers/5 |
| | 128 | | [HttpDelete("{id}")] |
| | 129 | | public async Task<IActionResult> DeleteCustomer(long id) |
| 1 | 130 | | { |
| | 131 | | try |
| 1 | 132 | | { |
| 1 | 133 | | var customer = await _context.Customers.FindAsync(id); |
| 1 | 134 | | if (customer != null) |
| 1 | 135 | | { |
| 1 | 136 | | _context.Customers.Remove(customer); |
| 1 | 137 | | await _context.SaveChangesAsync(); |
| 1 | 138 | | _logger.LogInformation("Customer deleted successfully."); |
| 1 | 139 | | } |
| | 140 | |
|
| 1 | 141 | | return NoContent(); |
| | 142 | | } |
| 0 | 143 | | catch (Exception ex) |
| 0 | 144 | | { |
| 0 | 145 | | _logger.LogError(ex, "Failed to delete customer."); |
| 0 | 146 | | return BadRequest(ex); |
| | 147 | | } |
| 1 | 148 | | } |
| | 149 | | } |
| | 150 | | } |