1 | package finki.paw5.web.controllers;
|
---|
2 |
|
---|
3 | import finki.paw5.model.entities.Employee;
|
---|
4 | import finki.paw5.model.entities.Pet;
|
---|
5 | import finki.paw5.model.entities.Post;
|
---|
6 | import finki.paw5.model.entities.User;
|
---|
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;
|
---|
11 | import finki.paw5.service.PetService;
|
---|
12 | import finki.paw5.service.PostService;
|
---|
13 | import jakarta.servlet.http.HttpServletRequest;
|
---|
14 | import org.springframework.stereotype.Controller;
|
---|
15 | import org.springframework.ui.Model;
|
---|
16 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
17 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
18 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
19 |
|
---|
20 | import java.time.LocalDate;
|
---|
21 |
|
---|
22 | @Controller
|
---|
23 | public class PostController {
|
---|
24 |
|
---|
25 | private final PostService postService;
|
---|
26 | private final PetService petService;
|
---|
27 |
|
---|
28 | public PostController(PostService postService, PetService petService) {
|
---|
29 | this.postService = postService;
|
---|
30 | this.petService = petService;
|
---|
31 | }
|
---|
32 |
|
---|
33 | @GetMapping("/create-post")
|
---|
34 | public String get(Model model) {
|
---|
35 | model.addAttribute("pets", this.petService.listpets());
|
---|
36 | return "create-post";
|
---|
37 | }
|
---|
38 |
|
---|
39 | @PostMapping("/submit-post")
|
---|
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,
|
---|
47 | @RequestParam(required = false) boolean canBeFostered,
|
---|
48 | HttpServletRequest request) {
|
---|
49 |
|
---|
50 | Employee employee = (Employee) request.getSession().getAttribute("employee");
|
---|
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());
|
---|
53 | this.petService.save(pet);
|
---|
54 |
|
---|
55 | Post post = new Post(LocalDate.now(), imageUrl, pet.getId() , null, employee.getId() );
|
---|
56 | this.postService.save(post);
|
---|
57 |
|
---|
58 | return "redirect:/home";
|
---|
59 | }
|
---|
60 | }
|
---|