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

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

use model instead of session

  • Property mode set to 100644
File size: 3.8 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){
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
86 return "list-posts-adoption";
87 }
88
89 @GetMapping("/pet-details-{id}")
90 public String getPostDetails(@PathVariable Integer id,
91 Model model){
92
93 Post post = this.postService.findById(id).get();
94 Pet pet = this.petService.findById(post.getPetId());
95
96 model.addAttribute("pet", pet);
97 model.addAttribute("post", post);
98
99 return "pet-details";
100 }
101}
Note: See TracBrowser for help on using the repository browser.