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

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

fix usages that got broken with the changes

  • Property mode set to 100644
File size: 3.8 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
[e76c5a6]60 Pet newPet = new Pet(imageUrl, AgeGroup.valueOf(ageGroup), Size.valueOf(size), breed, name, Species.valueOf(species), Gender.valueOf(gender), canBeFostered, null, employee.getShelter());
[4103eaa]61 this.petService.save(newPet);
[738b31a]62
[e76c5a6]63 Post post = new Post(LocalDate.now(), imageUrl, newPet, null, employee);
[4103eaa]64 this.postService.save(post);
65
66 } else{
67
68 Pet selectedPet = this.petService.findById(petId);
69
[e76c5a6]70 Post post = new Post(LocalDate.now(), imageUrl, selectedPet, null, employee);
[4103eaa]71 this.postService.save(post);
72
73 }
[738b31a]74
[c37c953]75 return "redirect:/home";
[738b31a]76 }
[a762b3a]77
78 @GetMapping("/adoption-posts")
[5f53114]79 public String getAdoptionPosts(Model model){
[a762b3a]80
81 List<Post> posts = this.postService.findAll();
[eac569a]82 List<Pet> pets = this.petService.listpets();
[5f53114]83 model.addAttribute("posts", posts);
84 model.addAttribute("pets",pets);
[a762b3a]85
86 return "list-posts-adoption";
87 }
88
89 @GetMapping("/pet-details-{id}")
[5f53114]90 public String getPostDetails(@PathVariable Integer id,
91 Model model){
[a762b3a]92
93 Post post = this.postService.findById(id).get();
[e76c5a6]94 Pet pet = post.getPet();
[a762b3a]95
[5f53114]96 model.addAttribute("pet", pet);
97 model.addAttribute("post", post);
[3c7bf5b]98
[a762b3a]99 return "pet-details";
100 }
[738b31a]101}
Note: See TracBrowser for help on using the repository browser.