Esempio n. 1
0
        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()));
        }