< Summary

Information
Class: Despesas.Backend.Controllers.DespesaController
Assembly: Despesas.Backend
File(s): /src/Despesas.Backend/Controllers/DespesaController.cs
Line coverage
100%
Covered lines: 32
Uncovered lines: 0
Coverable lines: 32
Total lines: 85
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Get()100%11100%
Get()100%22100%
Post()100%22100%
Put()100%22100%
Delete()100%22100%

File(s)

/src/Despesas.Backend/Controllers/DespesaController.cs

#LineLine coverage
 1using Despesas.Application.Abstractions;
 2using Despesas.Application.Dtos;
 3using Despesas.GlobalException.CustomExceptions.Core;
 4using Domain.Entities;
 5using Microsoft.AspNetCore.Authorization;
 6using Microsoft.AspNetCore.Mvc;
 7
 8namespace Despesas.Backend.Controllers;
 9public class DespesaController : AuthController
 10{
 11    private readonly IBusinessBase<DespesaDto, Despesa> _despesaBusiness;
 1112    public DespesaController(IBusinessBase<DespesaDto, Despesa> despesaBusiness)
 1113    {
 1114        _despesaBusiness = despesaBusiness;
 1115    }
 16
 17    [HttpGet]
 18    [Authorize("Bearer", Roles = "User, Admin")]
 19    [ProducesResponseType(200, Type = typeof(IList<DespesaDto>))]
 20    [ProducesResponseType(400, Type = typeof(string))]
 21    [ProducesResponseType(401)]
 22    [ProducesResponseType(403)]
 23    public async Task<IActionResult> Get()
 224    {
 225        return Ok(await _despesaBusiness.FindAll(UserIdentity));
 226    }
 27
 28    [HttpGet("GetById/{id}")]
 29    [Authorize("Bearer", Roles = "User, Admin")]
 30    [ProducesResponseType(200, Type = typeof(DespesaDto))]
 31    [ProducesResponseType(400, Type = typeof(string))]
 32    [ProducesResponseType(401)]
 33    [ProducesResponseType(403)]
 34    public async Task<IActionResult> Get([FromRoute] Guid id)
 235    {
 236        var despesa = await _despesaBusiness.FindById(id, UserIdentity)
 237            ?? throw new CustomException("Nenhuma despesa foi encontrada.");
 138        return Ok(despesa);
 139    }
 40
 41    [HttpPost]
 42    [Authorize("Bearer", Roles = "User, Admin")]
 43    [ProducesResponseType(200, Type = typeof(DespesaDto))]
 44    [ProducesResponseType(400, Type = typeof(string))]
 45    [ProducesResponseType(401)]
 46    [ProducesResponseType(403)]
 47    public async Task<IActionResult> Post([FromBody] DespesaDto despesa)
 248    {
 249        despesa.UsuarioId = UserIdentity;
 250        despesa = await _despesaBusiness.Create(despesa)
 251            ?? throw new CustomException("Não foi possível realizar o cadastro da despesa.");
 152        return Ok(despesa);
 153    }
 54
 55    [HttpPut]
 56    [Authorize("Bearer", Roles = "User, Admin")]
 57    [ProducesResponseType(200, Type = typeof(DespesaDto))]
 58    [ProducesResponseType(400, Type = typeof(string))]
 59    [ProducesResponseType(401)]
 60    [ProducesResponseType(403)]
 61    public async Task<IActionResult> Put([FromBody] DespesaDto despesa)
 262    {
 263        despesa.UsuarioId = UserIdentity;
 264        despesa = await _despesaBusiness.Update(despesa)
 265            ?? throw new CustomException("Não foi possível atualizar o cadastro da despesa.");
 166        return Ok(despesa);
 167    }
 68
 69    [HttpDelete("{idDespesa}")]
 70    [Authorize("Bearer", Roles = "User, Admin")]
 71    [ProducesResponseType(200, Type = typeof(bool))]
 72    [ProducesResponseType(400, Type = typeof(string))]
 73    [ProducesResponseType(401)]
 74    [ProducesResponseType(403)]
 75    public async Task<IActionResult> Delete(Guid idDespesa)
 376    {
 377        var despesa = new DespesaDto
 378        {
 379            Id = idDespesa,
 380            UsuarioId = UserIdentity
 381        };
 82
 383        return await _despesaBusiness.Delete(despesa) ? Ok(true) : BadRequest("Erro ao excluir Despesa!");
 384   }
 85}