1 | package finki.paw5.web.controllers;
|
---|
2 |
|
---|
3 | import finki.paw5.model.entities.Adopter;
|
---|
4 | import finki.paw5.model.entities.Adoption;
|
---|
5 | import finki.paw5.model.entities.Pet;
|
---|
6 | import finki.paw5.service.AdoptionService;
|
---|
7 | import finki.paw5.service.PetService;
|
---|
8 | import jakarta.servlet.http.HttpServletRequest;
|
---|
9 | import org.springframework.stereotype.Controller;
|
---|
10 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
11 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
12 |
|
---|
13 | import java.time.LocalDate;
|
---|
14 |
|
---|
15 | @Controller
|
---|
16 | public class PetController {
|
---|
17 |
|
---|
18 | private final PetService petService;
|
---|
19 | private final AdoptionService adoptionService;
|
---|
20 |
|
---|
21 | public PetController(PetService petService, AdoptionService adoptionService) {
|
---|
22 | this.petService = petService;
|
---|
23 | this.adoptionService = adoptionService;
|
---|
24 | }
|
---|
25 |
|
---|
26 | @PostMapping("/submit-adoption-{id}")
|
---|
27 | public String saveAdoption(@PathVariable Integer id, HttpServletRequest request) {
|
---|
28 |
|
---|
29 | Pet pet = this.petService.findById(id);
|
---|
30 |
|
---|
31 | Adopter adopter = (Adopter) request.getSession().getAttribute("user");
|
---|
32 |
|
---|
33 | Adoption adoption = new Adoption(LocalDate.now(), null, false, adopter);
|
---|
34 | this.adoptionService.save(adoption);
|
---|
35 |
|
---|
36 | pet.setAdoption(adoption);
|
---|
37 | this.petService.save(pet);
|
---|
38 |
|
---|
39 | return "redirect:/home";
|
---|
40 | }
|
---|
41 | }
|
---|