< Summary

Information
Class: cat_cafe.Controllers.CatsController
Assembly: cat_cafe
File(s): C:\Users\draia\Documents\Dev\N3_CSHARP\cat_cafe\cat_cafe\Controllers\CatsController.cs
Line coverage
61%
Covered lines: 59
Uncovered lines: 37
Coverable lines: 96
Total lines: 182
Line coverage: 61.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%
GetCats()100%110%
GetCatsV2()100%1160%
GetCat()100%2271.42%
PutCat()50%4452.38%
PostCat()100%1169.23%
DeleteCat()100%2273.33%

File(s)

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

#LineLine coverage
 1using AutoMapper;
 2using cat_cafe.Dto;
 3using cat_cafe.Entities;
 4using cat_cafe.Repositories;
 5using cat_cafe.WeSo;
 6using Microsoft.AspNetCore.Mvc;
 7using Microsoft.EntityFrameworkCore;
 8
 9namespace 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
 522        public CatsController(
 523            CatCafeContext context,
 524            IMapper mapper,
 525            ILogger<CatsController> logger,
 526            WebSocketHandler webSocketHandler
 527            )
 528        {
 529            _mapper = mapper;
 530            _context = context;
 531            _logger = logger;
 532            _webSocketHandler = webSocketHandler;
 533        }
 34
 35        // GET: api/v1/Cats
 36        [HttpGet]
 37        [MapToApiVersion("1.0")]
 38
 39        public async Task<ActionResult<IEnumerable<CatDto>>> GetCats()
 040        {
 41            try
 042            {
 043                var cats = await _context.Cats.ToListAsync();
 044                cats.Add(new Cat { Id = -1, Age = 42, Name = "Hi! I'm the secret V1 cat" });
 045                _logger.LogInformation("Cats retrieved successfully.");
 046                return Ok(_mapper.Map<List<CatDto>>(cats));
 47            }
 048            catch (Exception ex)
 049            {
 050                _logger.LogError(ex, "Failed to retrieve all cats.");
 051                return BadRequest(ex);
 52            }
 053        }
 54
 55
 56        // GET: api/v2/Cats
 57        [HttpGet]
 58        [MapToApiVersion("2.0")]
 59
 60        public async Task<ActionResult<IEnumerable<CatDto>>> GetCatsV2()
 161        {
 62            try
 163            {
 164                var cats = await _context.Cats.ToListAsync();
 65
 166                _logger.LogInformation("Cats retrieved successfully.");
 167                return Ok(_mapper.Map<List<CatDto>>(cats));
 68            }
 069            catch (Exception ex)
 070            {
 071                _logger.LogError(ex, "Failed to retrieve all cats.");
 072                return BadRequest(ex);
 73            }
 174        }
 75
 76        // GET: api/v1/Cats/5
 77        [HttpGet("{id}")]
 78        public async Task<ActionResult<CatDto>> GetCat(long id)
 479        {
 80            try
 481            {
 482                var cat = await _context.Cats.FindAsync(id);
 83
 484                if (cat == null)
 185                {
 186                    _logger.LogInformation("Cat not found.");
 187                    return NotFound();
 88                }
 389                _logger.LogInformation("Cat retrieved successfully.");
 390                return Ok(_mapper.Map<CatDto>(cat));
 91            }
 092            catch (Exception ex)
 093            {
 094                _logger.LogError(ex, "Failed to retrieve cat.");
 095                return BadRequest(ex);
 96            }
 497        }
 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)
 1103        {
 104            try
 1105            {
 1106                if (id != catDto.Id)
 0107                {
 0108                    _logger.LogError("No such cat.");
 0109                    return BadRequest();
 110                }
 111
 1112                var cat = await _context.Cats
 1113                    .SingleOrDefaultAsync(c => c.Id == id);
 114
 1115                if (cat == null)
 0116                {
 0117                    _logger.LogInformation("Cat not found.");
 0118                    return NotFound();
 119                }
 120
 1121                _mapper.Map(catDto, cat);
 122
 1123                await _context.SaveChangesAsync();
 124
 1125                _logger.LogInformation("Cat updated successfully.");
 1126                return NoContent();
 127            }
 128
 0129            catch (Exception ex)
 0130            {
 0131                _logger.LogError(ex, "Failed to update cat.");
 0132                return BadRequest(ex);
 133            }
 1134        }
 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)
 1140        {
 141            try
 1142            {
 1143                Cat cat = _mapper.Map<Cat>(catDto);
 1144                _context.Cats.Add(cat);
 1145                await _context.SaveChangesAsync();
 146
 1147                await _webSocketHandler.BroadcastMessageAsync("entity-created");
 1148                _logger.LogInformation("Cat created successfully.");
 1149                return CreatedAtAction("GetCat", new { id = catDto.Id }, _mapper.Map<CatDto>(cat));
 150
 151            }
 0152            catch (Exception ex)
 0153            {
 0154                _logger.LogError(ex, "Failed to create cat.");
 0155                return BadRequest(ex);
 156            }
 1157        }
 158
 159        // DELETE: api/v1/Cats/5
 160        [HttpDelete("{id}")]
 161        public async Task<IActionResult> DeleteCat(long id)
 1162        {
 163            try
 1164            {
 1165                var cat = await _context.Cats.FindAsync(id);
 1166                if (cat != null)
 1167                {
 1168                    _context.Cats.Remove(cat);
 1169                    await _context.SaveChangesAsync();
 1170                    _logger.LogInformation("Cat deleted successfully.");
 1171                }
 172
 1173                return NoContent();
 174            }
 0175            catch (Exception ex)
 0176            {
 0177                _logger.LogError(ex, "Failed to delete cat.");
 0178                return BadRequest(ex);
 179            }
 1180        }
 181    }
 182}