Changeset 4103eaa


Ignore:
Timestamp:
03/04/23 19:29:36 (21 months ago)
Author:
SazdovaEkaterina <sazdovaekaterina@…>
Branches:
main
Children:
f194b4e
Parents:
8f1a362
Message:

create post for existing pet + disable form if selecting from list

Location:
Prototype Application/Paw5/src/main
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • Prototype Application/Paw5/src/main/java/finki/paw5/service/PetService.java

    r8f1a362 r4103eaa  
    1010
    1111    List<Pet> listpets();
     12
     13    Pet findById(Integer id);
    1214}
  • Prototype Application/Paw5/src/main/java/finki/paw5/service/implementation/PetServiceImplementation.java

    r8f1a362 r4103eaa  
    2424    @Override
    2525    public List<Pet> listpets() {return this.petRepository.findAll();}
     26
     27    @Override
     28    public Pet findById(Integer id) {
     29        return this.petRepository.findById(id).get();
     30    }
    2631}
  • Prototype Application/Paw5/src/main/java/finki/paw5/web/controllers/PostController.java

    r8f1a362 r4103eaa  
    3838
    3939    @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,
     40    public String savePost(@RequestParam(required = false) Integer petId,
     41                           @RequestParam(required = false) boolean newPetCheckbox,
     42                           @RequestParam(required = false) String name,
     43                           @RequestParam(required = false) String gender,
     44                           @RequestParam(required = false) String ageGroup,
     45                           @RequestParam(required = false) String size,
     46                           @RequestParam(required = false) String species,
    4547                           @RequestParam(required = false) String breed,
    4648                           @RequestParam(required = false) String imageUrl,
     
    5052        Employee employee = (Employee) request.getSession().getAttribute("user");
    5153
    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        if(newPetCheckbox == true){
    5455
    55         Post post = new Post(LocalDate.now(), imageUrl, pet.getId(), null, employee.getId());
    56         this.postService.save(post);
     56            Pet newPet = new Pet(imageUrl, AgeGroup.valueOf(ageGroup), Size.valueOf(size), breed, name, Species.valueOf(species), Gender.valueOf(gender), canBeFostered, null, employee.getShelterId());
     57            this.petService.save(newPet);
     58
     59            Post post = new Post(LocalDate.now(), imageUrl, newPet.getId(), null, employee.getId());
     60            this.postService.save(post);
     61
     62        } else{
     63
     64            Pet selectedPet = this.petService.findById(petId);
     65
     66            Post post = new Post(LocalDate.now(), imageUrl, selectedPet.getId(), null, employee.getId());
     67            this.postService.save(post);
     68
     69        }
    5770
    5871        return "redirect:/home";
  • Prototype Application/Paw5/src/main/resources/templates/create-post.html

    r8f1a362 r4103eaa  
    1111    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    1212    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
     13    <script>
     14        function addNewPet(addNewPetCheckBox){
     15
     16            if(addNewPetCheckBox.checked){
     17
     18                document.getElementById("name").disabled = false;
     19                document.getElementById("gender").disabled = false;
     20                document.getElementById("ageGroup").disabled = false;
     21                document.getElementById("size").disabled = false;
     22                document.getElementById("species").disabled = false;
     23                document.getElementById("breed").disabled = false;
     24                document.getElementById("imageUrl").disabled = false;
     25                document.getElementById("canBeFostered").disabled = false;
     26
     27                document.getElementById("petId").disabled = true;
     28            } else{
     29
     30                document.getElementById("name").disabled = true;
     31                document.getElementById("gender").disabled = true;
     32                document.getElementById("ageGroup").disabled = true;
     33                document.getElementById("size").disabled = true;
     34                document.getElementById("species").disabled = true;
     35                document.getElementById("breed").disabled = true;
     36                document.getElementById("imageUrl").disabled = true;
     37                document.getElementById("canBeFostered").disabled = true;
     38
     39                document.getElementById("petId").disabled = false;
     40
     41            }
     42        }
     43    </script>
    1344</head>
    1445<body>
     
    4273
    4374    <div>
    44         <label for="pet">Select pet:</label>
    45         <select id="pet">
     75        <label for="petId">Select pet:</label>
     76        <select id="petId" name="petId">
    4677            <option
    4778            th:each="pet :${pets}"
    48             th:text="${pet.getName()}">
     79            th:text="${pet.getName()}"
     80            th:value="${pet.getId()}">
    4981            </option>
    5082        </select>
     
    5284
    5385    <div>
    54         <label for="newpet">Add new pet:</label>
    55             <input id="newpet" name="newpet" placeholder="newpet" type="checkbox">
     86        <label for="newPetCheckbox">Add new pet:</label>
     87            <input id="newPetCheckbox" name="newPetCheckbox" type="checkbox" onclick="addNewPet(this)">
    5688    </div>
    5789
     
    6294               name="name"
    6395               class="form-control"
    64                placeholder="Enter name">
     96               placeholder="Enter name"
     97               disabled>
    6598    </div>
    6699
     
    69102        <select id="gender"
    70103                name="gender"
    71                 class="form-control">
     104                class="form-control"
     105                disabled>
    72106            <option value = "MALE">male</option>
    73107            <option value = "FEMALE">female</option>
     
    79113        <select id="ageGroup"
    80114                name="ageGroup"
    81                 class="form-control">
     115                class="form-control"
     116                disabled>
    82117            <option value = "YOUNG">young</option>
    83118            <option value = "ADULT">adult</option>
     
    90125        <select id="size"
    91126                name="size"
    92                 class="form-control">
     127                class="form-control"
     128                disabled>
    93129            <option value = "XSMALL">extra small</option>
    94130            <option value = "SMALL">small</option>
     
    103139        <select id="species"
    104140                name="species"
    105                 class="form-control">
     141                class="form-control"
     142                disabled>
    106143            <option value = "CAT">cat</option>
    107144            <option value = "DOG">dog</option>
     
    116153               name="breed"
    117154               class="form-control"
    118                placeholder="Enter breed" >
     155               placeholder="Enter breed"
     156               disabled>
    119157    </div>
    120158
     
    125163               name="imageUrl"
    126164               class="form-control"
    127                placeholder="Enter image URL">
     165               placeholder="Enter image URL"
     166               disabled>
    128167        <!-- <label for="upload">Image:</label>
    129168        <input id="upload" type="file" accept="image/*">
     
    137176               name="canBeFostered"
    138177               class="form-control"
    139                value=false>
     178               value=false
     179               disabled>
    140180    </div>
    141181
Note: See TracChangeset for help on using the changeset viewer.