コード例 #1
0
        public async Task <ActionResult> Reserve(string plate)
        {
            if (string.IsNullOrWhiteSpace(plate))
            {
                return(BadRequest());
            }

            var actorProxy = this.actorFactory.Create <IVehicleActor>(new ActorId(plate),
                                                                      new Uri(UriConstants.VehicleActorUri));

            var vehicleInfo = await actorProxy.GetInfoAsync(default(CancellationToken));

            if (vehicleInfo == null || !vehicleInfo.IsValid())
            {
                return(NotFound());
            }

            var modelForReserve = new VehicleInfoForReserveDto(vehicleInfo);

            return(View(modelForReserve));
        }
コード例 #2
0
        public async Task <ActionResult> Reserve(string plate, [FromForm] VehicleInfoForReserveDto reservation)
        {
            if (!ModelState.IsValid)
            {
                return(View(reservation));
            }

            if (reservation.StartReservation > reservation.EndReservation)
            {
                ModelState.AddModelError("", "The start reservation date must be less then the end reservation date.");
                return(View(reservation));
            }

            var vehicleProxy = this.actorFactory.Create <IVehicleActor>(new ActorId(plate),
                                                                        new Uri(UriConstants.VehicleActorUri));

            var vehicleInfo = await vehicleProxy.GetInfoAsync(default(CancellationToken));

            if (vehicleInfo == null || !vehicleInfo.IsValid())
            {
                return(NotFound());
            }

            if (vehicleInfo.State != VehicleState.Free)
            {
                ModelState.AddModelError("", "The vehicle is not available.");
                return(View(reservation));
            }


            var result = await vehicleProxy.ReserveAsync(reservation.Customer, reservation.StartReservation,
                                                         reservation.EndReservation, default(CancellationToken));

            if (result == VehicleActorError.Ok)
            {
                return(RedirectToAction(nameof(Index)));
            }

            switch (result)
            {
            case VehicleActorError.Ok:
                break;

            case VehicleActorError.ReservationDatesWrong:
                ModelState.AddModelError("", "The reservation dates are wrong");
                break;

            case VehicleActorError.VehicleBusy:
                ModelState.AddModelError("", "The vehicle cannot be reserved");
                break;

            case VehicleActorError.VehicleNotAvailable:
                ModelState.AddModelError("", "The vehicle is not available");
                break;

            case VehicleActorError.VehicleNotExists:
                ModelState.AddModelError("", "The vehicle doesn't exist");
                break;

            case VehicleActorError.GenericError:
            default:
                ModelState.AddModelError("", "An error occurs during reservation procedure");
                break;
            }
            return(View(reservation));
        }