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

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

session has user attribute, not employee

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[738b31a]1package finki.paw5.web.controllers;
2
[7e3f2f7]3import finki.paw5.model.entities.Employee;
[468b7b6]4import finki.paw5.model.entities.Pet;
5import finki.paw5.model.entities.Post;
[7e3f2f7]6import finki.paw5.model.entities.User;
[738b31a]7import finki.paw5.model.enumerations.AgeGroup;
8import finki.paw5.model.enumerations.Gender;
9import finki.paw5.model.enumerations.Size;
10import finki.paw5.model.enumerations.Species;
[468b7b6]11import finki.paw5.service.PetService;
[738b31a]12import finki.paw5.service.PostService;
[7e3f2f7]13import jakarta.servlet.http.HttpServletRequest;
[738b31a]14import org.springframework.stereotype.Controller;
[468b7b6]15import org.springframework.ui.Model;
16import org.springframework.web.bind.annotation.GetMapping;
[738b31a]17import org.springframework.web.bind.annotation.PostMapping;
18import org.springframework.web.bind.annotation.RequestParam;
19
[468b7b6]20import java.time.LocalDate;
21
[738b31a]22@Controller
23public class PostController {
24
25 private final PostService postService;
[468b7b6]26 private final PetService petService;
[738b31a]27
[468b7b6]28 public PostController(PostService postService, PetService petService) {
[738b31a]29 this.postService = postService;
[468b7b6]30 this.petService = petService;
31 }
32
[6941fac]33 @GetMapping("/create-post")
[468b7b6]34 public String get(Model model) {
[7e3f2f7]35 model.addAttribute("pets", this.petService.listpets());
[468b7b6]36 return "create-post";
[738b31a]37 }
38
[6941fac]39 @PostMapping("/submit-post")
[468b7b6]40 public String savePost(@RequestParam(required = false) String name,
41 @RequestParam String gender,
42 @RequestParam String ageGroup,
43 @RequestParam String size,
44 @RequestParam String species,
45 @RequestParam(required = false) String breed,
46 @RequestParam(required = false) String imageUrl,
[7e3f2f7]47 @RequestParam(required = false) boolean canBeFostered,
48 HttpServletRequest request) {
[468b7b6]49
[3e59177]50 Employee employee = (Employee) request.getSession().getAttribute("user");
[7e3f2f7]51
52 Pet pet = new Pet(imageUrl, AgeGroup.valueOf(ageGroup), Size.valueOf(size), breed, name, Species.valueOf(species), Gender.valueOf(gender), canBeFostered, null, employee.getShelterId());
[468b7b6]53 this.petService.save(pet);
[738b31a]54
[3e59177]55 Post post = new Post(LocalDate.now(), imageUrl, pet.getId(), null, employee.getId());
[468b7b6]56 this.postService.save(post);
[738b31a]57
[c37c953]58 return "redirect:/home";
[738b31a]59 }
60}
Note: See TracBrowser for help on using the repository browser.