예제 #1
0
        /// <summary>
        /// Rents a specific parkingspot for a specific amount of time for a specific person or throws eather of 2 exceptions
        /// ParkingspotNotFoundException will be thrown if there is no corresponging parkingspot to the ID.
        /// SpotAllreadyOccupiedException will be thrown if someone is allready renting the parkingspot.
        /// </summary>
        /// <param name="ParkID">The ID to the parkingspot you want to rent.</param>
        /// <param name="user">The Account that are trying to rent the parkingspot.</param>
        /// <param name="timeSpan">The amount of time the person wants to rent the parkingspot.</param>
        public void Rent(int parkID, string user, TimeSpan timeSpan)
        {
            if (dataBase.Persons.Find(user) == null)
            {
                throw new PersonNotFoundException("A person with that SSN doesnt exists!");
            }

            Parkingspot tempSpot = (from spot in dataBase.Parkingspots
                                    where spot.ParkId == parkID
                                    select spot).FirstOrDefault();

            if (tempSpot == null)
            {
                throw new ParkingspotNotFoundException("Parkingspot (" + parkID + ") does not exist in this garage.");
            }

            if (tempSpot.TimeOfRental != null)
            {
                throw new SpotAllreadyOccupiedException("This parkingspot is not avalible for renting at this moment.");
            }

            tempSpot.SSN          = user;
            tempSpot.TimeOfRental = DateTime.Now;
            tempSpot.RentalTime   = timeSpan;
            dataBase.SaveChanges();
        }
        public async Task <ActionResult <Parkingspot> > GetAvailableParkingspot(PostParkingspotRequest requestJson)
        {
            Parkinglot parkinglot;

            try
            {
                parkinglot = await _repo.GetParkinglotById(requestJson.ParkinglotId);
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database failure {e.Message}"));
            }

            if (parkinglot == null)
            {
                return(NotFound($"Parkinglot with id {requestJson.ParkinglotId} could not be found."));
            }

            Parkingspot parkingspot = parkinglot.Parkingspot.Where(x => x.Occupied == false && x.Size == requestJson.Shipsize).FirstOrDefault();

            if (parkingspot == null)
            {
                return(NotFound($"Could not find a parkingspot supporting your ships size."));
            }
            return(Ok(parkingspot));
        }
예제 #3
0
        public ActionResult EditVehicle(int id)
        {
            //get slot
            Parkingspot spot = _repo.Parkingspots.First(b => b.ParkId == id);

            return(View(spot.ParkedVehicle));
        }
        public async Task <Parkingspot> GetAvailableParkingspot(int spaceportId, int spaceshipLength)
        {
            _logger.LogInformation($"Fetching available parkingspot from the spaceport.");

            var parkingspot = await _context.Parkingspot
                              .Where(z => z.ParkedSpaceship == null && Parkingspot.SpaceshipFits(spaceshipLength) && z.SpaceportId == spaceportId)
                              .FirstOrDefaultAsync();

            return(parkingspot);
        }
예제 #5
0
        public void ParkingspotCloneTest()
        {
            Person klas  = new Person("Klas", "Anderstorp 3", "073 - 333 44 44");
            Person göran = new Person("Göran", "Anderstorp 4", "073 - 334 33 44");

            Vehicle volkswagen = new Vehicle("VWV303", klas, System.Drawing.Color.Fuchsia, VehicleType.Car, 4);
            Vehicle seat       = new Vehicle("SEA702", göran, System.Drawing.Color.Fuchsia, VehicleType.Car, 4);

            Parkingspot parkingspot1 = new Parkingspot(volkswagen, DateTime.Now, TimeSpan.FromMinutes(10));
            Parkingspot parkingspot2 = (Parkingspot)parkingspot1.Clone();

            parkingspot2.ParkedVehicle = seat;

            Assert.AreEqual("VWV303", parkingspot1.ParkedVehicle.RegNum);
            Assert.AreEqual("SEA702", parkingspot2.ParkedVehicle.RegNum);
        }
예제 #6
0
        /// <summary>
        /// Evicts someone from a specifik parkingspot or throws an exception.
        /// ParkingspotNotFoundException will be thrown if there is no corresponging parkingspot to the ID.
        /// </summary>
        /// <param name="parkID">The ID to the parkingspot you want to evict someone and thier vehicle from.</param>

        public void Eviction(int parkID)
        {
            Parkingspot tempSpot = (from spot in dataBase.Parkingspots
                                    where spot.ParkId == parkID
                                    select spot).FirstOrDefault();

            if (tempSpot == null)
            {
                throw new ParkingspotNotFoundException("Parkingspot (" + parkID + ") does not exist in this garage.");
            }
            tempSpot.RegNum       = null;
            tempSpot.SSN          = null;
            tempSpot.RentalTime   = null;
            tempSpot.TimeOfRental = null;
            dataBase.SaveChanges();
        }
예제 #7
0
        public async Task <ActionResult> CheckoutParkedSpaceship(int parkingId, int spacePortId = 500)
        {
            try
            {
                var parkingspot = new Parkingspot()
                {
                    Id              = parkingId,
                    SpaceportId     = spacePortId,
                    ParkedSpaceship = null
                };
                await _parkingspotRepository.Update <Parkingspot>(parkingspot);

                return(Ok(parkingspot));
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database Failure: {e.Message}"));
            }
        }
예제 #8
0
        /// <summary>
        /// It will remove the vehicle at the parkingspot with ParkID or throw eather of 2 exceptions.
        /// ParkingspotNotFoundException will be thrown if there is no corresponging parkingspot to the ID.
        /// VehicleNotFoundException will be thrown if there are no vehicle in the spot where you want to take the vehicle from.
        /// </summary>
        /// <param name="ParkID">The ID to the parkingspot where the vehicle you want to leave with is at.</param>
        public void Leave(int ParkID)
        {
            Parkingspot tempSpot = (from spot in dataBase.Parkingspots
                                    where spot.ParkId == ParkID
                                    select spot).FirstOrDefault();

            if (tempSpot == null)
            {
                throw new ParkingspotNotFoundException("Parkingspot (" + ParkID + ") does not exist in this garage.");
            }

            if (tempSpot.ParkedVehicle == null)
            {
                throw new VehicleNotFoundException("There is no vehicle in that parkingspot");
            }

            tempSpot.ParkedVehicle = null;
            dataBase.SaveChanges();
        }
예제 #9
0
        /// <summary>
        /// It will add your vehicle to the parkingspot with ParkID or throw eather of 2 exceptions.
        /// ParkingspotNotFoundException will be thrown if there is no corresponging parkingspot to the ID.
        /// SpotAllreadyOccupiedException will be thrown if there allready is an vehicle where you are trying to park.
        /// </summary>
        /// <param name="vehicle">The vehicle you want to park.</param>
        /// <param name="ParkID">The ID to the paringspot you want to park at.</param>
        public void Park(Vehicle vehicle, int ParkID)
        {
            Parkingspot tempSpot = (from spot in dataBase.Parkingspots
                                    where spot.ParkId == ParkID
                                    select spot).FirstOrDefault();

            if (tempSpot == null)
            {
                throw new ParkingspotNotFoundException("Parkingspot (" + ParkID + ") does not exist in this garage.");
            }

            if (tempSpot.ParkedVehicle != null)
            {
                throw new SpotAllreadyOccupiedException("There is allready a vehicle in this spot. There cannot be two.");
            }

            dataBase.Vehicles.AddOrUpdate(vehicle);
            tempSpot.ParkedVehicle = vehicle;
            dataBase.SaveChanges();
        }
예제 #10
0
        public async void UpdateParkingspotById_IfIdExist_Expect200StatusCode()
        {
            //Arange
            var parkingspotRepo = new Mock <IParkingspotRepository>();

            parkingspotRepo.Setup(x => x.GetparkingspotById(It.IsAny <int>()))
            .Returns(Task.FromResult(new Parkingspot()));
            parkingspotRepo.Setup(x => x.Save()).Returns(Task.FromResult(true));

            var parkingspotController = new ParkingspotController(parkingspotRepo.Object);
            var parkingspot           = new Parkingspot
            {
                ParkingspotId = 1,
                Occupied      = true
            };
            //Act
            var result = await parkingspotController.UpdateParkingspot(parkingspot);

            var contentResult = result.Result as OkObjectResult;

            //Assert
            Assert.Equal(200, contentResult.StatusCode);
        }
        public async Task <ActionResult <Parkingspot> > UpdateParkingspot(Parkingspot spot)
        {
            try
            {
                var parkingspot = await _repo.GetparkingspotById(spot.ParkingspotId);

                if (parkingspot == null)
                {
                    return(NotFound());
                }

                parkingspot.Occupied = spot.Occupied;
                _repo.Update(parkingspot);
                if (await _repo.Save())
                {
                    return(Ok(parkingspot));
                }
                return(BadRequest());
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database failure {e.Message}"));
            }
        }
예제 #12
0
        public void SpaceshipFits_SameSize_True()
        {
            var shipFits = Parkingspot.SpaceshipFits(Parkingspot.MaxLength);

            Assert.IsTrue(shipFits);
        }
예제 #13
0
        public void SpaceshipFits_Smaller_True()
        {
            var shipFits = Parkingspot.SpaceshipFits(1);

            Assert.IsTrue(shipFits);
        }
예제 #14
0
        public void SpaceshipFits_TooBig_False()
        {
            var shipFits = Parkingspot.SpaceshipFits(999999999);

            Assert.IsFalse(shipFits);
        }
예제 #15
0
        protected override void Seed(Garage_2._1.Models.ApplicationDbContext context)
        {
            Person p1 = new Person()
            {
                SSN = "80", Name = "Sigge", Address = "Stockholm", Phonenumber = "00002205"
            };
            Person p2 = new Person()
            {
                SSN = "8", Name = "Aps", Address = "Stockholm", Phonenumber = "00002205"
            };
            Person p3 = new Person()
            {
                SSN = "3", Name = "Emil", Address = "Stockholm", Phonenumber = "00002205"
            };


            context.Persons.AddOrUpdate(
                p => p.SSN,
                p1,
                p2,
                p3
                );


            Vehicle v1 = new Vehicle()
            {
                RegNum = "AAA", SSN = "80", PaintColor = Color.LightSalmon, Type = VehicleType.Bus, NumberOfWheels = 1
            };
            Vehicle v2 = new Vehicle()
            {
                RegNum = "BBB", SSN = "80", PaintColor = Color.LightSalmon, Type = VehicleType.Car, NumberOfWheels = 2
            };
            Vehicle v3 = new Vehicle()
            {
                RegNum = "CCC", SSN = "8", PaintColor = Color.LightSalmon, Type = VehicleType.Car, NumberOfWheels = 3
            };
            Vehicle v4 = new Vehicle()
            {
                RegNum = "DDD", SSN = "8", PaintColor = Color.LightSalmon, Type = VehicleType.Motorcycle, NumberOfWheels = 4
            };
            Vehicle v5 = new Vehicle()
            {
                RegNum = "EEE", SSN = "3", PaintColor = Color.LightSalmon, Type = VehicleType.Trailer, NumberOfWheels = 5
            };
            Vehicle v6 = new Vehicle()
            {
                RegNum = "FFF", SSN = "3", PaintColor = Color.LightSalmon, Type = VehicleType.Car, NumberOfWheels = 6
            };

            context.Vehicles.AddOrUpdate(
                v => v.RegNum,
                v1,
                v2,
                v3,
                v4,
                v5,
                v6
                );


            Parkingspot ps1 = new Parkingspot()
            {
                ParkId = 1, RegNum = "AAA", SSN = "80", TimeOfRental = DateTime.Now, RentalTime = TimeSpan.FromMinutes(10)
            };
            Parkingspot ps2 = new Parkingspot()
            {
                ParkId = 2, RegNum = "BBB", SSN = "80", TimeOfRental = DateTime.Now, RentalTime = TimeSpan.FromMinutes(10)
            };
            Parkingspot ps3 = new Parkingspot()
            {
                ParkId = 3, RegNum = "CCC", SSN = "80", TimeOfRental = DateTime.Now, RentalTime = TimeSpan.FromMinutes(10)
            };
            Parkingspot ps4 = new Parkingspot()
            {
                ParkId = 4, RegNum = "DDD", SSN = "80", TimeOfRental = DateTime.Now, RentalTime = TimeSpan.FromMinutes(10)
            };
            Parkingspot ps5 = new Parkingspot()
            {
                ParkId = 5, RegNum = "EEE", SSN = "8", TimeOfRental = DateTime.Now, RentalTime = TimeSpan.FromMinutes(10)
            };
            Parkingspot ps6 = new Parkingspot()
            {
                ParkId = 6, RegNum = "FFF", SSN = "3", TimeOfRental = DateTime.Now, RentalTime = TimeSpan.FromMinutes(10)
            };



            context.Parkingspots.AddOrUpdate(
                park => park.ParkId,
                ps1,
                ps2,
                ps3,
                ps4,
                ps5,
                ps6
                );



            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
        public async Task <IList <Parkingspot> > GetAllAvailableParkingspots(int spaceshipLength)
        {
            _logger.LogInformation($"Fetching all available parkingspots");

            var parkingspots = await _context.Parkingspot
                               .Where(parkingspot => parkingspot.ParkedSpaceship == null && Parkingspot.SpaceshipFits(spaceshipLength))
                               .ToListAsync();

            return(parkingspots);
        }