예제 #1
0
        private static void CheckAndHandleReservations()
        {
            List <Reservation> reservations = reservationsService.GetReservations();

            if (reservations.Count == 0)
            {
                return;
            }

            lock (reservations)
            {
                List <Reservation> reservationsForRemoving = new List <Reservation>();

                Zone         zone;
                ParkingPlace parkingPlace;

                foreach (Reservation reservation in reservations)
                {
                    if (reservation.GetEndDateTimeServer() < DateTime.Now)
                    {
                        zone = zonesService.GetZone(reservation.ParkingPlace.Zone.Id);
                        lock (zone)
                        {
                            parkingPlace = zone.GetParkingPlace(reservation.ParkingPlace.Id);
                            lock (parkingPlace)
                            {
                                parkingPlace.Status = ParkingPlaceStatus.EMPTY;
                            }

                            zone.Version++;
                            zone.AddParkingPlaceChange(parkingPlace.Id, parkingPlace.Status);
                        }
                        reservation.User.Reservation = null;
                        reservation.User.AddViolation(true);

                        reservationsForRemoving.Add(reservation);
                    }
                }

                if (reservationsForRemoving.Count > 0)
                {
                    reservations.RemoveAll(item => reservationsForRemoving.Contains(item));
                }
            }
        }
예제 #2
0
        public async Task <HttpResponseMessage> getReports()
        {
            string token = GetHeader("token");

            if (token == null || (token != null && !TokenManager.ValidateToken(token)))
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }

            User             loggedUser     = usersService.GetLoggedUser(token);
            List <ReportDTO> allUserReports = new List <ReportDTO>();

            using (var db = new ApplicationDbContext())
            {
                List <Report> listReports = db.Reports.ToList();
                //allUserReports =  db.Reports.Where(r => r.UsernameSubmitter.Equals(loggedUser.Username))
                //                                          .Select(r => new ReportDTO(r, zonesService.GetZone(r.ZoneId)
                //                                                                                    .GetParkingPlace(r.ParkingPlaceId)
                //                                                                                    .Location.Address))
                //                                           .ToList();
                foreach (Report r in listReports)
                {
                    if (r.UsernameSubmitter.Equals(loggedUser.Username))
                    {
                        string address = zonesService.GetZone(r.ZoneId).GetParkingPlace(r.ParkingPlaceId).Location.Address;
                        allUserReports.Add(new ReportDTO(r, address));
                    }
                }
            }

            if (allUserReports != null)
            {
                return(Request.CreateResponse(HttpStatusCode.OK, allUserReports));
            }

            return(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        public async Task <HttpResponseMessage> PostReservation([FromBody] ReservingDto value)
        {
            string token = GetHeader("token");

            if (token == null || (token != null && !TokenManager.ValidateToken(token)))
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }

            Zone zone = null;

            try
            {
                zone = zonesService.GetZone(value.ZoneId);
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e.Message));
            }

            ParkingPlace parkingPlace = null;

            try
            {
                parkingPlace = zone.ParkingPlaces
                               .Where(pp => pp.Id == value.ParkingPlaceId)
                               .Single();
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e.Message));
            }


            Reservation reservation;

            lock (parkingPlace)
            {
                if (parkingPlace.Status != ParkingPlaceStatus.EMPTY)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "parkingPlace.Status != ParkingPlaceStatus.EMPTY"));
                }

                double distance = Distance.computeDistance(value.CurrentLocationLatitude,
                                                           value.CurrentLocationLongitude,
                                                           parkingPlace.Location.Latitude,
                                                           parkingPlace.Location.Longitude);
                if (distance > MAX_DISTANCE_FOR_RESERVATION)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest));
                }

                parkingPlace.Status = ParkingPlaceStatus.RESERVED;
                reservation         = new Reservation(parkingPlace, usersService.GetLoggedUser(token), value.DateTimeAndroid);
                reservationsService.AddReservation(reservation);

                lock (parkingPlace.Zone)
                {
                    parkingPlace.Zone.Version++;
                    parkingPlace.Zone.AddParkingPlaceChange(parkingPlace.Id, parkingPlace.Status);
                }
            }

            return(Request.CreateResponse(HttpStatusCode.OK, new ReservationDTO(reservation)));
        }