diff --git a/src/main/java/com/csaba79coder/littersnap/model/litter/dto/LitterCreateOrModifyModel.java b/src/main/java/com/csaba79coder/littersnap/model/litter/dto/LitterCreateOrModifyModel.java index 11c51d5..168bf35 100644 --- a/src/main/java/com/csaba79coder/littersnap/model/litter/dto/LitterCreateOrModifyModel.java +++ b/src/main/java/com/csaba79coder/littersnap/model/litter/dto/LitterCreateOrModifyModel.java @@ -6,12 +6,15 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import java.util.UUID; + @Getter @Setter @AllArgsConstructor @NoArgsConstructor public class LitterCreateOrModifyModel { + private UUID id; private AddressModel address; private String description; private byte[] image; diff --git a/src/main/java/com/csaba79coder/littersnap/model/litter/service/LitterService.java b/src/main/java/com/csaba79coder/littersnap/model/litter/service/LitterService.java index b210efd..8e4b3ab 100644 --- a/src/main/java/com/csaba79coder/littersnap/model/litter/service/LitterService.java +++ b/src/main/java/com/csaba79coder/littersnap/model/litter/service/LitterService.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.util.List; +import java.util.NoSuchElementException; import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; @@ -28,11 +29,16 @@ public class LitterService { private final AddressRepository addressRepository; - //Image i will need @RequestParam("image") MultipartFile file - - public List getAllLitters() { List litters = litterRepository.findAll(); + + + if (litters.isEmpty()) { + String message = "Litter list is empty"; + log.error(message); + throw new NoSuchElementException(message); + } + return litters.stream() .map(litter -> { LitterModel litterModel = Mapper.mapLitterEntityToModel(litter); @@ -52,12 +58,14 @@ public LitterModel addNewLitter(LitterCreateOrModifyModel litterModel, Address a address = addressRepository.save(address); } Litter litterEntity = new Litter(); - litterEntity.setAddress(address); //TODO set an incoming address also here! if address is not found in entiy new must be created + litterEntity.setAddress(address); litterEntity.setDescription(litterModel.getDescription()); try { litterEntity.setImage(ImageUtil.compressImage(file.getBytes())); // Compressing the image before saving } catch (IOException e) { - throw new RuntimeException(e); + String message = String.format("File not found."+ e.getMessage()); + log.error(message); + throw new NoSuchElementException(e); } Litter savedLitterEntity = litterRepository.save(litterEntity); return Mapper.mapLitterEntityToModel(savedLitterEntity); @@ -74,7 +82,7 @@ public LitterModel getLitterById(UUID id) { } else { String message = String.format("Litter with id %s not found", id); log.error(message); - throw new RuntimeException(message); + throw new NoSuchElementException(message); } } @@ -94,10 +102,12 @@ public LitterModel updateExistingLitter(UUID id, LitterCreateOrModifyModel model Litter updatedLitter = litterRepository.save(Mapper.mapLitterModelToEntity(existingLitter)); return Mapper.mapLitterEntityToModel(updatedLitter); + }else { + String message = String.format("Couldn't update. Litter with id %s not found", id); + log.error(message); + throw new NoSuchElementException(message); } - //TODO - //Case where liter not found - return null; + } public void deleteLitter(UUID id) { @@ -105,8 +115,9 @@ public void deleteLitter(UUID id) { if (optionalExistingLitter.isPresent()) { litterRepository.deleteById(id); } else { - //TODO - //Case where liter not found + String message = String.format("Couldn't delete. Litter with id %s not found", id); + log.error(message); + throw new NoSuchElementException(message); } } } diff --git a/src/main/java/com/csaba79coder/littersnap/model/user/dto/UserModel.java b/src/main/java/com/csaba79coder/littersnap/model/user/dto/UserModel.java index 291de1a..282cc4e 100644 --- a/src/main/java/com/csaba79coder/littersnap/model/user/dto/UserModel.java +++ b/src/main/java/com/csaba79coder/littersnap/model/user/dto/UserModel.java @@ -23,4 +23,5 @@ public class UserModel { private String email; private String firstName; private Role role; -} \ No newline at end of file + +} diff --git a/src/main/java/com/csaba79coder/littersnap/util/Mapper.java b/src/main/java/com/csaba79coder/littersnap/util/Mapper.java index c390f3d..7672830 100644 --- a/src/main/java/com/csaba79coder/littersnap/util/Mapper.java +++ b/src/main/java/com/csaba79coder/littersnap/util/Mapper.java @@ -56,6 +56,7 @@ public static LitterModel mapLitterEntityToModel(Litter entity) { model.setImage(ImageUtil.decompressImage(entity.getImage())); model.setAddress(mapAddressEntityToModel(entity.getAddress())); model.setDescription(entity.getDescription()); + model.setStatus(entity.getStatus()); return model; } diff --git a/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java b/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java index d34055b..644a033 100644 --- a/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java +++ b/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java @@ -11,6 +11,7 @@ import org.springframework.web.multipart.MultipartFile; import java.util.List; +import java.util.NoSuchElementException; import java.util.UUID; @Controller @@ -26,58 +27,106 @@ public LitterViewController(LitterService litterService) { @GetMapping public String getAllLitters(Model model) { - List litters = litterService.getAllLitters(); - - if (litters.isEmpty()) { - return "error_page"; - } else { + try { + List litters = litterService.getAllLitters(); model.addAttribute("litters", litters); - return "litter_list"; // Replace with the actual view name for displaying the list of litters + model.addAttribute("view", "litter_list"); + return "welcome"; // Replace with the actual view name for displaying the list of litters + } catch (NoSuchElementException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "error_page"; // Redirect to the error page to display the error message } } @GetMapping("/{id}") public String getLitterById(@PathVariable("id") UUID id, Model model) { - LitterModel litter = litterService.getLitterById(id); - model.addAttribute("litter", litter); - return "litter_details"; + + try { + LitterModel litter = litterService.getLitterById(id); + model.addAttribute("id", litter.getId()); + model.addAttribute("createdAt", litter.getCreatedAt()); + model.addAttribute("updatedAt", litter.getUpdatedAt()); + model.addAttribute("firstline", litter.getAddress().getFirstLine()); + model.addAttribute("city", litter.getAddress().getCity()); + model.addAttribute("country", litter.getAddress().getCountry()); + model.addAttribute("postcode", litter.getAddress().getPostCode()); + model.addAttribute("description", litter.getDescription()); + model.addAttribute("image", litter.getImage()); + model.addAttribute("status", litter.getStatus()); + model.addAttribute("view", "litter_details"); + return "welcome"; + } catch (NoSuchElementException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "error_page"; // Redirect to the error page to display the error message + } } @GetMapping("/create") public String showAddLitterForm(Model model) { - model.addAttribute("litter", new LitterCreateOrModifyModel()); - return "litter_add_form"; + try { + model.addAttribute("litter", new LitterCreateOrModifyModel()); + return "litter_add_form"; + } catch (NoSuchElementException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "error_page"; // Redirect to the error page to display the error message + } } @PostMapping("/create") public String addNewLitter(@ModelAttribute("litter") LitterCreateOrModifyModel litterModel, @ModelAttribute("address") Address address, - @RequestParam("file") MultipartFile file) { - litterService.addNewLitter(litterModel, address, file); - return "redirect:/thy/litter"; + @RequestParam("file") MultipartFile file,Model model) { + try { + litterService.addNewLitter(litterModel, address, file); + return "redirect:/thy/litter"; + } catch (NoSuchElementException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "error_page"; // Redirect to the error page to display the error message + } } @GetMapping("/edit/{id}") public String showEditForm(@PathVariable UUID id, Model model) { - LitterCreateOrModifyModel currentLitter = Mapper.mapModelToLitterCreateOrModifyModel(litterService.getLitterById(id)); - if (currentLitter == null) { - return "error_page"; - } else { - model.addAttribute("litter", currentLitter); - return "litter_edit_form"; + try { + LitterCreateOrModifyModel litter = Mapper.mapModelToLitterCreateOrModifyModel(litterService.getLitterById(id)); + model.addAttribute("id", litter.getId()); + model.addAttribute("firstline", litter.getAddress().getFirstLine()); + model.addAttribute("city", litter.getAddress().getCity()); + model.addAttribute("country", litter.getAddress().getCountry()); + model.addAttribute("postcode", litter.getAddress().getPostCode()); + model.addAttribute("description", litter.getDescription()); + model.addAttribute("image", litter.getImage()); + model.addAttribute("view","litter_edit_form"); + return "welcome"; + } catch (NoSuchElementException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "error_page"; // Redirect to the error page to display the error message } } @PostMapping("/edit/{id}") - public String updateLitter(@PathVariable UUID id, @ModelAttribute("report") LitterCreateOrModifyModel litterModel) { - litterService.updateExistingLitter(id,litterModel); - return "redirect:/reports"; // Redirect to the URL for displaying all reports after successful update + public String updateLitter(@PathVariable UUID id, @ModelAttribute("report") LitterCreateOrModifyModel litterModel, Model model) { + try { + litterService.updateExistingLitter(id, litterModel); + return "redirect:/reports"; // Redirect to the URL for displaying all reports after successful update + } catch (NoSuchElementException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "error_page"; // Redirect to the error page to display the error message + } } + @GetMapping("/delete/{id}") - public String deleteLitter(@PathVariable UUID id) { - litterService.deleteLitter(id); - return "redirect:/reports"; // Redirect to the URL for displaying all reports after successful deletion + public String deleteLitter(@PathVariable UUID id,Model model) { + + try { + litterService.deleteLitter(id); + return "redirect:/thy/litter"; // Redirect to the URL for displaying all reports after successful deletion + } catch (NoSuchElementException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "error_page"; // Redirect to the error page to display the error message + } + } diff --git a/src/main/java/com/csaba79coder/littersnap/view/UserViewController.java b/src/main/java/com/csaba79coder/littersnap/view/UserViewController.java index 29b9411..33d9873 100644 --- a/src/main/java/com/csaba79coder/littersnap/view/UserViewController.java +++ b/src/main/java/com/csaba79coder/littersnap/view/UserViewController.java @@ -1,12 +1,14 @@ package com.csaba79coder.littersnap.view; +import com.csaba79coder.littersnap.model.user.dto.UserModel; import com.csaba79coder.littersnap.model.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import java.util.List; +import java.util.NoSuchElementException; + @Controller @RequiredArgsConstructor @RequestMapping("/thy/users") @@ -15,8 +17,15 @@ public class UserViewController { private final UserService userService; @GetMapping - public String renderAllUsers(Model model) { - model.addAttribute("users", userService.findAllUsers()); - return "user"; + public String getAllUsers(Model model) { + try { + List users = userService.findAllUsers(); + model.addAttribute("users", users); + model.addAttribute("view", "user_list"); + return "welcome"; // Replace with the actual view name for displaying the list of litters + } catch (NoSuchElementException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "error_page"; // Redirect to the error page to display the error message + } } } \ No newline at end of file diff --git a/src/main/resources/static/style.css b/src/main/resources/static/style.css index 54c038d..f492786 100644 --- a/src/main/resources/static/style.css +++ b/src/main/resources/static/style.css @@ -20,7 +20,7 @@ body { .fixed-buttons { position: fixed; top: 0; - right: 20px; + right: 0; z-index: 999; } @@ -62,10 +62,27 @@ body { width: 100%; } -.input-label-logo-welcome-page { +/*Logo Control when user will be logged in*/ +.logged-logo { + position: relative; +} +.logged-logo .logged-logo-name { + position: absolute; + bottom: 20px; + right: 305px; + font-family: 'Niagara Solid', cursive; + font-size: 80px; + color: #1c5486; + text-align: center; /* Center the text horizontally */ } +.logged-logo .man-with-the-phone-icon { + width: 300px; + opacity: 0.965; +} + + /*Modal Container on index page*/ /*.modal-dialog {*/ diff --git a/src/main/resources/templates/error_page.html b/src/main/resources/templates/error_page.html index 869e3ad..2aa3bc8 100644 --- a/src/main/resources/templates/error_page.html +++ b/src/main/resources/templates/error_page.html @@ -16,10 +16,11 @@ - +
-

404 - Page Not Found

+

Error Page


+

Vasile, Csaba and Kev are on it now...

Image
diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 2446132..422475a 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -34,25 +34,10 @@ - - - - - -
-
-
-
diff --git a/src/main/resources/templates/litter_details.html b/src/main/resources/templates/litter_details.html index b93c265..1cdaebb 100644 --- a/src/main/resources/templates/litter_details.html +++ b/src/main/resources/templates/litter_details.html @@ -1,42 +1,38 @@ -