1 | package com.tourMate.dao.impl;
|
---|
2 |
|
---|
3 | import com.tourMate.dao.HotelDao;
|
---|
4 | import com.tourMate.entities.*;
|
---|
5 | import jakarta.persistence.EntityManager;
|
---|
6 | import jakarta.persistence.PersistenceContext;
|
---|
7 | import jakarta.transaction.Transactional;
|
---|
8 | import org.springframework.stereotype.Service;
|
---|
9 |
|
---|
10 | import java.util.Date;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public class HotelDaoImpl implements HotelDao {
|
---|
15 |
|
---|
16 | @PersistenceContext
|
---|
17 | EntityManager em;
|
---|
18 |
|
---|
19 | @Override
|
---|
20 | @Transactional
|
---|
21 | public void createHotel(Hotels hotel, long userId) {
|
---|
22 | User u = em.find(User.class, userId);
|
---|
23 | Hotels h = new Hotels(hotel.getHotelName(), hotel.getHotelDescripiton(), hotel.getHotelLocation(), hotel.getHotelEDBS(), hotel.getParking(), hotel.getPetFriendly(), hotel.getInternetAvailable(), u);
|
---|
24 | em.persist(h);
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public List<Hotels> getHotels() {
|
---|
29 | List<Hotels> hoteli = em.createQuery("select h from Hotels h order by h.hotelId").getResultList();
|
---|
30 | System.out.println("OREEEEEEL");
|
---|
31 | return hoteli;
|
---|
32 | //return em.createQuery("select h from Hotels h order by h.hotelId").getResultList();
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public List<Hotels> getHotelsForUser(long userId) {
|
---|
37 | User u = em.find(User.class, userId);
|
---|
38 | return em.createQuery("select h from Hotels h where h.owner = :u").setParameter("u", u).getResultList();
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public List<Hotels> getHotelsByLocation(String hotelLocation) {
|
---|
43 | return em.createQuery("select h from Hotels h where h.hotelLocation = hotelLocation").getResultList();
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Transactional
|
---|
47 | @Override
|
---|
48 | public void editHotel(long hotelId, String hotelName, String hotelDescripiton, String hotelLocation, String hotelEDBS, Boolean parking, Boolean petFriendly, Boolean internetAvailable) {
|
---|
49 | Hotels hotel = findHotelByID(hotelId);
|
---|
50 | hotel.setHotelName(hotelName);
|
---|
51 | hotel.setHotelDescripiton(hotelDescripiton);
|
---|
52 | hotel.setHotelLocation(hotelLocation);
|
---|
53 | hotel.setHotelEDBS(hotelEDBS);
|
---|
54 | hotel.setParking(parking);
|
---|
55 | hotel.setPetFriendly(petFriendly);
|
---|
56 | hotel.setInternetAvailable(internetAvailable);
|
---|
57 | em.persist(hotel);
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Transactional
|
---|
61 | @Override
|
---|
62 | public void deleteHotel(long hotelId) {
|
---|
63 | Hotels h = findHotelByID(hotelId);
|
---|
64 | em.remove(h);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public Hotels findHotelByID(long hotelId) {
|
---|
69 | return em.find(Hotels.class, hotelId);
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public List<HotelsImages> getHotelImages(Hotels hotel) {
|
---|
74 | return em.createQuery("select hi from HotelsImages hi where hi.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | @Transactional
|
---|
79 | public void addHotelImage(Hotels hotel, String url)
|
---|
80 | {
|
---|
81 | HotelsImages hotelsImages = new HotelsImages(hotel, url);
|
---|
82 | em.persist(hotelsImages);
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | @Transactional
|
---|
87 | public HotelsImages findHotelImageById(long hotelImageId){
|
---|
88 | return em.find(HotelsImages.class, hotelImageId);
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | @Transactional
|
---|
93 | public void deleteHotelImage(long hotelImageId){
|
---|
94 | HotelsImages hotelsImages = findHotelImageById(hotelImageId);
|
---|
95 | em.remove(hotelsImages);
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public List<HotelRoom> getRoomsOfHotel(long hotelId) {
|
---|
100 | Hotels h = findHotelByID(hotelId);
|
---|
101 | return em.createQuery("SELECT hr from HotelRoom hr where hr.hotel = :hotel").setParameter("hotel", h).getResultList();
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public List<HotelRoomAvailable> getRoomsAvailable(Long id) {
|
---|
106 | return em.createQuery("SELECT hra from HotelRoomAvailable hra WHERE hra.hotelRoom.id = :hotelRoomId").setParameter("hotelRoomId", id).getResultList();
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public HotelRoom findRoomById(long hotelRoomId) {
|
---|
111 | return em.find(HotelRoom.class, hotelRoomId);
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public List<HotelRoomImages> getRoomImages(HotelRoom hotelRoom) {
|
---|
116 | return em.createQuery("select i.url from HotelRoomImages i where i.room = :hotelRoom").setParameter("hotelRoom", hotelRoom).getResultList();
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Transactional
|
---|
120 | @Override
|
---|
121 | public void createRoom(Hotels hotel, String hotelRoomDescription, String hotelRoomName, Boolean kitchenAvailable, Boolean airConditioning, Boolean balcony, double price) {
|
---|
122 | HotelRoom hotelRoom = new HotelRoom(hotel, hotelRoomDescription, hotelRoomName, kitchenAvailable, airConditioning, balcony, price);
|
---|
123 | em.persist(hotelRoom);
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Transactional
|
---|
127 | @Override
|
---|
128 | public void editRoom(long hotelRoomId, Hotels hotel, String hotelRoomDescription, String hotelRoomName, Boolean kitchenAvailable, Boolean airConditioning, Boolean balcony, double price) {
|
---|
129 | HotelRoom hr = findRoomById(hotelRoomId);
|
---|
130 | hr.setHotel(hotel);
|
---|
131 | hr.setHotelRoomDescription(hotelRoomDescription);
|
---|
132 | hr.setHotelRoomName(hotelRoomName);
|
---|
133 | hr.setKitchenAvailable(kitchenAvailable);
|
---|
134 | hr.setAirConditioning(airConditioning);
|
---|
135 | hr.setBalcony(balcony);
|
---|
136 | hr.setPrice(price);
|
---|
137 | em.persist(hr);
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Transactional
|
---|
141 | @Override
|
---|
142 | public void deleteRoom(long hotelRoomId) {
|
---|
143 | HotelRoom hr = findRoomById(hotelRoomId);
|
---|
144 | em.remove(hr);
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Transactional
|
---|
148 | @Override
|
---|
149 | public void createRoomAvailible(HotelRoom hotelRoom, Date dateFrom, Date dateTo, int numberOfBeds) {
|
---|
150 | HotelRoomAvailable hra = new HotelRoomAvailable(hotelRoom, dateFrom, dateTo, numberOfBeds);
|
---|
151 | em.persist(hra);
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Transactional
|
---|
155 | @Override
|
---|
156 | public void editRoomAvailible(long hotelRoomAvailableId, HotelRoom hotelRoom, Date dateFrom, Date dateTo, int numberOfBeds) {
|
---|
157 | HotelRoomAvailable hr = findAvailibleRoomById(hotelRoomAvailableId);
|
---|
158 | hr.setHotelRoom(hotelRoom);
|
---|
159 | hr.setDateFrom(dateFrom);
|
---|
160 | hr.setDateTo(dateTo);
|
---|
161 | hr.setNumberOfBeds(numberOfBeds);
|
---|
162 | em.persist(hr);
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Transactional
|
---|
166 | @Override
|
---|
167 | public void deleteRoomAvailible(long hotelRoomAvailableId) {
|
---|
168 | HotelRoomAvailable hra = findAvailibleRoomById(hotelRoomAvailableId);
|
---|
169 | em.remove(hra);
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Override
|
---|
173 | public HotelRoomAvailable findAvailibleRoomById(long hotelRoomAvailableId) {
|
---|
174 | return em.find(HotelRoomAvailable.class, hotelRoomAvailableId);
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Override
|
---|
178 | public List<HotelRoomAvailable> getRoomsAvailibility() {
|
---|
179 | return em.createQuery("select hra from HotelRoomAvailable hra").getResultList();
|
---|
180 | }
|
---|
181 |
|
---|
182 | @Override
|
---|
183 | public List<HotelRoomAvailable> getRoomsAvailibilityByHotel(Hotels hotel) {
|
---|
184 | return em.createQuery("select hr from HotelRoomAvailable hr where hr.hotelRoom.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | public List<HotelRoomAvailable> getRoomsAvailibilityByDateAndLocation(String hotelLocation, Date dateFrom, Date dateTo, int numberOfBeds) {
|
---|
189 | return em.createQuery("select hr from HotelRoomAvailable hr where hr.dateFrom <= :dateFrom and hr.dateTo >= :dateTo " +
|
---|
190 | "and hr.hotelRoom.hotel.hotelLocation LIKE :hotelLocation and hr.numberOfBeds >= :numBeds")
|
---|
191 | .setParameter("hotelLocation", hotelLocation)
|
---|
192 | .setParameter("dateFrom", dateFrom)
|
---|
193 | .setParameter("dateTo", dateTo)
|
---|
194 | .setParameter("numBeds", numberOfBeds)
|
---|
195 | .getResultList();
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | @Transactional
|
---|
200 | public void createReservation(User user, HotelRoom hotelRoom, Date dateFrom, Date dateTo, Integer numberOfBeds) {
|
---|
201 | HotelRoomReservations r = new HotelRoomReservations(user, hotelRoom, dateFrom, dateTo, numberOfBeds);
|
---|
202 | em.persist(r);
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | @Transactional
|
---|
207 | public void editReservation(long hotelRoomReservedId, User user, HotelRoom hotelRoom, Date dateFrom, Date dateTo, Integer numberOfBeds) {
|
---|
208 | HotelRoomReservations hr = findReservationById(hotelRoomReservedId);
|
---|
209 | hr.setUser(user);
|
---|
210 | hr.setHotelRoom(hotelRoom);
|
---|
211 | hr.setDateFrom(dateFrom);
|
---|
212 | hr.setDateTo(dateTo);
|
---|
213 | hr.setNumberOfBeds(numberOfBeds);
|
---|
214 | em.persist(hr);
|
---|
215 |
|
---|
216 | }
|
---|
217 |
|
---|
218 | @Transactional
|
---|
219 | @Override
|
---|
220 | public void deleteReservation(long hotelRoomReservedId) {
|
---|
221 | HotelRoomReservations r = findReservationById(hotelRoomReservedId);
|
---|
222 | em.remove(hotelRoomReservedId);
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public HotelRoomReservations findReservationById(long hotelRoomReservedId) {
|
---|
227 | return em.find(HotelRoomReservations.class, hotelRoomReservedId);
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | public List<HotelRoomReservations> findReservationByUser(User user) {
|
---|
232 | return em.createQuery("select hr from HotelRoomReservations hr where hr.user = :user").setParameter("user", user).getResultList();
|
---|
233 | }
|
---|
234 |
|
---|
235 | @Override
|
---|
236 | public List<HotelRoomReservations> findReservationByHotel(Hotels hotel) {
|
---|
237 | List<HotelRoom> hotelRooms = getRoomsOfHotel(hotel.getHotelId());
|
---|
238 | return em.createQuery("select hr from HotelRoomReservations hr where hr.hotelRoom in :hotelRooms").setParameter("hotelRooms", hotelRooms).getResultList();
|
---|
239 | }
|
---|
240 |
|
---|
241 | @Override
|
---|
242 | public List<HotelRoomReservations> getReservations() {
|
---|
243 | return em.createQuery("select hr from HotelRoomReservations hr order by hr.user.name").getResultList();
|
---|
244 | }
|
---|
245 | } |
---|