public async Task <IActionResult> Put(int EventoId, EventosDtos Model) { try { var Evento = await _repo.GetEventoAsyncById(EventoId, false); if (Evento == null) { return(NotFound()); } var idLotes = new List <int>(); var idRedesSociais = new List <int>(); Model.Lotes.ForEach(item => idLotes.Add(item.id)); Model.RedesSociais.ForEach(item => idRedesSociais.Add(item.Id)); var lotes = Evento.Lotes.Where( lote => !idLotes.Contains(lote.id) ).ToArray(); var redesSociais = Evento.RedesSociais.Where( rede => !idLotes.Contains(rede.Id) ).ToArray(); if (lotes.Length > 0) { _repo.DeleteRange(lotes); } if (redesSociais.Length > 0) { _repo.DeleteRange(redesSociais); } _mapper.Map(Model, Evento); _repo.Update(Evento); if (await _repo.SaveChangesAsync()) { return(Created($"/api/evento/{Model.Id}", _mapper.Map <Evento>(Evento))); } return(BadRequest()); } catch (System.Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Banco de dados Falhou")); } return(BadRequest()); }
public async Task <IActionResult> Post(EventosDtos Model) { try { var evento = _mapper.Map <Evento>(Model); _repo.Add(evento); if (await _repo.SaveChangesAsync()) { return(Created($"/api/evento/{Model.Id}", _mapper.Map <Evento>(evento))); } } catch (System.Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Banco de dados Falhou")); } return(BadRequest()); }