コード例 #1
0
        public async Task <IActionResult> Create([FromBody] ContaResource contaResource)
        {
            if (contaResource == null)
            {
                return(NotFound());
            }
            try
            {
                var conta = new Conta();
                if (ModelState.IsValid)
                {
                    conta = ContaMapper.ResourceToModel(contaResource, conta);
                }
                _unitOfWork.Contas.Add(conta);
                await _unitOfWork.CompleteAsync();

                Usuario u = await _unitOfWork.Usuarios.GetByIdAsync(conta.UsuarioId);

                u.ContaId = conta.Id;
                _unitOfWork.Usuarios.Update(u);
                await _unitOfWork.CompleteAsync();

                ContaMapper.ModelToResource(conta);

                return(Ok(conta));
            }
            catch (Exception exception)
            {
                LogError.LogErrorWithSentry(exception);
                return(BadRequest());
            }
        }
コード例 #2
0
        public async Task <IActionResult> GetAll()
        {
            try
            {
                var contas = await _unitOfWork.Contas.GetAllContaWithPagarEReceberEUsuario();

                List <ContaResource> contaResource = new List <ContaResource>();

                if (contas == null)
                {
                    return(NoContent());
                }

                foreach (var conta in contas)
                {
                    if (conta.IsAtivo)
                    {
                        contaResource.Add(ContaMapper.ModelToResource(conta));
                    }
                }

                return(Ok(contaResource));
            }
            catch (Exception exception)
            {
                LogError.LogErrorWithSentry(exception);
                return(BadRequest());
            }
        }
コード例 #3
0
        public async Task <IActionResult> Get(int id)
        {
            var conta = await _unitOfWork.Contas.GetByIdContaWithPagarEReceber(id);

            if (conta.IsAtivo)
            {
                return(Ok(ContaMapper.ModelToResource(conta)));
            }

            return(NoContent());
        }
コード例 #4
0
        public async Task <IActionResult> Edit(int id, [FromBody] ContaResource contaResource)
        {
            try
            {
                var conta = await _unitOfWork.Contas.GetByIdAsync(id);

                if (ModelState.IsValid)
                {
                    conta = ContaMapper.ResourceToModel(contaResource, conta);
                    await _unitOfWork.CompleteAsync();

                    ContaMapper.ModelToResource(conta);
                }
                return(Ok(conta));
            }
            catch (Exception e)
            {
                LogError.LogErrorWithSentry(e);
                return(BadRequest(ModelState));
            }
        }