From 7da7fc52d8f42e6ea422247cfce66a383927283c Mon Sep 17 00:00:00 2001 From: Crypto-V Date: Thu, 1 Jun 2023 13:02:55 +0100 Subject: [PATCH 1/6] litter-list implemented --- .../model/litter/service/LitterService.java | 33 ++++--- .../littersnap/view/LitterViewController.java | 77 +++++++++++----- src/main/resources/static/style.css | 37 +++++++- src/main/resources/templates/error_page.html | 5 +- src/main/resources/templates/litter_list.html | 92 ++++++++++--------- 5 files changed, 162 insertions(+), 82 deletions(-) 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/view/LitterViewController.java b/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java index d34055b..570cbaf 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,88 @@ 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 + } 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("litter", litter); + return "litter_details"; + } 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 { + try { + LitterCreateOrModifyModel currentLitter = Mapper.mapModelToLitterCreateOrModifyModel(litterService.getLitterById(id)); model.addAttribute("litter", currentLitter); return "litter_edit_form"; + } 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:/reports"; // 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/resources/static/style.css b/src/main/resources/static/style.css index 695437b..9f6f2a3 100644 --- a/src/main/resources/static/style.css +++ b/src/main/resources/static/style.css @@ -62,10 +62,45 @@ 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; + left: 0; + right: 0; + font-family: 'Niagara Solid', cursive; + font-size: 60px; + color: #1c5486; + text-align: center; /* Center the text horizontally */ +} + +.logged-logo .logged-logo-slogan { + position: absolute; + bottom: 1px; + left: 0; + right: 0; + font-family: 'Arial Black', sans-serif; + font-size: 10px; + color: darkmagenta; + text-align: center; /* Center the text horizontally */ +} +.logged-logo .man-with-the-phone-icon { + width: 300px; + opacity: 0.965; } +.fixed-buttons { + position: fixed; + top: 0; + right: 0; +} + + /*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/litter_list.html b/src/main/resources/templates/litter_list.html index 6e00ce7..22247b7 100644 --- a/src/main/resources/templates/litter_list.html +++ b/src/main/resources/templates/litter_list.html @@ -63,62 +63,62 @@
- - - - - - - - - - - - -
-
- - - - - - - - -
+
+
DateByAddressPostCodeStatusAction
-
- View - Edit - Delete -
-
+ - + + + + + + - - -
-

No data

-
DateByAddressPostCodeStatusAction
+ + +
+ + + + + + + +
+ View + Edit + Delete +
+ + +
+
+ + + + + +
+ + +
- - -
-
-
+
+
+ From 739ec5b4f72f084a80bd6328a0e548b20c845373 Mon Sep 17 00:00:00 2001 From: Crypto-V Date: Thu, 1 Jun 2023 16:10:22 +0100 Subject: [PATCH 2/6] before converting the litterdetails to a fragment --- .../littersnap/view/LitterViewController.java | 10 +- src/main/resources/static/style.css | 24 +---- .../resources/templates/litter_details.html | 39 ++++---- src/main/resources/templates/litter_list.html | 95 ++++++------------- 4 files changed, 59 insertions(+), 109 deletions(-) diff --git a/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java b/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java index 570cbaf..6bde19b 100644 --- a/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java +++ b/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java @@ -42,7 +42,15 @@ public String getLitterById(@PathVariable("id") UUID id, Model model) { try { LitterModel litter = litterService.getLitterById(id); - model.addAttribute("litter", litter); + 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()); return "litter_details"; } catch (NoSuchElementException e) { model.addAttribute("errorMessage", e.getMessage()); diff --git a/src/main/resources/static/style.css b/src/main/resources/static/style.css index 9f6f2a3..6e2524f 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; } @@ -70,36 +70,18 @@ body { .logged-logo .logged-logo-name { position: absolute; bottom: 20px; - left: 0; - right: 0; + right: 305px; font-family: 'Niagara Solid', cursive; - font-size: 60px; + font-size: 80px; color: #1c5486; text-align: center; /* Center the text horizontally */ } -.logged-logo .logged-logo-slogan { - position: absolute; - bottom: 1px; - left: 0; - right: 0; - font-family: 'Arial Black', sans-serif; - font-size: 10px; - color: darkmagenta; - text-align: center; /* Center the text horizontally */ -} - .logged-logo .man-with-the-phone-icon { width: 300px; opacity: 0.965; } -.fixed-buttons { - position: fixed; - top: 0; - right: 0; -} - /*Modal Container on index page*/ diff --git a/src/main/resources/templates/litter_details.html b/src/main/resources/templates/litter_details.html index b93c265..027d725 100644 --- a/src/main/resources/templates/litter_details.html +++ b/src/main/resources/templates/litter_details.html @@ -11,32 +11,33 @@

- - +

Created At:

+

- - +

Updated At:

+

- - +

Street:

+

+

City:

+

+

Country:

+

+

PostCode:

+

- - +

Description:

+

- - - - - - - - - - - +

Image:

+

+

Status:

+

+
+ diff --git a/src/main/resources/templates/litter_list.html b/src/main/resources/templates/litter_list.html index 22247b7..43a282c 100644 --- a/src/main/resources/templates/litter_list.html +++ b/src/main/resources/templates/litter_list.html @@ -1,49 +1,21 @@ - - - Title - - - - - - - - - - - - - - - - - - + - - - - - - - + + Litter Snap -
-
+
Welcome,
@@ -107,37 +74,30 @@
-
-
+
- -
- - - - From 2ff882ec2c57057013afede9d01356cace561704 Mon Sep 17 00:00:00 2001 From: Crypto-V Date: Thu, 1 Jun 2023 18:24:21 +0100 Subject: [PATCH 3/6] before converting the litterdetails to a fragment --- src/main/resources/templates/litter_list.html | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/resources/templates/litter_list.html b/src/main/resources/templates/litter_list.html index 43a282c..852d100 100644 --- a/src/main/resources/templates/litter_list.html +++ b/src/main/resources/templates/litter_list.html @@ -32,6 +32,8 @@
+
+
From 7a7ee92bfeafa7257d8c6f4ec892948d3c30bc48 Mon Sep 17 00:00:00 2001 From: Crypto-V Date: Thu, 1 Jun 2023 18:35:53 +0100 Subject: [PATCH 4/6] fixed bug with status --- src/main/java/com/csaba79coder/littersnap/util/Mapper.java | 1 + 1 file changed, 1 insertion(+) 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; } From 6cc7d626176862fb1199f3e7579a1bab3c1fbcb3 Mon Sep 17 00:00:00 2001 From: Crypto-V Date: Thu, 1 Jun 2023 18:39:03 +0100 Subject: [PATCH 5/6] fixed bug with name --- src/main/resources/templates/litter_list.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/templates/litter_list.html b/src/main/resources/templates/litter_list.html index 852d100..236a964 100644 --- a/src/main/resources/templates/litter_list.html +++ b/src/main/resources/templates/litter_list.html @@ -17,7 +17,7 @@
-
Welcome,
+
Welcome,
- - - - - -
-
-
-
diff --git a/src/main/resources/templates/litter_details.html b/src/main/resources/templates/litter_details.html index 027d725..1cdaebb 100644 --- a/src/main/resources/templates/litter_details.html +++ b/src/main/resources/templates/litter_details.html @@ -1,43 +1,38 @@ -