Beispiel #1
0
        public async Task <ChurrascoUserGetUserDto> Update(ChurrascoUserUpdateDto churrascoUserDto)
        {
            var churrascoUser = await _context.ChurrascoUsers.Include(chuser => chuser.Churrasco)
                                .Include(chuser => chuser.User)
                                .AsNoTracking()
                                .Where(chuser => chuser.ChurrascoId == churrascoUserDto.ChurrascoId && chuser.UserId == churrascoUserDto.UserId)
                                .FirstOrDefaultAsync();

            if (churrascoUser == null)
            {
                return(null);
            }

            churrascoUser.BebidaInclusa = churrascoUserDto.BebidaInclusa;
            churrascoUser.Pago          = churrascoUserDto.Pago;

            if (churrascoUserDto.ValorContribuicao > 0)
            {
                if (churrascoUserDto.ValorContribuicao < churrascoUser.Churrasco.ValorSugeridoSemBebida)
                {
                    churrascoUser.ValorContribuicao = churrascoUserDto.ValorContribuicao;
                    churrascoUser.BebidaInclusa     = false;
                }
                else
                {
                    churrascoUser.ValorContribuicao = churrascoUserDto.ValorContribuicao;
                }
            }

            _context.Update(churrascoUser);

            return(CreateChurrascoUserGetUserDtoModel(churrascoUser));
        }
        public async Task <ActionResult <ChurrascoUserGetUserDto> > Update([FromServices] IChurrascoUserRepository churrascoUserRepository,
                                                                           [FromServices] IChurrascoRepository churrascoRepository,
                                                                           [FromServices] IUnitOfWork unitOfWork,
                                                                           [FromBody] ChurrascoUserUpdateDto model)
        {
            try
            {
                var churrascoUserDto = await churrascoUserRepository.Update(model);

                if (churrascoUserDto == null)
                {
                    return(BadRequest("Relação de churrasco com usuário não encontrada."));
                }

                if (await unitOfWork.Commit())
                {
                    return(churrascoUserDto);
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(BadRequest($"Erro: {ex.Message}"));
            }
        }