public async Task <IActionResult> CreateBooking(int userId, BookingForCreationDto bookingForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); bookingForCreationDto.UserId = userId; bookingForCreationDto.Status = "Pending"; var booking = _mapper.Map <Booking>(bookingForCreationDto); userFromRepo.Bookings.Add(booking); if (await _repo.SaveAll()) { var bookingForReturn = _mapper.Map <BookingForDetailedDto>(booking); return(CreatedAtRoute("GetBooking", new { userId, id = booking.Id }, bookingForReturn)); } throw new Exception("Creating the booking failed on save"); }
public async Task <IActionResult> CreateBooking([FromBody] BookingForCreationDto booking) { if (booking == null) { return(BadRequest("BookingForCreationDto object is null")); } if (!ModelState.IsValid) { return(UnprocessableEntity(ModelState)); } var room = await _repository.Room.GetRoomAsync(booking.RoomId, true); if (room == null) { return(BadRequest()); } var reserved = CheckIfRoomIsReserved(room, booking.Start, booking.End); if (reserved) { return(Conflict("Room is already reserved for this time.")); } var bookingEntity = _mapper.Map <Booking>(booking); _repository.Booking.CreateBooking(bookingEntity); await _repository.SaveAsync(); var bookingToReturn = _mapper.Map <BookingDto>(bookingEntity); return(CreatedAtRoute("BookingById", new { id = bookingToReturn.Id }, bookingToReturn)); }
public IActionResult CreateBooking(Guid carId, [FromBody] BookingForCreationDto booking) { if (booking == null) { return(BadRequest()); } if (!_carRepository.CarExists(carId)) { return(NotFound()); } var isCarAvailable = _carRepository.CarAvailable(carId); if (!isCarAvailable) { return(BadRequest($"The car is not available.")); } var bookingEntity = _mapper.Map <Booking>(booking); _carRepository.AddBooking(carId, bookingEntity); if (!_carRepository.Save()) { throw new Exception($"Creating a booking for car {carId} failed on save."); } var bookingToReturn = _mapper.Map <BookingDto>(bookingEntity); return(CreatedAtRoute("GetBooking", new { bookingId = bookingToReturn.Id }, bookingToReturn)); }
public async Task AddBooking(BookingForCreationDto bookingForCreationDto) { try { // dodac ModelState.IsValid // podzielic klase na mniejsza if (bookingForCreationDto.DateIn > bookingForCreationDto.DateOut) { throw new Exception("Data wyjazdu nie moze byc mniejsza od daty przyjazdu! Zmień termin."); } if (bookingForCreationDto.DateIn == bookingForCreationDto.DateOut) { throw new Exception("Nie mozna zarezerwować pokoju na jeden dzien. Wybierz dłuższy termin"); } var temp = await _context.Bookings.Where(x => x.RoomsId == bookingForCreationDto.RoomsId).ToListAsync(); // sprawdzenie czy dany pokoj w podanym terminie jest wolny for (int i = 0; i < temp.Count; i++) { if ((bookingForCreationDto.DateIn >= temp[i].DateIn && bookingForCreationDto.DateOut <= temp[i].DateOut) || (bookingForCreationDto.DateIn <= temp[i].DateIn && bookingForCreationDto.DateOut >= temp[i].DateOut)) { throw new Exception("W podanym terminie nie można dokonać rezerwacji. Wybierz inną date"); } } double howLong = bookingForCreationDto.DateOut.Day - bookingForCreationDto.DateIn.Day; double priceThisRoom = await _context.Rooms.Where(x => x.Id == bookingForCreationDto.RoomsId).Select(x => x.Price).FirstOrDefaultAsync(); bookingForCreationDto.TotalPrice = howLong * priceThisRoom; // Obliczanie rzeczywistej ceny za pobyt var booking = _mapper.Map <Booking>(bookingForCreationDto); var isComment = _context.Comments.Where(id => id.UserId == bookingForCreationDto.UserId).Select(x => x.Content); var isPhoto = _context.Emails.Where(id => id.UserId == bookingForCreationDto.UserId).Select(x => x.IsPhoto); if (isComment != null && isPhoto != null) { booking.Discount = booking.TotalPrice * 0.05; // 5% całkowitej ceny booking.TotalPrice = booking.TotalPrice * 0.95; // 95% całkowitej ceny } _context.Bookings.Add(booking); } catch (Exception ex) { throw new Exception(ex.ToString()); } finally { await _context.SaveChangesAsync(); } }
public BookingDto CreateBooking(BookingForCreationDto bookingForCreationDto) { var bookingToRepo = _mapper.Map <BookingForCreationDto, Booking>(bookingForCreationDto); bookingToRepo.Id = GenerateId(); CreatePersonForBooking(bookingToRepo); _repository.Save(bookingToRepo); var bookingToReturn = _mapper.Map <Booking, BookingDto>(bookingToRepo); return(bookingToReturn); }
public async Task CreateBooking_WhenBookingIsNotSpecifiedCorrectly_ShoudReturn400StatusCode(string method) { // Arrange var requestObj = new BookingForCreationDto { }; var request = CreateRequestMessage(method, "api/flights/81/bookings", requestObj); // Act var response = await Client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }
public async Task CreateBooking_WhenBookingSpecifiedCorrectly_ShoudReturn201StatusCode(string method) { // Arrange var requestObj = new BookingForCreationDto { Number = "WO-789654", Customer = new PersonForCreationDto { Name = "Test Customer", Address = "Test address", DateBirth = new DateTime(1993, 07, 27), Email = "*****@*****.**", Gender = GenderType.Male }, Passengers = new List <PersonForCreationDto> { new PersonForCreationDto { Name = "Test Customer2", Address = "Test address2", DateBirth = new DateTime(1994, 04, 24), Email = "*****@*****.**", Gender = GenderType.Male }, new PersonForCreationDto { Name = "Test Customer3", Address = "Test address3", DateBirth = new DateTime(1995, 05, 25), Email = "*****@*****.**", Gender = GenderType.Female } } }; var request = CreateRequestMessage(method, "api/flights/81/bookings", requestObj); // Act var response = await Client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.Created, response.StatusCode); }
public async Task <IActionResult> CreateBooking(BookingForCreationDto bookingForCreationDto) { var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var bookingToCreate = _mapper.Map <Booking>(bookingForCreationDto); _repo.Add(bookingToCreate); if (await _repo.SaveAll()) { var bookingToReturn = _mapper.Map <BookingToReturnDto>(bookingToCreate); return(CreatedAtRoute("GetBooking", new { id = bookingToCreate.Id }, bookingToReturn)); } throw new Exception("Creating the booking failed on save"); }
public IActionResult CreateBooking(int flightId, BookingForCreationDto bookingForCreationDto) { if (bookingForCreationDto == null) { return(BadRequest()); } var flight = _flightService.Get(flightId); if (flight == null) { return(NotFound()); } bookingForCreationDto.Flight = flight; var createdBooking = _bookingService.CreateBooking(bookingForCreationDto); return(CreatedAtRoute("GetBooking", new { flightId = createdBooking.Flight.Id, bookingId = createdBooking.Id }, createdBooking)); }
public ActionResult <BookingDto> CreateBookingForTimeSlot(Guid calendarId, Guid timeSlotId, BookingForCreationDto booking) { if (!_vejledningsbookingRepository.StudentExists(booking.StudentId)) { return(NotFound($"Booking invalid: Noknown Student.")); } if (!_vejledningsbookingRepository.TimeSlotExists(timeSlotId)) { return(NotFound()); } var bookingEntity = _mapper.Map <Entities.Booking>(booking); if (_vejledningsbookingRepository.BookingOverLap(bookingEntity)) { return(BadRequest(new ValidationResult($"Booking within existing booking, overlap with stored booking, either student own booking or overlap with other bookings in timeslot.", new[] { "Booking" }))); } if (_vejledningsbookingRepository.CountStudentBookings(booking.StudentId) >= 2) { return(BadRequest(new ValidationResult($"The maximum booking limit reached.", new[] { "booking.StudentId" }))); } _vejledningsbookingRepository.AddBooking(timeSlotId, bookingEntity); _vejledningsbookingRepository.Save(); var calendarToReturn = _mapper.Map <BookingDto>(bookingEntity); return(CreatedAtRoute("GetCalendar", new { calendarId = calendarId }, calendarToReturn)); }
public async Task <IActionResult> AddBooking(BookingForCreationDto bookingForCreationDto) { await _bookingRepository.AddBooking(bookingForCreationDto); return(Ok()); }