Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Prototype Application/Paw5/src/main/java/finki/paw5/web/controllers/PostController.java

    r3c7bf5b r4103eaa  
    11package finki.paw5.web.controllers;
    22
    3 import finki.paw5.model.entities.PersonalProfile;
     3import finki.paw5.model.entities.Employee;
    44import finki.paw5.model.entities.Pet;
    55import finki.paw5.model.entities.Post;
    6 import finki.paw5.model.entities.Shelter;
     6import finki.paw5.model.entities.User;
    77import finki.paw5.model.enumerations.AgeGroup;
    88import finki.paw5.model.enumerations.Gender;
    99import finki.paw5.model.enumerations.Size;
    1010import finki.paw5.model.enumerations.Species;
    11 import finki.paw5.service.PersonalProfileService;
    1211import finki.paw5.service.PetService;
    1312import finki.paw5.service.PostService;
    14 import finki.paw5.service.ShelterService;
    1513import jakarta.servlet.http.HttpServletRequest;
    1614import org.springframework.stereotype.Controller;
    1715import org.springframework.ui.Model;
    1816import org.springframework.web.bind.annotation.GetMapping;
    19 import org.springframework.web.bind.annotation.PathVariable;
    2017import org.springframework.web.bind.annotation.PostMapping;
    2118import org.springframework.web.bind.annotation.RequestParam;
    2219
    2320import java.time.LocalDate;
    24 import java.util.List;
    2521
    2622@Controller
     
    2925    private final PostService postService;
    3026    private final PetService petService;
    31     private final PersonalProfileService personalProfileService;
    3227
    33     public PostController(PostService postService, PetService petService, PersonalProfileService personalProfileService, ShelterService shelterService) {
     28    public PostController(PostService postService, PetService petService) {
    3429        this.postService = postService;
    3530        this.petService = petService;
    36         this.personalProfileService = personalProfileService;
    3731    }
    3832
    39     @GetMapping("create-post")
     33    @GetMapping("/create-post")
    4034    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);
     35        model.addAttribute("pets", this.petService.listpets());
    4436        return "create-post";
    4537    }
    4638
    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,
     39    @PostMapping("/submit-post")
     40    public String savePost(@RequestParam(required = false) Integer petId,
     41                           @RequestParam(required = false) boolean newPetCheckbox,
     42                           @RequestParam(required = false) String name,
     43                           @RequestParam(required = false) String gender,
     44                           @RequestParam(required = false) String ageGroup,
     45                           @RequestParam(required = false) String size,
     46                           @RequestParam(required = false) String species,
    5347                           @RequestParam(required = false) String breed,
    5448                           @RequestParam(required = false) String imageUrl,
    55                            @RequestParam(required = false) boolean canBeFostered) {
     49                           @RequestParam(required = false) boolean canBeFostered,
     50                           HttpServletRequest request) {
    5651
    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);
     52        Employee employee = (Employee) request.getSession().getAttribute("user");
    5953
    60         Post post = new Post(LocalDate.now(), imageUrl, pet.getId(), null, 10);//TODO: employee id da se zeme preku session user getid
    61         this.postService.save(post);
     54        if(newPetCheckbox == true){
     55
     56            Pet newPet = new Pet(imageUrl, AgeGroup.valueOf(ageGroup), Size.valueOf(size), breed, name, Species.valueOf(species), Gender.valueOf(gender), canBeFostered, null, employee.getShelterId());
     57            this.petService.save(newPet);
     58
     59            Post post = new Post(LocalDate.now(), imageUrl, newPet.getId(), null, employee.getId());
     60            this.postService.save(post);
     61
     62        } else{
     63
     64            Pet selectedPet = this.petService.findById(petId);
     65
     66            Post post = new Post(LocalDate.now(), imageUrl, selectedPet.getId(), null, employee.getId());
     67            this.postService.save(post);
     68
     69        }
    6270
    6371        return "redirect:/home";
    6472    }
    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 
    90         if(pet.getAdoptionId() != null){
    91             request.getSession().setAttribute("disableAdoption", true);
    92         } else{
    93             request.getSession().setAttribute("disableAdoption", false);
    94         }
    95 
    96         return "pet-details";
    97     }
    9873}
Note: See TracChangeset for help on using the changeset viewer.