コード例 #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> 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));
            }
        }