< Summary

Information
Class: cat_cafe.Controllers.BarsController
Assembly: cat_cafe
File(s): C:\Users\draia\Documents\Dev\N3_CSHARP\cat_cafe\cat_cafe\Controllers\BarsController.cs
Line coverage
71%
Covered lines: 65
Uncovered lines: 26
Coverable lines: 91
Total lines: 164
Line coverage: 71.4%
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%
GetBars()100%1166.66%
GetBar()100%2275%
PutBar()50%4460%
PostBar()100%1173.33%
DeleteBar()100%2276.47%

File(s)

C:\Users\draia\Documents\Dev\N3_CSHARP\cat_cafe\cat_cafe\Controllers\BarsController.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 BarsController : ControllerBase
 14    {
 15        private readonly CatCafeContext _context;
 16        private readonly IMapper _mapper;
 17        private readonly ILogger<BarsController> _logger;
 18
 519        public BarsController(CatCafeContext context, IMapper mapper, ILogger<BarsController> logger)
 520        {
 521            _context = context;
 522            _mapper = mapper;
 523            _logger = logger;
 524        }
 25
 26        // GET: api/v1/Bars
 27        [HttpGet]
 28        public async Task<ActionResult<IEnumerable<BarDto>>> GetBars()
 129        {
 30            try
 131            {
 132                var bars = await _context.Bars
 133                               .Include(b => b.Cats)
 134                               .ToListAsync();
 135                _logger.LogInformation("Bars retrieved successfully.");
 136                return Ok(_mapper.Map<IEnumerable<BarDto>>(bars));
 37            }
 038            catch (Exception ex)
 039            {
 040                _logger.LogError(ex, "Failed to retrieve all bars.");
 041                return BadRequest(ex);
 42            }
 43
 144        }
 45
 46        // GET: api/v1/Bars/5
 47        [HttpGet("{id}")]
 48        public async Task<ActionResult<BarDto>> GetBar(long id)
 449        {
 50            try
 451            {
 452                var bar = await _context.Bars
 453                                .Include(b => b.Cats)
 454                                .SingleOrDefaultAsync(b => b.Id == id);
 55
 456                if (bar == null)
 157                {
 158                    _logger.LogError("No such bar.");
 159                    return NotFound();
 60                }
 61
 362                _logger.LogInformation("Bar retrieved successfully.");
 363                return Ok(_mapper.Map<BarDto>(bar));
 64            }
 065            catch (Exception ex)
 066            {
 067                _logger.LogError(ex, "Failed to retrieve bar.");
 068                return BadRequest(ex);
 69            }
 70
 471        }
 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)
 177        {
 78            try
 179            {
 180                if (id != barDto.Id)
 081                {
 082                    _logger.LogError("No such bar.");
 083                    return BadRequest();
 84                }
 85
 186                var bar = await _context.Bars
 187                    .Include(b => b.Cats)
 188                    .SingleOrDefaultAsync(b => b.Id == id);
 89
 190                if (bar == null)
 091                {
 092                    _logger.LogInformation("Bar not found.");
 093                    return NotFound();
 94                }
 95
 196                _mapper.Map(barDto, bar);
 197                bar.Cats = await _context.Cats
 198                    .Where(c => barDto.CatIds.Contains(c.Id))
 199                    .ToListAsync();
 100
 1101                await _context.SaveChangesAsync();
 1102                _logger.LogInformation("Bar updated successfully.");
 1103                return NoContent();
 104            }
 0105            catch (Exception ex)
 0106            {
 0107                _logger.LogError(ex, "Failed to update bar.");
 0108                return BadRequest(ex);
 109            }
 110
 1111        }
 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)
 1117        {
 118            try
 1119            {
 1120                var bar = _mapper.Map<Bar>(barDto);
 1121                bar.Cats = await _context.Cats
 1122                    .Where(c => barDto.CatIds.Contains(c.Id))
 1123                    .ToListAsync();
 124
 1125                _context.Bars.Add(bar);
 1126                await _context.SaveChangesAsync();
 1127                _logger.LogInformation("Bar created succesfully.");
 1128                return CreatedAtAction(nameof(GetBar), new { id = bar.Id }, _mapper.Map<BarDto>(bar));
 129            }
 0130            catch (Exception ex)
 0131            {
 0132                _logger.LogError(ex, "Failed to create bar.");
 0133                return BadRequest(ex);
 134            }
 135
 1136        }
 137
 138        // DELETE: api/v1/Bars/5
 139        [HttpDelete("{id}")]
 140        public async Task<IActionResult> DeleteBar(long id)
 1141        {
 142            try
 1143            {
 1144                var bar = await _context.Bars
 1145                    .Include(b => b.Cats)
 1146                    .SingleOrDefaultAsync(b => b.Id == id);
 147
 1148                if (bar != null)
 1149                {
 1150                    _context.Bars.Remove(bar);
 1151                    await _context.SaveChangesAsync();
 1152                    _logger.LogInformation("Bar deleted succesfully.");
 1153                }
 154
 1155                return NoContent();
 156            }
 0157            catch (Exception ex)
 0158            {
 0159                _logger.LogError(ex, "Failed to delete bar.");
 0160                return BadRequest(ex);
 161            }
 1162        }
 163    }
 164}