Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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";
Expand All @@ -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());
Expand All @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/litter_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h4><span class="text-muted">Description:</span></h4>
<p><span th:text="${description}"></span></p>

<h4><span class="text-muted">Image:</span></h4>
<p><span th:text="${image}"></span></p>
<img th:src="@{'data:image/png;base64,' + ${image}}" alt="Litter Image" />

<h4><span class="text-muted">Status:</span></h4>
<p><span th:text="${status}"></span></p>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<div th:if="~{${view}}" class="row m-4">
<div class="col">
<!-- Dynamic Content Placeholder -->
<div th:include="~{${view}}"></div>
<div th:insert="~{${view}}"></div>

</div>
</div>
Expand Down