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}")); } }
private ChurrascoUserGetUserDto CreateChurrascoUserGetUserDtoModel(ChurrascoUser churrascoUser) { var churrascoUserGetUserDto = new ChurrascoUserGetUserDto { User = new UserGetDto { Id = churrascoUser.User.Id, Email = churrascoUser.User.Email, Nome = churrascoUser.User.Nome }, BebidaInclusa = churrascoUser.BebidaInclusa, Pago = churrascoUser.Pago, ValorContribuicao = churrascoUser.ValorContribuicao }; return(churrascoUserGetUserDto); }
public void Add(ChurrascoUser churrascoUser) { var churrasco = _context.Churrascos.AsNoTracking() .Where(c => c.Id == churrascoUser.ChurrascoId) .FirstOrDefault(); if (churrascoUser.ValorContribuicao == 0) { if (churrascoUser.BebidaInclusa) { churrascoUser.ValorContribuicao = churrasco.ValorSugerido; } else { churrascoUser.ValorContribuicao = churrasco.ValorSugeridoSemBebida; } } _context.Add(churrascoUser); }
public void Delete(ChurrascoUser churrascoUser) { _context.Remove(churrascoUser); }