Skip to content

Commit

Permalink
feat: Add booking function (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
janeljs committed May 31, 2021
1 parent ea47d6d commit 9fd154f
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
37 changes: 37 additions & 0 deletions BE/airbnb/src/main/java/airbnb/controller/BookingController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package airbnb.controller;

import airbnb.domain.Booking;
import airbnb.domain.Image;
import airbnb.domain.Room;
import airbnb.domain.User;
import airbnb.request.BookingRequest;
import airbnb.response.BookingResponse;
import airbnb.service.BookingService;
import airbnb.service.RoomService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class BookingController {
private final RoomService roomService;
private final BookingService bookingService;

@PostMapping("/book/rooms/{roomId}")
public ResponseEntity<BookingResponse> reserve(@PathVariable Long roomId, @RequestBody BookingRequest reservationInfo) {
User user = new User();
Room room = roomService.findRoomById(roomId);
BookingResponse bookingResponse = bookingService.reserve(user, room, reservationInfo);
return ResponseEntity.ok(bookingResponse);
}

@DeleteMapping("/cancel/rooms/{roomId}")
public ResponseEntity<String> cancel() {
return new ResponseEntity<>("예약이 성공적으로 취소되었습니다.", HttpStatus.OK);
}

}
22 changes: 22 additions & 0 deletions BE/airbnb/src/main/java/airbnb/request/BookingRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package airbnb.request;

import airbnb.domain.Guest;
import lombok.Getter;

@Getter
public class BookingRequest {
private String checkIn;
private String checkOut;
private int adults;
private int children;
private int infants;
private int totalPrice;

public static Guest createGuest(BookingRequest bookingRequest) {
return Guest.builder()
.adults(bookingRequest.adults)
.children(bookingRequest.children)
.infants(bookingRequest.infants)
.build();
}
}
21 changes: 21 additions & 0 deletions BE/airbnb/src/main/java/airbnb/response/BookingResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package airbnb.response;

import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Getter
@Builder
public class BookingResponse {
private final String name;
private final List<String> roomImages;
private final String place;
private final String host;
private final LocalDateTime checkIn;
private final LocalDateTime checkOut;
private final int numberOfGuests;
private final int totalPrice;
}
22 changes: 22 additions & 0 deletions BE/airbnb/src/main/java/airbnb/service/BookingService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package airbnb.service;

import airbnb.domain.Booking;
import airbnb.domain.Room;
import airbnb.domain.User;
import airbnb.repository.BookingRepository;
import airbnb.request.BookingRequest;
import airbnb.response.BookingResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class BookingService {
private final BookingRepository bookingRepository;

public BookingResponse reserve(User user, Room room, BookingRequest reservationInfo) {
Booking booking = Room.createBooking(user, room, reservationInfo);
bookingRepository.save(booking);
return Booking.createBookingResponse(booking);
}
}

0 comments on commit 9fd154f

Please sign in to comment.