diff --git a/angular/RestClient/environments/environment.ts b/angular/RestClient/environments/environment.ts new file mode 100644 index 0000000000000000000000000000000000000000..a58d68bab83a0e466fcaebffe09f96707af74628 --- /dev/null +++ b/angular/RestClient/environments/environment.ts @@ -0,0 +1,9 @@ +const monolithUrl = 'http://localhost:8080'; + +export const environment = { + production: true, + authAPI: 'http://localhost:8101', + userAPI: `http://${monolithUrl}`, + hotelAPI: `http://${monolithUrl}`, + bookingAPI: `http://${monolithUrl}`, +}; diff --git a/angular/RestClient/src/app/core/features/user/user-booking-list/user-booking-list.component.ts b/angular/RestClient/src/app/core/features/user/user-booking-list/user-booking-list.component.ts index 10be15d9b999d3d0f2a370236507605df40b4bbd..677a18d81df11aef347c455b31460af659e49d31 100644 --- a/angular/RestClient/src/app/core/features/user/user-booking-list/user-booking-list.component.ts +++ b/angular/RestClient/src/app/core/features/user/user-booking-list/user-booking-list.component.ts @@ -44,7 +44,7 @@ export class UserBookingListComponent { } updateBookings() { - this.bookingClient.getUserBookings(this.userId).subscribe({ + this.bookingClient.getBookingsByUser(this.userId).subscribe({ next: (bookings) => { this.search = true; switch (this.selectedState) { @@ -88,7 +88,7 @@ export class UserBookingListComponent { } updateUserStatus() { - this.bookingClient.getUserBookings(this.userId).subscribe({ + this.bookingClient.getBookingsByUser(this.userId).subscribe({ next: (bookings) => { const withActive = bookings.find( (booking) => this.genBookingState(booking) === 'Reserva activa' diff --git a/angular/RestClient/src/app/shared/auth-client.service.ts b/angular/RestClient/src/app/shared/auth-client.service.ts index ec3280b68da6db216a68e72c8847e5fee4e6b04d..61bf7a25a37f8e8208b30e789aa1086e72c6ba32 100644 --- a/angular/RestClient/src/app/shared/auth-client.service.ts +++ b/angular/RestClient/src/app/shared/auth-client.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@angular/core'; -import { environment } from '../../environments/environment'; +import { environment } from '../../../environments/environment'; +import { HttpClient } from '@angular/common/http'; @Injectable({ providedIn: 'root', @@ -7,5 +8,39 @@ import { environment } from '../../environments/environment'; export class AuthClientService { private readonly URI = environment.authAPI; - constructor() {} + constructor(private http: HttpClient) {} + + login(email: String, password: String) { + return this.http.post( + `${this.URI}/login`, + { email, password }, + { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': + 'GET, POST, OPTIONS, PUT, PATCH, DELETE', + 'Access-Control-Allow-Headers': 'X-Requested-With,content-type', + 'Access-Control-Allow-Credentials': 'true', + }, + } + ); + } + + register(name: String, email: String, password: String, rol?: String) { + return this.http.post( + `${this.URI}/register`, + { + name, + email, + password, + rol, + }, + { + headers: { + 'Content-Type': 'application/json', + }, + } + ); + } } diff --git a/angular/RestClient/src/app/shared/booking-client.service.ts b/angular/RestClient/src/app/shared/booking-client.service.ts index 7d38299878b9a5500ade1883c47d08585ca01ebb..f05be5efb55bdfcc544d25a302626400c5cd2835 100644 --- a/angular/RestClient/src/app/shared/booking-client.service.ts +++ b/angular/RestClient/src/app/shared/booking-client.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs'; -import { environment } from '../../environments/environment'; +import { environment } from '../../../environments/environment'; import { Booking } from '../../types/Booking'; // Ajusta la ruta a tu modelo Booking @Injectable({ @@ -31,9 +31,8 @@ export class BookingClientService { return this.http.get<Booking>(`${this.URI}/${id}`); } - getUserBookings(userId: number) { - // TODO revisar tras división en microservicios - return this.http.get<Booking[]>(`${this.URI}/${userId}/bookings`); + getBookingsByUser(userId: number) { + return this.http.get<Booking[]>(`${this.URI}?userId=${userId}`); } // Método para eliminar una reserva diff --git a/angular/RestClient/src/app/shared/hotel-client.service.ts b/angular/RestClient/src/app/shared/hotel-client.service.ts index f00b3e6d5259866c895bffa7856e8e838ae85deb..f38bb2adf2c40e48cafe684ec7dcb12c79ddb524 100644 --- a/angular/RestClient/src/app/shared/hotel-client.service.ts +++ b/angular/RestClient/src/app/shared/hotel-client.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { environment } from '../../environments/environment'; +import { environment } from '../../../environments/environment'; import { HttpClient } from '@angular/common/http'; import { Hotel, Room } from '../../types'; diff --git a/angular/RestClient/src/app/shared/user-client.service.ts b/angular/RestClient/src/app/shared/user-client.service.ts index ea51f7e5060cf349c86159e981748a028806ef80..2a09c6d2c6b373ee3eb329d73066d84591f7fef3 100644 --- a/angular/RestClient/src/app/shared/user-client.service.ts +++ b/angular/RestClient/src/app/shared/user-client.service.ts @@ -1,6 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import { environment } from '../../environments/environment'; +import { environment } from '../../../environments/environment'; import { User, UserState } from '../../types'; @Injectable({ diff --git a/angular/RestClient/src/environments/environment.monolith.ts b/angular/RestClient/src/environments/environment.monolith.ts deleted file mode 100644 index 676025533ad47d166dedf355b0a08f5e8a96e904..0000000000000000000000000000000000000000 --- a/angular/RestClient/src/environments/environment.monolith.ts +++ /dev/null @@ -1,10 +0,0 @@ -// Simple -> Un servicio fachada / monolito -const hostname = 'http://localhost:8080'; - -export const environment = { - production: false, - authAPI: `${hostname}/auth`, - userAPI: `${hostname}/users`, - hotelAPI: `${hostname}/hotels`, - bookingAPI: `${hostname}/bookings`, -}; diff --git a/angular/RestClient/src/environments/environment.prod.ts b/angular/RestClient/src/environments/environment.prod.ts deleted file mode 100644 index 9318edcf83573d82d0005c135849aca26dc609a2..0000000000000000000000000000000000000000 --- a/angular/RestClient/src/environments/environment.prod.ts +++ /dev/null @@ -1,8 +0,0 @@ -// Disgregado en microservicios -export const environment = { - production: true, - authAPI: 'http://auth-api:8080', - userAPI: 'http://users-api:8080', - hotelAPI: 'http://hotels-api:8080', - bookingAPI: 'http://bookings-api:8080', -}; diff --git a/angular/RestClient/src/environments/environment.ts b/angular/RestClient/src/environments/environment.ts deleted file mode 100644 index b5fe467280faf9259227fb281e401cff79c5aa87..0000000000000000000000000000000000000000 --- a/angular/RestClient/src/environments/environment.ts +++ /dev/null @@ -1,8 +0,0 @@ -// Disgregado en microservicios -export const environment = { - production: false, - authAPI: 'http://localhost:8101', - userAPI: 'http://localhost:8111', - hotelAPI: 'http://localhost:8121', - bookingAPI: 'http://localhost:8131', -}; diff --git a/angular/RestClient/src/mocks/bookings.json b/angular/RestClient/src/mocks/bookings.json deleted file mode 100644 index 7db55ff76f1627de21762d95a7732a4f7be1ce34..0000000000000000000000000000000000000000 --- a/angular/RestClient/src/mocks/bookings.json +++ /dev/null @@ -1,30 +0,0 @@ -[ - { - "user": { - "name": "John Doe", - "email": "john.doe@example.com", - "status": "NO_BOOKINGS" - }, - "room": { - "roomNumber": "101", - "type": "SINGLE", - "available": true - }, - "startDate": "2024-03-01", - "endDate": "2024-03-08" - }, - { - "user": { - "name": "Pepe", - "email": "pepe@example.com", - "status": "WITH_ACTIVE_BOOKINGS" - }, - "room": { - "roomNumber": "101", - "type": "SINGLE", - "available": true - }, - "startDate": "2024-03-15", - "endDate": "2024-03-22" - } -] diff --git a/angular/RestClient/src/mocks/hotels.json b/angular/RestClient/src/mocks/hotels.json deleted file mode 100644 index 11381112f37ac1123d22f06a2a0526c07825f211..0000000000000000000000000000000000000000 --- a/angular/RestClient/src/mocks/hotels.json +++ /dev/null @@ -1,46 +0,0 @@ -[ - { - "id": 1, - "name": "Hotel 1", - "address": { - "id": 1, - "streetName": "Aca al lao", - "streetKind": "Alargada", - "number": 12, - "postCode": "12345" - }, - "rooms": [ - { - "id": 1, - "roomNumber": "101", - "type": "SINGLE", - "available": true - }, - { - "id": 2, - "roomNumber": "102", - "type": "DOUBLE", - "available": false - } - ] - }, - { - "id": 2, - "name": "Hotel 2", - "address": { - "id": 2, - "streetName": "Calle de la plaza", - "streetKind": "Alargada", - "number": 12, - "postCode": "12345" - }, - "rooms": [ - { - "id": 3, - "roomNumber": "103", - "type": "SUITE", - "available": true - } - ] - } -] diff --git a/angular/RestClient/src/mocks/users.json b/angular/RestClient/src/mocks/users.json deleted file mode 100644 index 2f02d00125b0deaa2a86db47ea26f0c91f2e5fa3..0000000000000000000000000000000000000000 --- a/angular/RestClient/src/mocks/users.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - "name": "John Doe", - "email": "john.doe@example.com", - "status": "NO_BOOKINGS" - }, - { - "name": "Pepe", - "email": "pepe@example.com", - "status": "WITH_ACTIVE_BOOKINGS" - } -] diff --git a/java/roomBooking/src/main/java/com/uva/monolith/services/bookings/controllers/BookingController.java b/java/roomBooking/src/main/java/com/uva/monolith/services/bookings/controllers/BookingController.java index f309f2b1be9be7f5c7dbd1f0e46309cab95f4a34..fb61c1841d43ae67f0536717eae3bb69cfada116 100644 --- a/java/roomBooking/src/main/java/com/uva/monolith/services/bookings/controllers/BookingController.java +++ b/java/roomBooking/src/main/java/com/uva/monolith/services/bookings/controllers/BookingController.java @@ -33,11 +33,12 @@ public class BookingController { this.roomRepository = roomRepository; } - @GetMapping(params = { "start", "end", "roomId" }) + @GetMapping() public List<Booking> getAllBookings( @RequestParam(required = false) LocalDate start, @RequestParam(required = false) LocalDate end, - @RequestParam(required = false) Integer roomId) { + @RequestParam(required = false) Integer roomId, + @RequestParam(required = false) Integer userId) { List<Booking> bookings = null; if (start != null && end != null) { @@ -52,7 +53,16 @@ public class BookingController { .toList(); } } - if (start == null & end == null && roomId == null) { + if (userId != null) { + if (bookings == null) { + bookings = bookingRepository.findByUserId(userId); + } else { + bookings = bookings.stream() + .filter(booking -> booking.getUserId().getId() == userId) + .toList(); + } + } + if (start == null & end == null && roomId == null && userId == null) { bookings = bookingRepository.findAll(); } return bookings; diff --git a/java/roomBooking/src/main/java/com/uva/monolith/services/bookings/repositories/BookingRepository.java b/java/roomBooking/src/main/java/com/uva/monolith/services/bookings/repositories/BookingRepository.java index 103dbaa5ef76f8027b8152810fb9ca67aba19546..b5ace65939b898798e6e5416fedda793388f2615 100644 --- a/java/roomBooking/src/main/java/com/uva/monolith/services/bookings/repositories/BookingRepository.java +++ b/java/roomBooking/src/main/java/com/uva/monolith/services/bookings/repositories/BookingRepository.java @@ -14,25 +14,28 @@ import org.springframework.data.repository.query.Param; import com.uva.monolith.services.bookings.models.Booking; public interface BookingRepository extends JpaRepository<Booking, Integer> { - @Query("SELECT b FROM Booking b WHERE b.roomId.id = ?1") - List<Booking> findByRoomId(int roomId); - - @Query("SELECT b FROM Booking b WHERE b.startDate < ?1 AND b.endDate > ?2") - List<Booking> findByDateRange(@Param("startDate") LocalDate startDate, - @Param("endDate") LocalDate endDate); - - @Query("SELECT b FROM Booking b WHERE b.roomId.id = ?1 AND b.startDate < ?2 AND b.endDate > ?3") - List<Booking> findByRoomIdAndDateRange(@Param("roomId") int roomId, @Param("startDate") LocalDate startDate, - @Param("endDate") LocalDate endDate); - - @Transactional - @Modifying - @Query("DELETE FROM Booking b WHERE b.id = ?1") - void deleteBookingById(@Param("id") Integer id); - - @Transactional - @Modifying - @Query("DELETE FROM Booking b WHERE b.roomId.hotel.id = ?1") - void deleteAllByHotelId(int hotelId); + @Query("SELECT b FROM Booking b WHERE b.userId.id = ?1") + List<Booking> findByUserId(int userId); + + @Query("SELECT b FROM Booking b WHERE b.roomId.id = ?1") + List<Booking> findByRoomId(int roomId); + + @Query("SELECT b FROM Booking b WHERE b.startDate >= ?1 AND b.endDate <= ?2") + List<Booking> findByDateRange(@Param("startDate") LocalDate startDate, + @Param("endDate") LocalDate endDate); + + @Query("SELECT b FROM Booking b WHERE b.roomId.id = ?1 AND b.startDate < ?2 AND b.endDate > ?3") + List<Booking> findByRoomIdAndDateRange(@Param("roomId") int roomId, @Param("startDate") LocalDate startDate, + @Param("endDate") LocalDate endDate); + + @Transactional + @Modifying + @Query("DELETE FROM Booking b WHERE b.id = ?1") + void deleteBookingById(@Param("id") Integer id); + + @Transactional + @Modifying + @Query("DELETE FROM Booking b WHERE b.roomId.hotel.id = ?1") + void deleteAllByHotelId(int hotelId); } diff --git a/java/roomBooking/src/main/java/com/uva/monolith/services/users/controllers/UserController.java b/java/roomBooking/src/main/java/com/uva/monolith/services/users/controllers/UserController.java index 812d0b83ff5b82c078d3f471c479fb680db252df..c097ef7d7c26443ff82da3860e1cf54362df94d2 100644 --- a/java/roomBooking/src/main/java/com/uva/monolith/services/users/controllers/UserController.java +++ b/java/roomBooking/src/main/java/com/uva/monolith/services/users/controllers/UserController.java @@ -1,6 +1,7 @@ package com.uva.monolith.services.users.controllers; import java.time.LocalDate; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.RestController; import com.uva.monolith.services.bookings.models.Booking; import com.uva.monolith.services.users.models.User; +import com.uva.monolith.services.users.models.UserRol; import com.uva.monolith.services.users.models.UserStatus; import com.uva.monolith.services.users.repositories.UserRepository; @@ -50,6 +52,8 @@ public class UserController { @PostMapping public User addUser(@RequestBody User user) { user.setStatus(UserStatus.NO_BOOKINGS); + if (user.getRol() == null) // Rol por defecto + user.setRol(UserRol.CONSUMER); return userRepository.save(user); } diff --git a/java/roomBooking/src/main/java/com/uva/monolith/services/users/models/User.java b/java/roomBooking/src/main/java/com/uva/monolith/services/users/models/User.java index 3936e7d566f1c43c4a702f67fbcdb2707edac64c..5c1f928c0bf0f273f27d890739801c89bdd9c9be 100644 --- a/java/roomBooking/src/main/java/com/uva/monolith/services/users/models/User.java +++ b/java/roomBooking/src/main/java/com/uva/monolith/services/users/models/User.java @@ -102,7 +102,7 @@ public class User { } public UserStatus getStatus() { - if (!getBookings().isEmpty()) + if (getBookings() == null || getBookings().isEmpty()) return UserStatus.NO_BOOKINGS; boolean activeBookings = getBookings().stream() .anyMatch(booking -> !booking.getEndDate().isBefore(LocalDate.now())); // reserva >= ahora diff --git a/java/services/auth/src/main/java/com/uva/authentication/controllers/AuthController.java b/java/services/auth/src/main/java/com/uva/authentication/controllers/AuthController.java index 1bd5c44944e419ea24f3ccff7984aa8c9edae502..08ad46a807354a557e2bd5f6f6f9d5fc94a79949 100644 --- a/java/services/auth/src/main/java/com/uva/authentication/controllers/AuthController.java +++ b/java/services/auth/src/main/java/com/uva/authentication/controllers/AuthController.java @@ -31,8 +31,12 @@ public class AuthController { @PostMapping("/register") public ResponseEntity<String> register(@RequestBody RegisterRequest registerRequest) { try { + LoginRequest loginRequest = new LoginRequest(); + loginRequest.setEmail(registerRequest.getEmail()); + loginRequest.setPassword(registerRequest.getPassword()); + authService.register(registerRequest); - return login(registerRequest); + return login(loginRequest); } catch (HttpClientErrorException e) { if (e.getStatusCode() == HttpStatus.CONFLICT) { // return new ResponseEntity<Void>(HttpStatus.FORBIDDEN); diff --git a/java/services/auth/src/main/java/com/uva/authentication/services/AuthService.java b/java/services/auth/src/main/java/com/uva/authentication/services/AuthService.java index c5ed741466942fa64aa3c6ac47b42a424b90f76d..872aa3bd27e6635786c9effb6873118fefbad2d7 100644 --- a/java/services/auth/src/main/java/com/uva/authentication/services/AuthService.java +++ b/java/services/auth/src/main/java/com/uva/authentication/services/AuthService.java @@ -31,8 +31,8 @@ public class AuthService { if (user == null) return false; String hashPass = hashPass(request.getPassword()); - // System.err.println(request.getPassword() + " -> " + hashPass + " == " + - // user.getPassword()); + System.err.println(request.getPassword() + " -> " + hashPass + " == " + + user.getPassword()); return hashPass.equals(user.getPassword()); } @@ -55,6 +55,8 @@ public class AuthService { if (user != null) throw new HttpClientErrorException(HttpStatus.CONFLICT, "Email already in use"); + String hashPass = hashPass(registerRequest.getPassword()); + registerRequest.setPassword(hashPass); return userAPI.registerUser(registerRequest); }