1 | package com.example.villadihovo.service.impl.reservationImpl;
|
---|
2 |
|
---|
3 | import com.example.villadihovo.model.offers.Rooms;
|
---|
4 | import com.example.villadihovo.model.payments.OnSite;
|
---|
5 | import com.example.villadihovo.model.payments.Online;
|
---|
6 | import com.example.villadihovo.model.payments.Payment;
|
---|
7 | import com.example.villadihovo.model.reservations.GuestsMakeReservation;
|
---|
8 | import com.example.villadihovo.model.reservations.Reservation;
|
---|
9 | import com.example.villadihovo.model.users.UserTable;
|
---|
10 | import com.example.villadihovo.repository.offers.RoomRepository;
|
---|
11 | import com.example.villadihovo.repository.offers.VillaRepository;
|
---|
12 | import com.example.villadihovo.repository.payment.OnSiteRepository;
|
---|
13 | import com.example.villadihovo.repository.payment.OnlineRepository;
|
---|
14 | import com.example.villadihovo.repository.payment.PaymentRepository;
|
---|
15 | import com.example.villadihovo.repository.reservation.GuestMakeReservationRepository;
|
---|
16 | import com.example.villadihovo.repository.reservation.ReservationRepository;
|
---|
17 | import com.example.villadihovo.repository.users.GuestRepository;
|
---|
18 | import com.example.villadihovo.repository.users.UserRepository;
|
---|
19 | import com.example.villadihovo.service.reservation.ReservationService;
|
---|
20 | import lombok.AllArgsConstructor;
|
---|
21 | import org.springframework.stereotype.Service;
|
---|
22 |
|
---|
23 | import java.time.LocalDate;
|
---|
24 | import java.time.Period;
|
---|
25 | import java.util.List;
|
---|
26 | import java.util.Optional;
|
---|
27 |
|
---|
28 | @Service
|
---|
29 | @AllArgsConstructor
|
---|
30 | public class ReservationServiceImpl implements ReservationService {
|
---|
31 |
|
---|
32 | ReservationRepository reservationRepository;
|
---|
33 | RoomRepository roomRepository;
|
---|
34 | VillaRepository villaRepository;
|
---|
35 | PaymentRepository paymentRepository;
|
---|
36 | UserRepository userRepository;
|
---|
37 | OnSiteRepository onSiteRepository;
|
---|
38 | OnlineRepository onlineRepository;
|
---|
39 | GuestRepository guestRepository;
|
---|
40 | GuestMakeReservationRepository guestMakeReservationRepository;
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public List<Reservation> allReservations() {
|
---|
44 | return reservationRepository.findAll();
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public Reservation addReservation(LocalDate start_date, LocalDate end_date, Integer number_guests, Integer adults, Integer children,String payment_method, String card_number, Integer room_id, UserTable user, Integer price) {
|
---|
49 |
|
---|
50 | Reservation reservation = new Reservation();
|
---|
51 |
|
---|
52 | Optional<Rooms> roomsOptional = roomRepository.findById(room_id);
|
---|
53 | roomsOptional.ifPresent(room -> {
|
---|
54 | reservation.setVilla_id(room.getVilla_id());
|
---|
55 | reservation.setRoom_id(room);
|
---|
56 | room.setAvailability(false);
|
---|
57 | roomRepository.save(room);
|
---|
58 | });
|
---|
59 |
|
---|
60 |
|
---|
61 | Period period = Period.between(start_date, end_date);
|
---|
62 | int days = period.getDays();
|
---|
63 |
|
---|
64 | Payment payment = new Payment();
|
---|
65 | payment.setTotal_payment(days * price);
|
---|
66 | payment.setPay_date(LocalDate.now());
|
---|
67 | payment.setRec_id("12345678910");
|
---|
68 | payment.setCustomer_id(user.getUser_id() + "22");
|
---|
69 | payment.setUser_id(user);
|
---|
70 | paymentRepository.save(payment);
|
---|
71 |
|
---|
72 | if(payment_method == "creditCard")
|
---|
73 | {
|
---|
74 | Online online = new Online();
|
---|
75 | online.setPayment_id(payment.getPayment_id());
|
---|
76 | online.setCard_number(card_number);
|
---|
77 | onlineRepository.save(online);
|
---|
78 | }else if(payment_method == "onSite")
|
---|
79 | {
|
---|
80 | OnSite onSite = new OnSite();
|
---|
81 | onSite.setPayment_id(payment.getPayment_id());
|
---|
82 | onSite.setCurrency("euros");
|
---|
83 | onSite.setPayment_type("cash");
|
---|
84 | onSite.setUser_id(user);
|
---|
85 | onSiteRepository.save(onSite);
|
---|
86 | }
|
---|
87 | reservation.setPayment_id(payment);
|
---|
88 | reservation.setStart_date(start_date);
|
---|
89 | reservation.setEnd_date(end_date);
|
---|
90 | reservation.setNumber_guests(number_guests);
|
---|
91 | reservation.setAdults(adults);
|
---|
92 | reservation.setChildren(children);
|
---|
93 |
|
---|
94 | int reservation_id = reservationRepository.save(reservation).getReservation_id();
|
---|
95 |
|
---|
96 | GuestsMakeReservation guestsMakeReservation = new GuestsMakeReservation();
|
---|
97 | guestsMakeReservation.setReservation_id(reservation_id);
|
---|
98 | guestsMakeReservation.setUser_id(user.getUser_id());
|
---|
99 | guestMakeReservationRepository.save(guestsMakeReservation);
|
---|
100 |
|
---|
101 | return reservation;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | }
|
---|