Esempio n. 1
0
        public async Task <ActionResult <ChurrascoGetDto> > Update([FromServices] IChurrascoRepository churrascoRepository,
                                                                   [FromServices] IUnitOfWork unitOfWork,
                                                                   [FromBody] ChurrascoUpdateDto model)
        {
            try
            {
                var churrasco = await churrascoRepository.GetEntityById(model.Id.Value);

                if (churrasco == null)
                {
                    return(BadRequest("Churrasco not found."));
                }

                var churrascoGetDto = await churrascoRepository.Update(model);

                if (await unitOfWork.Commit())
                {
                    return(churrascoGetDto);
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(BadRequest($"Erro: {ex.Message}"));
            }
        }
        public async Task <ChurrascoGetDto> Update(ChurrascoUpdateDto churrascoDto)
        {
            var churrasco = await _context.Churrascos.Include(chuser => chuser.Convidados).AsNoTracking().Where(c => c.Id == churrascoDto.Id).FirstOrDefaultAsync();

            churrasco.DataChurras = churrascoDto.DataChurras;
            churrasco.Motivo      = churrascoDto.Motivo;
            churrasco.Observacoes = churrascoDto.Observacoes;

            if (churrascoDto.ValorSugerido > 0)
            {
                if (churrascoDto.ValorSugerido >= 10)
                {
                    churrasco.ValorSugerido          = churrascoDto.ValorSugerido;
                    churrasco.ValorSugeridoSemBebida = churrasco.ValorSugerido - 10;
                }
                else
                {
                    churrasco.ValorSugerido          = churrascoDto.ValorSugerido;
                    churrasco.ValorSugeridoSemBebida = 0;
                }
            }

            _context.Update(churrasco);

            return(CreateChurrascoGetDtoModel(churrasco, true));
        }