source: src/main/java/com/example/villadihovo/service/impl/reservationImpl/ReservationServiceImpl.java

Last change on this file was f7c05a1, checked in by Elena Shulevska <elena.shulevska@…>, 16 months ago

initial commit of the source code on origin

  • Property mode set to 100644
File size: 4.1 KB
Line 
1package com.example.villadihovo.service.impl.reservationImpl;
2
3import com.example.villadihovo.model.offers.Rooms;
4import com.example.villadihovo.model.payments.OnSite;
5import com.example.villadihovo.model.payments.Online;
6import com.example.villadihovo.model.payments.Payment;
7import com.example.villadihovo.model.reservations.GuestsMakeReservation;
8import com.example.villadihovo.model.reservations.Reservation;
9import com.example.villadihovo.model.users.UserTable;
10import com.example.villadihovo.repository.offers.RoomRepository;
11import com.example.villadihovo.repository.offers.VillaRepository;
12import com.example.villadihovo.repository.payment.OnSiteRepository;
13import com.example.villadihovo.repository.payment.OnlineRepository;
14import com.example.villadihovo.repository.payment.PaymentRepository;
15import com.example.villadihovo.repository.reservation.GuestMakeReservationRepository;
16import com.example.villadihovo.repository.reservation.ReservationRepository;
17import com.example.villadihovo.repository.users.GuestRepository;
18import com.example.villadihovo.repository.users.UserRepository;
19import com.example.villadihovo.service.reservation.ReservationService;
20import lombok.AllArgsConstructor;
21import org.springframework.stereotype.Service;
22
23import java.time.LocalDate;
24import java.time.Period;
25import java.util.List;
26import java.util.Optional;
27
28@Service
29@AllArgsConstructor
30public 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}
Note: See TracBrowser for help on using the repository browser.