1 | package finki.paw5.web.controllers;
|
---|
2 | import finki.paw5.model.entities.Pet;
|
---|
3 | import finki.paw5.model.entities.Post;
|
---|
4 | import finki.paw5.model.entities.Employee;
|
---|
5 | import finki.paw5.model.enumerations.AgeGroup;
|
---|
6 | import finki.paw5.model.enumerations.Gender;
|
---|
7 | import finki.paw5.model.enumerations.Size;
|
---|
8 | import finki.paw5.model.enumerations.Species;
|
---|
9 | import finki.paw5.service.PersonalProfileService;
|
---|
10 | import finki.paw5.service.PetService;
|
---|
11 | import finki.paw5.service.PostService;
|
---|
12 | import finki.paw5.service.ShelterService;
|
---|
13 | import jakarta.servlet.http.HttpServletRequest;
|
---|
14 | import org.springframework.stereotype.Controller;
|
---|
15 | import org.springframework.ui.Model;
|
---|
16 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
17 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
18 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
19 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
20 |
|
---|
21 | import java.time.LocalDate;
|
---|
22 | import java.util.List;
|
---|
23 |
|
---|
24 | @Controller
|
---|
25 | public class PostController {
|
---|
26 |
|
---|
27 | private final PostService postService;
|
---|
28 | private final PetService petService;
|
---|
29 | private final PersonalProfileService personalProfileService;
|
---|
30 |
|
---|
31 | public PostController(PostService postService, PetService petService, PersonalProfileService personalProfileService, ShelterService shelterService) {
|
---|
32 | this.postService = postService;
|
---|
33 | this.petService = petService;
|
---|
34 | this.personalProfileService = personalProfileService;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @GetMapping("/create-post")
|
---|
38 | public String get(Model model) {
|
---|
39 | model.addAttribute("pets", this.petService.listpets());
|
---|
40 | return "create-post";
|
---|
41 | }
|
---|
42 |
|
---|
43 | @PostMapping("/submit-post")
|
---|
44 | public String savePost(@RequestParam(required = false) Integer petId,
|
---|
45 | @RequestParam(required = false) boolean newPetCheckbox,
|
---|
46 | @RequestParam(required = false) String name,
|
---|
47 | @RequestParam(required = false) String gender,
|
---|
48 | @RequestParam(required = false) String ageGroup,
|
---|
49 | @RequestParam(required = false) String size,
|
---|
50 | @RequestParam(required = false) String species,
|
---|
51 | @RequestParam(required = false) String breed,
|
---|
52 | @RequestParam(required = false) String imageUrl,
|
---|
53 | @RequestParam(required = false) boolean canBeFostered,
|
---|
54 | HttpServletRequest request) {
|
---|
55 |
|
---|
56 | Employee employee = (Employee) request.getSession().getAttribute("user");
|
---|
57 |
|
---|
58 | if(newPetCheckbox == true){
|
---|
59 |
|
---|
60 | Pet newPet = new Pet(imageUrl, AgeGroup.valueOf(ageGroup), Size.valueOf(size), breed, name, Species.valueOf(species), Gender.valueOf(gender), canBeFostered, null, employee.getShelterId());
|
---|
61 | this.petService.save(newPet);
|
---|
62 |
|
---|
63 | Post post = new Post(LocalDate.now(), imageUrl, newPet.getId(), null, employee.getId());
|
---|
64 | this.postService.save(post);
|
---|
65 |
|
---|
66 | } else{
|
---|
67 |
|
---|
68 | Pet selectedPet = this.petService.findById(petId);
|
---|
69 |
|
---|
70 | Post post = new Post(LocalDate.now(), imageUrl, selectedPet.getId(), null, employee.getId());
|
---|
71 | this.postService.save(post);
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | return "redirect:/home";
|
---|
76 | }
|
---|
77 |
|
---|
78 | @GetMapping("/adoption-posts")
|
---|
79 | public String getAdoptionPosts(Model model, HttpServletRequest request){
|
---|
80 |
|
---|
81 | List<Post> posts = this.postService.findAll();
|
---|
82 | List<Pet> pets = this.petService.listpets();
|
---|
83 | //model.addAttribute("posts", posts);
|
---|
84 | //model.addAttribute("pets",pets);
|
---|
85 | request.getSession().setAttribute("posts",posts);//temp
|
---|
86 | request.getSession().setAttribute("pets",pets);//temp
|
---|
87 |
|
---|
88 | return "list-posts-adoption";
|
---|
89 | }
|
---|
90 |
|
---|
91 | @GetMapping("/pet-details-{id}")
|
---|
92 | public String getPostDetails(@PathVariable Integer id, Model model, HttpServletRequest request){
|
---|
93 |
|
---|
94 | Post post = this.postService.findById(id).get();
|
---|
95 | Pet pet = this.petService.findById(post.getPetId());
|
---|
96 |
|
---|
97 | //model.addAttribute("pet", pet);
|
---|
98 | //model.addAttribute("post", post);
|
---|
99 | request.getSession().setAttribute("post", post);//temp
|
---|
100 | request.getSession().setAttribute("pet", pet);//temp
|
---|
101 |
|
---|
102 | if(pet.getAdoptionId() != null){
|
---|
103 | request.getSession().setAttribute("disableAdoption", true);
|
---|
104 | } else{
|
---|
105 | request.getSession().setAttribute("disableAdoption", false);
|
---|
106 | }
|
---|
107 |
|
---|
108 | return "pet-details";
|
---|
109 | }
|
---|
110 | }
|
---|