From 39cc475ccbb5f668b9bb83062a98d17ff77a3f12 Mon Sep 17 00:00:00 2001
From: migudel <miguel.moras@estudiantes.uva.es>
Date: Sat, 23 Nov 2024 12:46:31 +0100
Subject: [PATCH] =?UTF-8?q?Resto=20de=20cambios=20del=20anterior=20commit?=
 =?UTF-8?q?=20antes=20de=20fusi=C3=B3n?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../RestClient/environments/environment.ts    |  9 ++++
 .../user-booking-list.component.ts            |  4 +-
 .../src/app/shared/auth-client.service.ts     | 39 +++++++++++++++-
 .../src/app/shared/booking-client.service.ts  |  7 ++-
 .../src/app/shared/hotel-client.service.ts    |  2 +-
 .../src/app/shared/user-client.service.ts     |  2 +-
 .../src/environments/environment.monolith.ts  | 10 ----
 .../src/environments/environment.prod.ts      |  8 ----
 .../src/environments/environment.ts           |  8 ----
 angular/RestClient/src/mocks/bookings.json    | 30 ------------
 angular/RestClient/src/mocks/hotels.json      | 46 -------------------
 angular/RestClient/src/mocks/users.json       | 12 -----
 .../controllers/BookingController.java        | 16 +++++--
 .../repositories/BookingRepository.java       | 43 +++++++++--------
 .../users/controllers/UserController.java     |  4 ++
 .../monolith/services/users/models/User.java  |  2 +-
 .../controllers/AuthController.java           |  6 ++-
 .../authentication/services/AuthService.java  |  6 ++-
 18 files changed, 103 insertions(+), 151 deletions(-)
 create mode 100644 angular/RestClient/environments/environment.ts
 delete mode 100644 angular/RestClient/src/environments/environment.monolith.ts
 delete mode 100644 angular/RestClient/src/environments/environment.prod.ts
 delete mode 100644 angular/RestClient/src/environments/environment.ts
 delete mode 100644 angular/RestClient/src/mocks/bookings.json
 delete mode 100644 angular/RestClient/src/mocks/hotels.json
 delete mode 100644 angular/RestClient/src/mocks/users.json

diff --git a/angular/RestClient/environments/environment.ts b/angular/RestClient/environments/environment.ts
new file mode 100644
index 0000000..a58d68b
--- /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 10be15d..677a18d 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 ec3280b..61bf7a2 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 7d38299..f05be5e 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 f00b3e6..f38bb2a 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 ea51f7e..2a09c6d 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 6760255..0000000
--- 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 9318edc..0000000
--- 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 b5fe467..0000000
--- 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 7db55ff..0000000
--- 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 1138111..0000000
--- 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 2f02d00..0000000
--- 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 f309f2b..fb61c18 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 103dbaa..b5ace65 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 812d0b8..c097ef7 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 3936e7d..5c1f928 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 1bd5c44..08ad46a 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 c5ed741..872aa3b 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);
   }
 
-- 
GitLab