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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,11 +29,16 @@ public class LitterService {

private final AddressRepository addressRepository;

//Image i will need @RequestParam("image") MultipartFile file


public List<LitterModel> getAllLitters() {
List<Litter> 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);
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand All @@ -94,19 +102,22 @@ 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) {
Optional<Litter> optionalExistingLitter = litterRepository.findById(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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public class UserModel {
private String email;
private String firstName;
private Role role;
}

}
1 change: 1 addition & 0 deletions src/main/java/com/csaba79coder/littersnap/util/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID;

@Controller
Expand All @@ -26,58 +27,106 @@ public LitterViewController(LitterService litterService) {

@GetMapping
public String getAllLitters(Model model) {
List<LitterModel> litters = litterService.getAllLitters();

if (litters.isEmpty()) {
return "error_page";
} else {
try {
List<LitterModel> 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
}

}


Expand Down
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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<UserModel> 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
}
}
}
21 changes: 19 additions & 2 deletions src/main/resources/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ body {
.fixed-buttons {
position: fixed;
top: 0;
right: 20px;
right: 0;
z-index: 999;
}

Expand Down Expand Up @@ -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 {*/
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/templates/error_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
</head>

<body>
<!-- Your 404 content here -->

<div class="container d-flex flex-column justify-content-center align-items-center" style="min-height: 100vh;">
<h1>404 - Page Not Found</h1>
<h1>Error Page</h1>
<br>
<p th:text = "${errorMessage}" ></p>
<p class="text-center">Vasile, Csaba and Kev are on it now...</p>
<img th:src="@{/images/404.jpg}" alt="Image" class="no_page_found_icon">
</div>
Expand Down
15 changes: 0 additions & 15 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,10 @@
</button>
</div>
</div>
<button class="btn btn-primary btn-sm btn-block m-1" data-bs-toggle="modal" data-bs-target="#addLitterModal">
TestAddButton
</button>
<button class="btn btn-warning btn-sm btn-block m-1" data-bs-toggle="modal" data-bs-target="#editLitterModal">
TestEditButton
</button>

<button class="btn btn-danger btn-sm btn-block m-1" data-bs-toggle="modal" data-bs-target="#showLitterDetailsModal">
Test Litter Details
</button>


<div th:replace="~{litter_add_form}"></div>
<div th:replace="~{litter_edit_form}"></div>
<div th:replace="~{litter_details}"></div>
<div th:replace="~{signIn_form}"></div>
<div th:replace="~{signUp_form}"></div>


<!-- Middle Part -->
<div class="row justify-content-center align-items-center min-vh-100">
<div class="col-12 col-md-8 col-lg-6">
Expand Down
Loading