public Result <FoodTruck> UpdateFoodTruck(UpdateFoodTruckCommand foodTruckInfo)
        {
            try
            {
                // Creates our Food Truck object
                var foodTruck = _foodTruckRepository.GetFoodTruck(foodTruckInfo.FoodTruckId);

                if (foodTruck == null)
                {
                    return(Result.Failure <FoodTruck>(new ObjectNotFoundError($"No food truck found with the id of {foodTruckInfo.FoodTruckId}")));
                }

                // Handle Properties
                foodTruck.Name             = foodTruckInfo.Name;
                foodTruck.Description      = foodTruckInfo.Description;
                foodTruck.Website          = foodTruckInfo.Website;
                foodTruck.LastModifiedDate = foodTruckInfo.LastModifiedDate;

                // Persist the changes to the database
                _foodTruckRepository.Save(foodTruck);
                UnitOfWork.SaveChanges();

                return(Result.Success <FoodTruck>(foodTruck));
            }
            catch (DBConcurrencyException ce)
            {
                // If there is a database conflict, then data access layer (like EF) will throw a DbConcurrencyException, so we catch it and turn
                // it into an error to be passed up the stack with the existing object
                var foodTruck = _foodTruckRepository.GetFoodTruck(foodTruckInfo.FoodTruckId);
                return(Result.Failure <FoodTruck>(
                           new ConcurrencyError <FoodTruck>($"The food truck could not be updated due to a concurrency exception.  This is most likely because the object has changed since the object was retrieved.  Compare your changes to the current state of the object (included) and resubmit as neccessary",
                                                            foodTruck)));
            }
        }
示例#2
0
        public FoodTruck UpdateFoodTruck(UpdateFoodTruckCommand foodTruckInfo)
        {
            try
            {
                // Creates our Food Truck object
                var foodTruck = this.foodTruckRepository.GetFoodTruck(foodTruckInfo.FoodTruckId);

                if (foodTruck == null)
                {
                    throw new ObjectNotFoundException($"No food truck was found with the id {foodTruckInfo.FoodTruckId}");
                }

                // Handle Properties
                foodTruck.Name        = foodTruckInfo.Name;
                foodTruck.Description = foodTruckInfo.Description;
                foodTruck.Website     = foodTruckInfo.Website;

                // Persist the changes to the database
                this.foodTruckRepository.Save(foodTruck);
                this.UnitOfWork.SaveChanges();

                return(foodTruck);
            }
            catch (Exception ex)
            {
                Logger.LogError(new EventId(104), ex, $"Error thrown while calling FoodTruckService.UpdateFoodTruck()");
                throw;
            }
        }
        public IActionResult Put(int id, [FromBody]UpdateFoodTruckModel updateModel)
        {
            var updateCommand = new UpdateFoodTruckCommand() { FoodTruckId = id };
            this.mapper.Map<UpdateFoodTruckModel, UpdateFoodTruckCommand>(updateModel, updateCommand);

            FoodTruck foodTruck = this.foodTruckService.UpdateFoodTruck(updateCommand);

            var model = this.mapper.Map<FoodTruck, FoodTruckModel>(foodTruck);
            return this.Ok(model);
        }
        public ActionResult <FoodTruckModelV11> PutV11(int id, [FromBody] UpdateFoodTruckModel updateModel)
        {
            var updateCommand = new UpdateFoodTruckCommand()
            {
                FoodTruckId = id
            };

            _mapper.Map <UpdateFoodTruckModel, UpdateFoodTruckCommand>(updateModel, updateCommand);

            var result = _foodTruckService.UpdateFoodTruck(updateCommand);

            return(CreateResponse <FoodTruck, FoodTruckModelV11>(result));
        }
        public FoodTruck UpdateFoodTruck(UpdateFoodTruckCommand foodTruckInfo)
        {
            try
            {
                // Creates our Food Truck object
                var foodTruck = _foodTruckRepository.GetFoodTruck(foodTruckInfo.FoodTruckId);

                if (foodTruck == null)
                {
                    throw new ObjectNotFoundException($"No food truck was found with the id {foodTruckInfo.FoodTruckId}");
                }

                // Handle Properties
                foodTruck.Name             = foodTruckInfo.Name;
                foodTruck.Description      = foodTruckInfo.Description;
                foodTruck.Website          = foodTruckInfo.Website;
                foodTruck.LastModifiedDate = foodTruckInfo.LastModifiedDate;

                // Persist the changes to the database
                _foodTruckRepository.Save(foodTruck);
                UnitOfWork.SaveChanges();

                return(foodTruck);
            }
            catch (DBConcurrencyException ce)
            {
                // This is the object in the database - so the object our update conflicted with
                var foodTruck = _foodTruckRepository.GetFoodTruck(foodTruckInfo.FoodTruckId);

                Logger.LogWarning(new EventId(125), ce, $"Concurrency exception thrown while calling FoodTruckService.UpdateFoodTruck() - Update Command = || {foodTruckInfo.ToJson()} || Database Object = || {foodTruck.ToJson() } ||");

                // Get the current state of the object so we can return it with the ConcurrencyException
                throw new ConcurrencyException <FoodTruck>($"The food truck could not be updated due to a concurrency exception.  This is most likely because the object has changed since the object was retrieved.  Compare your changes to the current state of the object (included) and resubmit as neccessary",
                                                           foodTruck);
            }
            catch (Exception ex)
            {
                Logger.LogError(new EventId(104), ex, $"Error thrown while calling FoodTruckService.UpdateFoodTruck()");
                throw;
            }
        }
        public IActionResult PutV11(int id, [FromBody] UpdateFoodTruckModel updateModel)
        {
            var updateCommand = new UpdateFoodTruckCommand()
            {
                FoodTruckId = id
            };

            this.mapper.Map <UpdateFoodTruckModel, UpdateFoodTruckCommand>(updateModel, updateCommand);

            try
            {
                FoodTruck foodTruck = this.foodTruckService.UpdateFoodTruck(updateCommand);
                var       model     = this.mapper.Map <FoodTruck, FoodTruckModel>(foodTruck);
                return(this.Ok(model));
            }
            catch (ConcurrencyException <FoodTruck> ce)
            {
                String logMessage = $"Unable to update food truck {id} due to concurrency exception";
                return(this.CreateConcurrencyConflictErrorResult <FoodTruckModelV11, FoodTruck>(ce));
            }
        }