source: src/main/java/com/tourMate/dao/impl/HotelDaoImpl.java@ e6c2521

Last change on this file since e6c2521 was e6c2521, checked in by darsov2 <62809499+darsov2@…>, 13 months ago

images upload/download impl, other fixes

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