public IHttpActionResult Put(Guid id, [FromBody] CreateSpot spot) { if (id == default(Guid) || !ModelState.IsValid) { return(BadRequest(ModelState)); } var existingSpot = _repository.Find(id); if (existingSpot is null) { return(this.Error().ResourceNotFound("Spot not found")); } try { spot.Validate(); } catch (BreakTypeException ex) { ModelState.AddModelError( nameof(CreateSpot.BreakType), BreakTypeValueErrorMessage(ex) ); return(BadRequest(ModelState)); } existingSpot.Update(spot); _repository.Update(existingSpot); _repository.SaveChanges(); return(Ok(existingSpot)); }
public IHttpActionResult Put(string externalId, [FromBody] CreateSpot newSpot) { if (newSpot != null && (string.IsNullOrWhiteSpace(externalId) || externalId != newSpot.ExternalSpotRef)) { ModelState.AddModelError(nameof(Spot.ExternalSpotRef), "External Id does not match"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { newSpot.Validate(); } catch (BreakTypeException ex) { ModelState.AddModelError( nameof(CreateSpot.BreakType), BreakTypeValueErrorMessage(ex) ); return(BadRequest(ModelState)); } var existingSpot = _repository.FindByExternalSpotRef(externalId); var reviseResult = ReviseAndModifySpotChanges(newSpot, existingSpot); if (!reviseResult.Success) { ModelState.AddModelError(reviseResult.Error.ErrorField, reviseResult.Error.ErrorMessage); return(BadRequest(ModelState)); } if (existingSpot is null) { existingSpot = CreateSpots(new List <CreateSpot> { newSpot }).First(); } else { existingSpot.Update(newSpot); _repository.Update(existingSpot); } // Do not remove this, need to persist changes now so that we can // return model _repository.SaveChanges(); return(Ok(_spotModelCreator.Create(new[] { existingSpot }).FirstOrDefault())); }