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
RevLine 
[738b31a]1package finki.paw5.web.controllers;
[468b7b6]2import finki.paw5.model.entities.Pet;
3import finki.paw5.model.entities.Post;
[eac569a]4import finki.paw5.model.entities.Employee;
[738b31a]5import finki.paw5.model.enumerations.AgeGroup;
6import finki.paw5.model.enumerations.Gender;
7import finki.paw5.model.enumerations.Size;
8import finki.paw5.model.enumerations.Species;
[a762b3a]9import finki.paw5.service.PersonalProfileService;
[468b7b6]10import finki.paw5.service.PetService;
[738b31a]11import finki.paw5.service.PostService;
[a762b3a]12import finki.paw5.service.ShelterService;
13import jakarta.servlet.http.HttpServletRequest;
[738b31a]14import org.springframework.stereotype.Controller;
[468b7b6]15import org.springframework.ui.Model;
16import org.springframework.web.bind.annotation.GetMapping;
[a762b3a]17import org.springframework.web.bind.annotation.PathVariable;
[738b31a]18import org.springframework.web.bind.annotation.PostMapping;
19import org.springframework.web.bind.annotation.RequestParam;
20
[468b7b6]21import java.time.LocalDate;
[a762b3a]22import java.util.List;
[468b7b6]23
[738b31a]24@Controller
25public class PostController {
26
27 private final PostService postService;
[468b7b6]28 private final PetService petService;
[a762b3a]29 private final PersonalProfileService personalProfileService;
[738b31a]30
[a762b3a]31 public PostController(PostService postService, PetService petService, PersonalProfileService personalProfileService, ShelterService shelterService) {
[738b31a]32 this.postService = postService;
[468b7b6]33 this.petService = petService;
[a762b3a]34 this.personalProfileService = personalProfileService;
[468b7b6]35 }
36
[6941fac]37 @GetMapping("/create-post")
[468b7b6]38 public String get(Model model) {
[7e3f2f7]39 model.addAttribute("pets", this.petService.listpets());
[468b7b6]40 return "create-post";
[738b31a]41 }
42
[6941fac]43 @PostMapping("/submit-post")
[4103eaa]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,
[468b7b6]51 @RequestParam(required = false) String breed,
52 @RequestParam(required = false) String imageUrl,
[7e3f2f7]53 @RequestParam(required = false) boolean canBeFostered,
54 HttpServletRequest request) {
[468b7b6]55
[3e59177]56 Employee employee = (Employee) request.getSession().getAttribute("user");
[7e3f2f7]57
[4103eaa]58 if(newPetCheckbox == true){
[468b7b6]59
[4103eaa]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);
[738b31a]62
[4103eaa]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 }
[738b31a]74
[c37c953]75 return "redirect:/home";
[738b31a]76 }
[a762b3a]77
78 @GetMapping("/adoption-posts")
79 public String getAdoptionPosts(Model model, HttpServletRequest request){
80
81 List<Post> posts = this.postService.findAll();
[eac569a]82 List<Pet> pets = this.petService.listpets();
[a762b3a]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();
[eac569a]95 Pet pet = this.petService.findById(post.getPetId());
[a762b3a]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
[3c7bf5b]102 if(pet.getAdoptionId() != null){
103 request.getSession().setAttribute("disableAdoption", true);
104 } else{
105 request.getSession().setAttribute("disableAdoption", false);
106 }
107
[a762b3a]108 return "pet-details";
109 }
[738b31a]110}
Note: See TracBrowser for help on using the repository browser.