source: Prototype Application/Paw5/src/main/java/finki/paw5/web/controllers/PetController.java@ 9a180fd

main
Last change on this file since 9a180fd was 9a180fd, checked in by SazdovaEkaterina <sazdovaekaterina@…>, 17 months ago

adopt a pet button + save adoption to db

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[4885da3]1package finki.paw5.web.controllers;
2
[9a180fd]3import finki.paw5.model.entities.Adoption;
4import finki.paw5.model.entities.Pet;
5import finki.paw5.model.entities.User;
6import finki.paw5.model.exceptions.InvalidPetIdException;
7import finki.paw5.service.AdoptionService;
[4885da3]8import finki.paw5.service.PetService;
[9a180fd]9import jakarta.servlet.http.HttpServletRequest;
[4885da3]10import org.springframework.stereotype.Controller;
[9a180fd]11import org.springframework.web.bind.annotation.PathVariable;
12import org.springframework.web.bind.annotation.PostMapping;
13
14import java.time.LocalDate;
[4885da3]15
16@Controller
17public class PetController {
18
19 private final PetService petService;
[9a180fd]20 private final AdoptionService adoptionService;
[4885da3]21
[9a180fd]22 public PetController(PetService petService, AdoptionService adoptionService) {
[4885da3]23 this.petService = petService;
[9a180fd]24 this.adoptionService = adoptionService;
[4885da3]25 }
26
[9a180fd]27 @PostMapping("/submit-adopton-{id}")
28 public String saveAdoption(@PathVariable Integer id, HttpServletRequest request) {
29
30 Pet pet = this.petService.findById(id).orElseThrow(InvalidPetIdException::new);
31
32 User user = (User) request.getSession().getAttribute("user");
33
34 Adoption adoption = new Adoption(LocalDate.now(), null, false, user.getId());
35 this.adoptionService.save(adoption);
36
37 pet.setAdoptionId(adoption.getId());
38 this.petService.save(pet);
39
40 return "redirect:/home";
41 }
[4885da3]42}
Note: See TracBrowser for help on using the repository browser.