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