From 8b8cb7efe31cdec636982c41f7cf521f019ef8bf Mon Sep 17 00:00:00 2001 From: Csaba79-coder Date: Thu, 1 Jun 2023 22:46:05 +0200 Subject: [PATCH 1/2] import --- .../com/csaba79coder/littersnap/view/UserViewController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/csaba79coder/littersnap/view/UserViewController.java b/src/main/java/com/csaba79coder/littersnap/view/UserViewController.java index 33d9873..2768007 100644 --- a/src/main/java/com/csaba79coder/littersnap/view/UserViewController.java +++ b/src/main/java/com/csaba79coder/littersnap/view/UserViewController.java @@ -4,6 +4,8 @@ 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; From 3a1fb1915efa85a465a64dedac7d1829a965dbb2 Mon Sep 17 00:00:00 2001 From: Csaba79-coder Date: Fri, 2 Jun 2023 00:44:27 +0200 Subject: [PATCH 2/2] render photo from database on thymeleaf --- .../littersnap/view/LitterViewController.java | 24 +++++++++++++++---- .../resources/templates/litter_details.html | 2 +- src/main/resources/templates/welcome.html | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java b/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java index 644a033..414c33f 100644 --- a/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java +++ b/src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java @@ -7,9 +7,15 @@ import com.csaba79coder.littersnap.util.Mapper; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; +import java.util.Base64; import java.util.List; import java.util.NoSuchElementException; import java.util.UUID; @@ -40,7 +46,6 @@ public String getAllLitters(Model model) { @GetMapping("/{id}") public String getLitterById(@PathVariable("id") UUID id, Model model) { - try { LitterModel litter = litterService.getLitterById(id); model.addAttribute("id", litter.getId()); @@ -51,7 +56,11 @@ public String getLitterById(@PathVariable("id") UUID id, Model model) { model.addAttribute("country", litter.getAddress().getCountry()); model.addAttribute("postcode", litter.getAddress().getPostCode()); model.addAttribute("description", litter.getDescription()); - model.addAttribute("image", litter.getImage()); + + // Convert the byte array to a base64 encoded string + String base64Image = Base64.getEncoder().encodeToString(litter.getImage()); + model.addAttribute("image", base64Image); + model.addAttribute("status", litter.getStatus()); model.addAttribute("view", "litter_details"); return "welcome"; @@ -64,7 +73,11 @@ public String getLitterById(@PathVariable("id") UUID id, Model model) { @GetMapping("/create") public String showAddLitterForm(Model model) { try { - model.addAttribute("litter", new LitterCreateOrModifyModel()); + LitterCreateOrModifyModel litterModel = new LitterCreateOrModifyModel(); + + // Set any other necessary properties in the litterModel object + + model.addAttribute("litter", litterModel); return "litter_add_form"; } catch (NoSuchElementException e) { model.addAttribute("errorMessage", e.getMessage()); @@ -75,7 +88,8 @@ public String showAddLitterForm(Model model) { @PostMapping("/create") public String addNewLitter(@ModelAttribute("litter") LitterCreateOrModifyModel litterModel, @ModelAttribute("address") Address address, - @RequestParam("file") MultipartFile file,Model model) { + @RequestParam("file") MultipartFile file, + Model model) { try { litterService.addNewLitter(litterModel, address, file); return "redirect:/thy/litter"; diff --git a/src/main/resources/templates/litter_details.html b/src/main/resources/templates/litter_details.html index 1cdaebb..3087b27 100644 --- a/src/main/resources/templates/litter_details.html +++ b/src/main/resources/templates/litter_details.html @@ -26,7 +26,7 @@

Description:

Image:

-

+ Litter Image

Status:

diff --git a/src/main/resources/templates/welcome.html b/src/main/resources/templates/welcome.html index 1dc18e9..29f3376 100644 --- a/src/main/resources/templates/welcome.html +++ b/src/main/resources/templates/welcome.html @@ -58,7 +58,7 @@
-
+