public async Task <IActionResult> Put(int MyEventId, MyEventDTO model) { try { var myEvent = await _repository.GetEventById(MyEventId, false); if (myEvent == null) { return(NotFound()); } var idLots = new List <int>(); var idSocial = new List <int>(); model.Lots.ForEach(item => idLots.Add(item.Id)); model.SocialNetworks.ForEach(item => idSocial.Add(item.Id)); var lots = myEvent.Lots.Where(lot => !idLots.Contains(lot.Id)).ToArray(); var socialNetworks = myEvent.SocialNetworks.Where(net => !idLots.Contains(net.Id)).ToArray(); if (lots.Length > 0) { _repository.DeleteRange(lots); } if (socialNetworks.Length > 0) { _repository.DeleteRange(socialNetworks); } _mapper.Map(model, myEvent); _repository.Update(myEvent); if (await _repository.SaveChangesAsync()) { return(Created($"/api/myEvent/{model.Id}", _mapper.Map <MyEventDTO>(myEvent))); } } catch (System.Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Error occurred in database")); } return(BadRequest()); }
public async Task <IActionResult> Post(MyEventDTO model) { try { var myEvent = _mapper.Map <MyEvent>(model); _repository.Add(myEvent); if (await _repository.SaveChangesAsync()) { return(Created($"/api/myEvent/{model.Id}", _mapper.Map <MyEventDTO>(myEvent))); } } catch (System.Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "[ERROR_DB] Error occurred in database")); } return(BadRequest()); }