Ejemplo n.º 1
0
        public Parking Book(Parking parking, int userId)
        {
            var user   = _context.Users.Find(userId);
            var space  = _context.Spaces.Find(parking.SpaceId);
            var garage = _context.Garages.Find(parking.GarageId);

            if (user == null || space == null || garage == null)
            {
                throw new AppException("Can't Book Parking! Missing Data");
            }

            parking.UserId             = userId;
            parking.AllocationMangerId = space.AllocationManagerId;

            int occupiedCapacityInt = Int32.Parse(space.OccupiedCapacity);
            int totalCapacityInt    = Int32.Parse(space.TotalCapacity);

            if (!(occupiedCapacityInt + 1 <= totalCapacityInt))
            {
                throw new AppException("Can't Book Parking!");
            }

            parking.isBooked = true;

            var oldParking = _context.Parkings.Count(p => p.UserId == userId && p.isBooked == true);

            if (oldParking > 0)
            {
                throw new AppException("Already Booked Parking!");
            }

            if (!(parking.withCleaningService && garage.hasCleaningService))
            {
                parking.withCleaningService = false;
            }

            _context.Parkings.Add(parking);
            _context.SaveChanges();

            _garageService.PlusGarageCapacity(garage);
            _spaceService.PlusSpaceCapacity(space);

            return(parking);
        }