예제 #1
0
        public async Task <IActionResult> ReserveBike([FromRoute] int bikeId, [FromBody] BikeReservationRequest bikeReservationRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var idClaim = User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

            if (idClaim == null)
            {
                return(BadRequest());
            }

            var newReservation = new BikeReservation
            {
                BikeId     = bikeId,
                ReservedBy = idClaim.Value,
                StartTime  = bikeReservationRequest.StartTime,
                EndTime    = bikeReservationRequest.EndTime,
                IsActive   = true
            };

            _context.Add(newReservation);
            await _context.SaveChangesAsync();

            return(CreatedAtRoute("ReservationDetails", newReservation.Id, newReservation));
        }
예제 #2
0
 public async Task PublishNewBikeReservationMessageAsync(BikeReservation bikeReservation)
 {
     var bike = new BikeReservationIntegrationMessage
     {
         Id         = Guid.NewGuid().ToString(),
         BikeId     = bikeReservation.BikeId,
         CustomerId = bikeReservation.CustomerId,
         RentFrom   = bikeReservation.RentFrom,
         RentTo     = bikeReservation.RentTo
     };
     var serializedMessage     = JsonSerializer.Serialize(bike);
     ServiceBusMessage message = new ServiceBusMessage(serializedMessage);
     await _serviceBusSender.SendMessageAsync(message);
 }
        public async Task <OperationResponse <BikeReservation> > MakeReservationAsync(BikeReservation bikeReservation, string name, string phoneNumber)
        {
            var carFromReservation = await _dataRepository.GetAsync(bikeReservation.BikeId, bikeReservation.BikeId);

            if (carFromReservation == null)
            {
                return(new OperationResponse <BikeReservation>()
                       .SetAsFailureResponse(OperationErrorDictionary.BikeReservation.BikeDoesNotExist()));
            }

            var existingCarReservation = await _bikeReservationRepository.GetExistingReservationByBikeIdAsync(bikeReservation.BikeId, bikeReservation.RentFrom);

            if (existingCarReservation != null)
            {
                return(new OperationResponse <BikeReservation>()
                       .SetAsFailureResponse(OperationErrorDictionary.BikeReservation.BikeAlreadyReserved()));
            }

            else
            {
                bikeReservation.Id           = Guid.NewGuid().ToString();
                bikeReservation.CustomerId   = phoneNumber;
                bikeReservation.CustomerName = name;
                var createdCarReservation = await _bikeReservationRepository.AddAsync(bikeReservation);

                return(new OperationResponse <BikeReservation>(createdCarReservation));
            }
        }