Changeset 9a180fd


Ignore:
Timestamp:
02/16/23 14:45:38 (21 months ago)
Author:
SazdovaEkaterina <sazdovaekaterina@…>
Branches:
main
Children:
3c7bf5b
Parents:
50f2c2a
Message:

adopt a pet button + save adoption to db

Location:
Prototype Application/Paw5/src/main
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • Prototype Application/Paw5/src/main/java/finki/paw5/model/entities/Adoption.java

    r50f2c2a r9a180fd  
    55
    66import java.time.LocalDate;
    7 import java.util.Date;
    87
    98@Data
     
    2726
    2827    @Column(name = "id_adopter", nullable = false)
    29     private int adopterId;
     28    private Integer adopterId;
    3029
    31     public Adoption(LocalDate startDate, LocalDate endDateFoster, boolean approved, int adopterId) {
     30    public Adoption(LocalDate startDate, LocalDate endDateFoster, boolean approved, Integer adopterId) {
    3231        this.startDate = startDate;
    3332        this.endDateFoster = endDateFoster;
  • Prototype Application/Paw5/src/main/java/finki/paw5/web/controllers/PetController.java

    r50f2c2a r9a180fd  
    11package finki.paw5.web.controllers;
    22
     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;
    38import finki.paw5.service.PetService;
     9import jakarta.servlet.http.HttpServletRequest;
    410import org.springframework.stereotype.Controller;
     11import org.springframework.web.bind.annotation.PathVariable;
     12import org.springframework.web.bind.annotation.PostMapping;
     13
     14import java.time.LocalDate;
    515
    616@Controller
     
    818
    919    private final PetService petService;
     20    private final AdoptionService adoptionService;
    1021
    11     public PetController(PetService petService) {
     22    public PetController(PetService petService, AdoptionService adoptionService) {
    1223        this.petService = petService;
     24        this.adoptionService = adoptionService;
    1325    }
    1426
     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    }
    1542}
  • Prototype Application/Paw5/src/main/resources/templates/pet-details.html

    r50f2c2a r9a180fd  
    3737        <td th:text = "${session.pet.getSize()}"></td>
    3838    </tr>
    39 
    4039</table>
     40<form method="POST" th:action="@{'/submit-adopton-{id}' (id=${session.pet.getId()})}" th:disabled="${session.pet.getAdoptionId()==null}">
     41    <button id="submit" type="submit">Adopt</button>
     42</form>
    4143
    4244</body>
Note: See TracChangeset for help on using the changeset viewer.