Esempio n. 1
0
 public IActionResult AddReservation(ReservationCreationDto reservation)
 {
     try
     {
         Reservation res = _mapper.Map <Reservation>(reservation);
         res = _reservationService.AddReservation(res);
         return(CreatedAtRoute("GetReservation", new { id = res.Id }, res));
     }
     catch (Exception ex)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
     }
 }
        public IActionResult Post([FromRoute] int restaurantId, [FromBody] ReservationRequestModel reservationRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (!int.TryParse(userIdClaim, out var userId))
            {
                throw new InvalidClaimException();
            }

            reservationRequest.UserId       = userId;
            reservationRequest.RestaurantId = restaurantId;

            var reservation = _reservationsService.AddReservation(reservationRequest);

            return(Ok(reservation));
        }
        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)));
        }