-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0f5f56
commit 27606c0
Showing
14 changed files
with
631 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.app.config; | ||
|
||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.web.filter.HiddenHttpMethodFilter; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
@Bean | ||
public FilterRegistrationBean<HiddenHttpMethodFilter> hiddenHttpMethodFilter() { | ||
FilterRegistrationBean<HiddenHttpMethodFilter> filterRegistrationBean = new FilterRegistrationBean<>(new HiddenHttpMethodFilter()); | ||
filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE); | ||
return filterRegistrationBean; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/app/controllers/manager/ManagerBookingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.app.controllers.manager; | ||
|
||
import com.app.dtos.BookingDTO; | ||
import com.app.models.Booking; | ||
import com.app.services.BookingService; | ||
import com.app.services.UserService; | ||
import groovy.util.logging.Slf4j; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.servlet.mvc.support.RedirectAttributes; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Scanner; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
@Controller | ||
@RequestMapping("/manager/bookings") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ManagerBookingController { | ||
|
||
@Autowired | ||
BookingService bookingService; | ||
|
||
@Autowired | ||
UserService userService; | ||
|
||
@GetMapping | ||
public String index(Model model, | ||
@RequestParam("page") Optional<Integer> page, | ||
@RequestParam("size") Optional<Integer> size) { | ||
int currentPage = page.orElse(1); | ||
int pageSize = size.orElse(5); | ||
|
||
Pageable pageable = PageRequest.of(currentPage - 1, pageSize); | ||
Page<Booking> bookingPage = bookingService.findPaginatedBookings(pageable); | ||
int totalPages = bookingPage.getTotalPages(); | ||
model.addAttribute("bookingPage", bookingPage); | ||
if (totalPages > 0) { | ||
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages) | ||
.boxed() | ||
.collect(Collectors.toList()); | ||
model.addAttribute("pageNumbers", pageNumbers); | ||
} | ||
|
||
return "manager/bookings/index"; | ||
} | ||
|
||
@GetMapping("/new") | ||
public String showCreateForm(Model model) { | ||
model.addAttribute("bookingDTO", new BookingDTO()); | ||
model.addAttribute("customers", userService.findAll()); | ||
return "manager/bookings/add-booking"; | ||
} | ||
|
||
@PostMapping("/") | ||
public String saveBooking(@ModelAttribute("bookingDTO") BookingDTO bookingDTO, RedirectAttributes redirectAttributes) { | ||
bookingService.saveBooking(bookingDTO); | ||
redirectAttributes.addFlashAttribute("message", "Booking created successfully!"); | ||
return "redirect:/manager/bookings"; | ||
} | ||
|
||
@GetMapping("/{id}/edit") | ||
public String showEditForm(@PathVariable("id") Integer id, Model model) { | ||
Booking booking = bookingService.findById(id); | ||
model.addAttribute("bookingDTO", bookingService.convertToDTO(booking)); | ||
return "manager/bookings/edit-booking"; | ||
} | ||
|
||
@PatchMapping("/{id}/update") | ||
public String updateBooking(@PathVariable("id") Integer id, @ModelAttribute("bookingDTO") BookingDTO bookingDTO, RedirectAttributes redirectAttributes) { | ||
bookingService.updateBooking(id, bookingDTO); | ||
redirectAttributes.addFlashAttribute("message", "Booking updated successfully!"); | ||
return "redirect:/manager/bookings"; | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public String deleteBooking(@PathVariable("id") Integer id, RedirectAttributes redirectAttributes) { | ||
bookingService.deleteBooking(id); | ||
redirectAttributes.addFlashAttribute("message", "Booking deleted successfully!"); | ||
return "redirect:/manager/bookings"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.