public async Task<IHttpActionResult> Update(EventUpdateDTO model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var result = await _eventService.UpdateEvent(model); if (result.Success) { return Ok(); } return BadRequest(result.Error); }
public async Task<ValidationResult> UpdateEvent(EventUpdateDTO model) { try { //Parse guid(s) Guid? eId = ParseGuid(model.Id); if (eId == null) { Result.Error = "Failed to parse provided Id"; return Result; } //Guid? calId = ParseGuid(model.CalendarId); //if (calId == null) //{ // Result.Error = "Failed to parse provided CalendarId"; // return Result; //} //Load the event from the db Event evnt = await _eventRepo.GetByIdAsync(eId.Value); if (evnt == null) { Result.Error = "404 : Event not found"; return Result; } //Update properties evnt.Title = model.Title; evnt.Body = model.Body; evnt.Location = model.Location; evnt.AllDay = model.AllDay; evnt.StartDateTime = ParseUKDate(model.StartDateTime); evnt.EndDateTime = ParseUKDate(model.EndDateTime); evnt.ColourId = model.ColourId; //If all day event override start/end times if (evnt.AllDay) { evnt.StartDateTime = evnt.StartDateTime.Date; //Trims the time off the date evnt.EndDateTime = evnt.EndDateTime.Date.AddDays(1);//Rounds to the start of the next day } _eventRepo.Update(evnt); await SaveChangesAsync(); Result.Success = true; return Result; } catch (Exception ex) { Result.Error = ex.Message; if (ex.InnerException != null) Result.Error += "\nInner exception: " + ex.InnerException; return Result; } }