Пример #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}"));
            }
        }
Пример #2
0
        public async Task <ActionResult> Delete([FromServices] IChurrascoRepository churrascoRepository,
                                                [FromServices] IUnitOfWork unitOfWork,
                                                int id)
        {
            try
            {
                var churrasco = await churrascoRepository.GetEntityById(id);

                if (churrasco == null)
                {
                    return(BadRequest("Churrasco não encontrado."));
                }

                churrascoRepository.Delete(churrasco);
                if (await unitOfWork.Commit())
                {
                    return(Ok("Churrasco deletado!"));
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(BadRequest($"Erro: {ex.Message}"));
            }
        }
        public async Task <ActionResult <ChurrascoUser> > Post([FromServices] IChurrascoUserRepository churrascoUserRepository,
                                                               [FromServices] IChurrascoRepository churrascoRepository,
                                                               [FromServices] IUserRepository userRepository,
                                                               [FromServices] IUnitOfWork unitOfWork,
                                                               [FromBody] ChurrascoUser model)
        {
            try
            {
                var user = await userRepository.GetEntityById(model.UserId.Value);

                var churrasco = await churrascoRepository.GetEntityById(model.ChurrascoId.Value);

                if (user == null || churrasco == null)
                {
                    return(BadRequest("Usuário ou Churrasco não encontrado."));
                }

                churrascoUserRepository.Add(model);

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