< Summary

Information
Class: cat_cafe.Controllers.CustomersController
Assembly: cat_cafe
File(s): C:\Users\draia\Documents\Dev\N3_CSHARP\cat_cafe\cat_cafe\Controllers\CustomersController.cs
Line coverage
66%
Covered lines: 52
Uncovered lines: 26
Coverable lines: 78
Total lines: 150
Line coverage: 66.6%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
GetCustomers()100%1160%
GetCustomer()100%2271.42%
PutCustomer()50%4452.38%
PostCustomer()100%1166.66%
DeleteCustomer()100%2273.33%

File(s)

C:\Users\draia\Documents\Dev\N3_CSHARP\cat_cafe\cat_cafe\Controllers\CustomersController.cs

#LineLine coverage
 1using AutoMapper;
 2using cat_cafe.Dto;
 3using cat_cafe.Entities;
 4using cat_cafe.Repositories;
 5using Microsoft.AspNetCore.Mvc;
 6using Microsoft.EntityFrameworkCore;
 7
 8namespace 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
 519        public CustomersController(CatCafeContext context, IMapper mapper, ILogger<CustomersController> logger)
 520        {
 521            _context = context;
 522            _mapper = mapper;
 523            _logger = logger;
 524        }
 25
 26        // GET: api/v1/Customers
 27        [HttpGet]
 28        public async Task<ActionResult<IEnumerable<CustomerDto>>> GetCustomers()
 129        {
 30            try
 131            {
 132                var customers = await _context.Customers.ToListAsync();
 133                _logger.LogInformation("Customers retrieved successfully.");
 134                return Ok(_mapper.Map<List<CustomerDto>>(customers));
 35            }
 036            catch (Exception ex)
 037            {
 038                _logger.LogError(ex, "Failed to retrieve customers.");
 039                return BadRequest(ex);
 40            }
 141        }
 42
 43        // GET: api/v1/Customers/5
 44        [HttpGet("{id}")]
 45        public async Task<ActionResult<CustomerDto>> GetCustomer(long id)
 446        {
 47            try
 448            {
 449                var customer = await _context.Customers.FindAsync(id);
 50
 451                if (customer == null)
 152                {
 153                    _logger.LogInformation("Customer not found.");
 154                    return NotFound();
 55                }
 56
 357                _logger.LogInformation("Customer retrieved successfully.");
 358                return Ok(_mapper.Map<CustomerDto>(customer));
 59            }
 60
 061            catch (Exception ex)
 062            {
 063                _logger.LogError(ex, "Failed to retrieve customer.");
 064                return BadRequest(ex);
 65            }
 466        }
 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)
 172        {
 73            try
 174            {
 175                if (id != customerDto.Id)
 076                {
 077                    _logger.LogError("No such customer.");
 078                    return BadRequest();
 79                }
 80
 181                var customer = await _context.Customers
 182                    .SingleOrDefaultAsync(c => c.Id == id);
 83
 184                if (customer == null)
 085                {
 086                    _logger.LogInformation("Customer not found.");
 087                    return NotFound();
 88                }
 89
 190                _mapper.Map(customerDto, customer);
 91
 192                await _context.SaveChangesAsync();
 93
 194                _logger.LogInformation("Customer updated successfully.");
 195                return NoContent();
 96            }
 97
 098            catch (Exception ex)
 099            {
 0100                _logger.LogError(ex, "Failed to update customer.");
 0101                return BadRequest(ex);
 102            }
 1103        }
 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)
 1109        {
 110            try
 1111            {
 1112                Customer customer = _mapper.Map<Customer>(customerDto);
 1113                _context.Customers.Add(customer);
 1114                await _context.SaveChangesAsync();
 115
 1116                _logger.LogInformation("Customer created successfully.");
 1117                return CreatedAtAction("GetCustomer", new { id = customer.Id }, _mapper.Map<CustomerDto>(customer));
 118            }
 0119            catch (Exception ex)
 0120            {
 0121                _logger.LogError(ex, "Failed to create customer.");
 0122                return BadRequest(ex);
 123            }
 124
 1125        }
 126
 127        // DELETE: api/v1/Customers/5
 128        [HttpDelete("{id}")]
 129        public async Task<IActionResult> DeleteCustomer(long id)
 1130        {
 131            try
 1132            {
 1133                var customer = await _context.Customers.FindAsync(id);
 1134                if (customer != null)
 1135                {
 1136                    _context.Customers.Remove(customer);
 1137                    await _context.SaveChangesAsync();
 1138                    _logger.LogInformation("Customer deleted successfully.");
 1139                }
 140
 1141                return NoContent();
 142            }
 0143            catch (Exception ex)
 0144            {
 0145                _logger.LogError(ex, "Failed to delete customer.");
 0146                return BadRequest(ex);
 147            }
 1148        }
 149    }
 150}