source: Prototype Application/Paw5/src/main/java/finki/paw5/web/controllers/PostController.java@ 9358bff

main
Last change on this file since 9358bff was eac569a, checked in by SazdovaEkaterina <sazdovaekaterina@…>, 16 months ago

merge main into adopting-a-pet

  • Property mode set to 100644
File size: 4.3 KB
Line 
1package finki.paw5.web.controllers;
2import finki.paw5.model.entities.Pet;
3import finki.paw5.model.entities.Post;
4import finki.paw5.model.entities.Employee;
5import finki.paw5.model.enumerations.AgeGroup;
6import finki.paw5.model.enumerations.Gender;
7import finki.paw5.model.enumerations.Size;
8import finki.paw5.model.enumerations.Species;
9import finki.paw5.service.PersonalProfileService;
10import finki.paw5.service.PetService;
11import finki.paw5.service.PostService;
12import finki.paw5.service.ShelterService;
13import jakarta.servlet.http.HttpServletRequest;
14import org.springframework.stereotype.Controller;
15import org.springframework.ui.Model;
16import org.springframework.web.bind.annotation.GetMapping;
17import org.springframework.web.bind.annotation.PathVariable;
18import org.springframework.web.bind.annotation.PostMapping;
19import org.springframework.web.bind.annotation.RequestParam;
20
21import java.time.LocalDate;
22import java.util.List;
23
24@Controller
25public 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}
Note: See TracBrowser for help on using the repository browser.