Ignore:
Timestamp:
04/28/25 14:21:17 (3 weeks ago)
Author:
Aleksandar Panovski <apano77@…>
Branches:
main
Children:
e15e8d9
Parents:
f5b256e
Message:

Big change done fully handle_reservation_update() trigger works

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/rezevirajmasa/demo/web/rest/testController.java

    rf5b256e rdeea3c4  
    11package com.example.rezevirajmasa.demo.web.rest;
    22
    3 import com.example.rezevirajmasa.demo.dto.CustomerDTO;
    4 import com.example.rezevirajmasa.demo.dto.UserDto;
     3import com.example.rezevirajmasa.demo.dto.*;
    54import com.example.rezevirajmasa.demo.mappers.UserMapper;
    65import com.example.rezevirajmasa.demo.model.*;
     6import com.example.rezevirajmasa.demo.model.exceptions.InvalidReservationException;
    77import com.example.rezevirajmasa.demo.service.*;
    88import com.example.rezevirajmasa.demo.service.impl.TokenService;
     
    2525import java.time.LocalDateTime;
    2626import java.time.format.DateTimeFormatter;
    27 import java.util.ArrayList;
    28 import java.util.List;
    29 import java.util.Map;
    30 import java.util.Optional;
     27import java.util.*;
     28import java.util.stream.Collectors;
    3129
    3230@CrossOrigin(origins = "http://localhost:3000/")
     
    5755    }
    5856
    59     //restaurants
    60 
    61     @RequestMapping("/api/restaurants")
    62     public ResponseEntity<List<Restaurant>> getAllRestaurants() {
    63         return new ResponseEntity<List<Restaurant>>(restaurantService.listall(), HttpStatus.OK);
    64     }
    65 
    66     @RequestMapping("/api/restaurants/{restaurantId}")
    67     public ResponseEntity<Restaurant> getRestaurantById(@PathVariable Long restaurantId) {
    68         return new ResponseEntity<Restaurant>(restaurantService.findById(restaurantId), HttpStatus.OK);
     57    //restaurants calls
     58
     59    @GetMapping("/api/restaurants")
     60    public ResponseEntity<List<RestaurantDTO>> getAllRestaurants() {
     61            return new ResponseEntity<List<RestaurantDTO>>(restaurantService.listall(), HttpStatus.OK);
     62    }
     63
     64    @GetMapping("/api/restaurants/{restaurantId}")
     65    public ResponseEntity<RestaurantDTO> getRestaurantById(@PathVariable Long restaurantId) {
     66        return new ResponseEntity<RestaurantDTO>(restaurantService.findById(restaurantId), HttpStatus.OK);
    6967    }
    7068
    7169    @PostMapping("/api/search")
    72     public ResponseEntity<List<Restaurant>> searchRestaurants(@RequestBody Map<String, Object> requestData) {
     70    public ResponseEntity<List<RestaurantDTO>> searchRestaurants(@RequestBody Map<String, Object> requestData) {
    7371        Optional<String> dateTimeOptional = Optional.ofNullable((String) requestData.get("dateTime"));
    7472        int partySize = Integer.parseInt(requestData.get("partySize").toString());
     
    8482        }
    8583
    86         List<Restaurant> filteredRestaurants = restaurantService.findRestaurantsBySearchParams(parsedDateTime, partySize, search);
    87 
    88         return new ResponseEntity<List<Restaurant>>(filteredRestaurants, HttpStatus.OK);
     84        List<RestaurantDTO> filteredRestaurants = restaurantService.findRestaurantsBySearchParams(parsedDateTime, partySize, search);
     85
     86        return new ResponseEntity<>(filteredRestaurants, HttpStatus.OK);
    8987    }
    9088
    9189    @PostMapping("/api/search/shortcut/{param}")
    92     public ResponseEntity<List<Restaurant>> searchByCuisineTypeShortcut(@PathVariable String param) {
    93         List<Restaurant> filteredRestaurants;
     90    public ResponseEntity<List<RestaurantDTO>> searchByCuisineTypeShortcut(@PathVariable String param) {
     91        List<RestaurantDTO> filteredRestaurants;
    9492        if(param != null && !param.isEmpty()) {
    9593            filteredRestaurants = restaurantService.findRestaurantsByCuisineType(param);
     
    9795            filteredRestaurants = restaurantService.listall();
    9896        }
    99         return new ResponseEntity<List<Restaurant>>(filteredRestaurants, HttpStatus.OK);
     97        return new ResponseEntity<List<RestaurantDTO>>(filteredRestaurants, HttpStatus.OK);
    10098    }
    10199
     
    106104    }
    107105
     106    // User calls
     107
     108    @GetMapping("/api/user/{email}")
     109    public ResponseEntity<User> getUserByEmail(@PathVariable String email)
     110    {
     111        User user = userService.findByMail(email);
     112        return ResponseEntity.ok(user);
     113    }
     114
    108115//    Customer CALLS
    109116
    110     @RequestMapping("/api/customers")
     117    @GetMapping("/api/customers")
    111118    public ResponseEntity<List<CustomerDTO>> getAllCustomers() {
    112119        List<Customer> customers = customerService.listAll();
     
    119126    }
    120127
    121     @RequestMapping("/api/customers/{id}")
     128    @GetMapping("/api/customers/{id}")
    122129    public ResponseEntity<Customer> getCustomerById(@PathVariable Long id) {
    123130        return new ResponseEntity<Customer>(customerService.findById(id), HttpStatus.OK);
     
    130137        );
    131138    }
    132     @GetMapping("/api/user")
    133     public ResponseEntity<?> getCurrentUser() {
    134         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    135 
    136         if (authentication == null || !authentication.isAuthenticated() || authentication.getPrincipal().equals("anonymousUser")) {
    137             return new ResponseEntity<>("User is not authenticated", HttpStatus.UNAUTHORIZED);
    138         }
    139 
    140         Customer customer = (Customer) authentication.getPrincipal();
    141         return new ResponseEntity<>(customer, HttpStatus.OK);
    142     }
    143 
    144139
    145140    @PostMapping("/api/customers")
     
    166161
    167162    @GetMapping("/api/reservations/by/{email}")
    168     public ResponseEntity<List<Reservation>> getReservations(@PathVariable String email) {
    169         User user = userService.findByMail(email);
    170         return new ResponseEntity<List<Reservation>>(reservationService.findReservationByUser(user), HttpStatus.OK);
    171     }
    172 
    173     @PostMapping("/api/reservations/{email}")
    174     public ResponseEntity<Reservation> createReservation(@RequestBody Reservation reservation, @PathVariable String email) {
    175         User user = userService.findByMail(email);
     163    public ResponseEntity<List<ReservationDTO>> getReservations(@PathVariable String email) {
     164        if (email == null || email.isEmpty()) {
     165            return ResponseEntity.badRequest().build();
     166        }
     167        User user = userService.findByMail(email);
     168        if (user == null) {
     169            return ResponseEntity.notFound().build();
     170        }
     171
     172        List<Reservation> reservations = reservationService.findReservationByUser(user);
     173        List<ReservationDTO> reservationDTOs = reservations.stream()
     174                .map(ReservationDTO::new)
     175                .collect(Collectors.toList());
     176
     177        return ResponseEntity.ok(reservationDTOs);
     178    }
     179
     180    @PostMapping("/api/reservations")
     181    public ResponseEntity<?> createReservation(@RequestBody ReservationDTO reservation) {
     182        User user = userService.findByMail(reservation.getUserEmail());
    176183        Reservation savedReservation = reservationService.makeReservationRest(reservation, user);
    177184
     
    179186    }
    180187
    181 
    182188    @GetMapping("/api/reservations/{reservationId}")
    183     public ResponseEntity<Reservation> getReservation(@PathVariable Long reservationId) {
     189    public ResponseEntity<ReservationDTO> getReservation(@PathVariable Long reservationId) {
    184190        Reservation reservation = reservationService.findById(reservationId);
    185191        if (reservation != null) {
    186             return ResponseEntity.ok(reservation);
    187         } else {
    188             return ResponseEntity.notFound().build();
    189         }
    190     }
     192            return ResponseEntity.ok(new ReservationDTO(reservation));
     193        } else {
     194            return ResponseEntity.notFound().build();
     195        }
     196    }
     197
     198//    @GetMapping("/api/reservations/{restaurantId}")
     199//    public ResponseEntity<List<Reservation>> getReservationsByRestaurant(@PathVariable Long restaurantId)
     200//    {
     201//        Restaurant restaurant = restaurantService.findByIdRestaurant(restaurantId);
     202//        List<Reservation> reservations = reservationService.findAllByRestaurant(restaurant);
     203//        return ResponseEntity.ok(reservations);
     204//    }
     205
     206    @GetMapping("/api/table-reservations/{tableId}")
     207    public ResponseEntity<List<LocalDateTime>> tableReservations(@PathVariable Long tableId) {
     208        TableEntity table = tableService.findByIdTable(tableId);
     209        if (table == null) {
     210            return ResponseEntity.notFound().build();
     211        }
     212
     213        List<Reservation> reservations = reservationService.reservationsForTable(table);
     214        List<LocalDateTime> reservedTimes = reservations.stream()
     215                .map(Reservation::getReservationDateTime)
     216                .collect(Collectors.toList());
     217
     218        return ResponseEntity.ok(reservedTimes);
     219    }
     220
    191221
    192222    @PostMapping("/api/reservations/{reservationId}/{email}")
    193223    public ResponseEntity<Reservation> editReservation(@PathVariable Long reservationId,
    194                                                        @RequestBody Reservation reservation,
     224                                                       @RequestBody ReservationDTO reservationDTO,
    195225                                                       @PathVariable String email) {
    196226        User user = userService.findByMail(email);
    197227
    198         if (!reservation.getReservationID().equals(reservationId)) {
     228        if (!reservationDTO.getReservationID().equals(reservationId)) {
    199229            return ResponseEntity.badRequest().build();
    200230        }
    201231
    202         // Fetch existing reservation
    203         Reservation existingReservation = reservationService.findById(reservationId);
    204         if (existingReservation == null) {
    205             return ResponseEntity.notFound().build();
    206         }
    207 
    208         if (!reservation.getCheckInTime().equals(existingReservation.getCheckInTime())) {
    209             tableService.canceledTimeSlots(existingReservation.getTable().getId(), existingReservation.getCheckInTime());
    210 
    211             tableService.deleteTimeSlotsForReservation(reservation.getTable().getId(), reservation.getCheckInTime());
    212         }
    213 
    214         existingReservation.setReservationDateTime(LocalDateTime.now());
    215         existingReservation.setCheckInTime(reservation.getCheckInTime());
    216         existingReservation.setCheckOutTime(reservation.getCheckInTime().plusHours(2));
    217         existingReservation.setPartySize(reservation.getPartySize());
    218         existingReservation.setSpecialRequests(reservation.getSpecialRequests());
    219 
    220         Reservation updatedReservation = reservationService.makeReservationRest(existingReservation, user);
    221 
    222         System.out.println("Updated reservation time: " + updatedReservation.getCheckInTime());
    223         return ResponseEntity.ok(updatedReservation);
    224     }
     232        try {
     233            Reservation updatedReservation = reservationService.updateReservation(reservationId, reservationDTO, user);
     234            return ResponseEntity.ok(updatedReservation);
     235        } catch (InvalidReservationException e) {
     236            return ResponseEntity.status(409).body(null);
     237        } catch (Exception e) {
     238            return ResponseEntity.status(500).build();
     239        }
     240    }
     241
    225242
    226243
     
    241258    }
    242259
    243 //    TableEntity Calls
    244 
     260    // TableEntity Calls
    245261    @GetMapping("/api/tables/{tableId}")
    246     public ResponseEntity<TableEntity> findTableById(@PathVariable Long tableId) {
    247         return new ResponseEntity<TableEntity>(tableService.findById(tableId), HttpStatus.OK);
     262    public ResponseEntity<TableDTO> findTableById(@PathVariable Long tableId) {
     263        TableDTO tableDTO = tableService.findById(tableId);
     264        return new ResponseEntity<>(tableDTO, HttpStatus.OK);
    248265    }
    249266
     
    255272        return new ResponseEntity<>(reservations, HttpStatus.OK);
    256273    }
    257 
    258274}
Note: See TracChangeset for help on using the changeset viewer.