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