Пример #1
0
        public Schedule AddFoodTruckSchedule(CreateFoodTruckScheduleCommand command)
        {
            var foodTruck = this.foodTruckRepository.GetFoodTruck(command.FoodTruckId);

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


            var location = this.locationRepository.GetLocation(command.LocationId);

            if (location == null)
            {
                throw new ObjectNotFoundException($"No location with the id {command.LocationId} found");
            }

            // Create the new schedule object and add it to the food truck
            Schedule schedule = new Schedule(foodTruck, location, command.StartTime, command.EndTime);

            foodTruck.AddSchedule(schedule);

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

            return(schedule);
        }
        public Result <Schedule> AddFoodTruckSchedule(CreateFoodTruckScheduleCommand command)
        {
            var foodTruck = _foodTruckRepository.GetFoodTruck(command.FoodTruckId);

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

            var location = _locationRepository.GetLocation(command.LocationId);

            if (location == null)
            {
                return(Result.Failure <Schedule>(new InvalidDataError($"No location with the id {command.LocationId} found")));
            }

            // Create the new schedule object and add it to the food truck
            Schedule schedule = new Schedule(foodTruck, location, command.StartTime, command.EndTime);

            foodTruck.AddSchedule(schedule);

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

            return(Result.Success <Schedule>(schedule));
        }
Пример #3
0
        public IActionResult Post(int foodTruckId, [FromBody] CreateFoodTruckScheduleModel createModel)
        {
            var createCommand = new CreateFoodTruckScheduleCommand()
            {
                FoodTruckId = foodTruckId
            };

            _mapper.Map <CreateFoodTruckScheduleModel, CreateFoodTruckScheduleCommand>(createModel, createCommand);

            Schedule schedule = _scheduleService.AddFoodTruckSchedule(createCommand);

            var model = _mapper.Map <Schedule, FoodTruckScheduleModel>(schedule);

            return(CreatedAtRoute(GET_SINGLE_FOOD_TRUCK_SCHEDULE,
                                  new { foodTruckId = model.FoodTruckId, scheduleId = model.ScheduleId }, model));
        }