Skip to content

Commit 7da7fc5

Browse files
committed
litter-list implemented
1 parent 74b9e01 commit 7da7fc5

File tree

5 files changed

+162
-82
lines changed

5 files changed

+162
-82
lines changed

src/main/java/com/csaba79coder/littersnap/model/litter/service/LitterService.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.io.IOException;
1717
import java.util.List;
18+
import java.util.NoSuchElementException;
1819
import java.util.Optional;
1920
import java.util.UUID;
2021
import java.util.stream.Collectors;
@@ -28,11 +29,16 @@ public class LitterService {
2829

2930
private final AddressRepository addressRepository;
3031

31-
//Image i will need @RequestParam("image") MultipartFile file
32-
33-
3432
public List<LitterModel> getAllLitters() {
3533
List<Litter> litters = litterRepository.findAll();
34+
35+
36+
if (litters.isEmpty()) {
37+
String message = "Litter list is empty";
38+
log.error(message);
39+
throw new NoSuchElementException(message);
40+
}
41+
3642
return litters.stream()
3743
.map(litter -> {
3844
LitterModel litterModel = Mapper.mapLitterEntityToModel(litter);
@@ -52,12 +58,14 @@ public LitterModel addNewLitter(LitterCreateOrModifyModel litterModel, Address a
5258
address = addressRepository.save(address);
5359
}
5460
Litter litterEntity = new Litter();
55-
litterEntity.setAddress(address); //TODO set an incoming address also here! if address is not found in entiy new must be created
61+
litterEntity.setAddress(address);
5662
litterEntity.setDescription(litterModel.getDescription());
5763
try {
5864
litterEntity.setImage(ImageUtil.compressImage(file.getBytes())); // Compressing the image before saving
5965
} catch (IOException e) {
60-
throw new RuntimeException(e);
66+
String message = String.format("File not found."+ e.getMessage());
67+
log.error(message);
68+
throw new NoSuchElementException(e);
6169
}
6270
Litter savedLitterEntity = litterRepository.save(litterEntity);
6371
return Mapper.mapLitterEntityToModel(savedLitterEntity);
@@ -74,7 +82,7 @@ public LitterModel getLitterById(UUID id) {
7482
} else {
7583
String message = String.format("Litter with id %s not found", id);
7684
log.error(message);
77-
throw new RuntimeException(message);
85+
throw new NoSuchElementException(message);
7886
}
7987
}
8088

@@ -94,19 +102,22 @@ public LitterModel updateExistingLitter(UUID id, LitterCreateOrModifyModel model
94102
Litter updatedLitter = litterRepository.save(Mapper.mapLitterModelToEntity(existingLitter));
95103

96104
return Mapper.mapLitterEntityToModel(updatedLitter);
105+
}else {
106+
String message = String.format("Couldn't update. Litter with id %s not found", id);
107+
log.error(message);
108+
throw new NoSuchElementException(message);
97109
}
98-
//TODO
99-
//Case where liter not found
100-
return null;
110+
101111
}
102112

103113
public void deleteLitter(UUID id) {
104114
Optional<Litter> optionalExistingLitter = litterRepository.findById(id);
105115
if (optionalExistingLitter.isPresent()) {
106116
litterRepository.deleteById(id);
107117
} else {
108-
//TODO
109-
//Case where liter not found
118+
String message = String.format("Couldn't delete. Litter with id %s not found", id);
119+
log.error(message);
120+
throw new NoSuchElementException(message);
110121
}
111122
}
112123
}

src/main/java/com/csaba79coder/littersnap/view/LitterViewController.java

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.web.multipart.MultipartFile;
1212

1313
import java.util.List;
14+
import java.util.NoSuchElementException;
1415
import java.util.UUID;
1516

1617
@Controller
@@ -26,58 +27,88 @@ public LitterViewController(LitterService litterService) {
2627

2728
@GetMapping
2829
public String getAllLitters(Model model) {
29-
List<LitterModel> litters = litterService.getAllLitters();
30-
31-
if (litters.isEmpty()) {
32-
return "error_page";
33-
} else {
30+
try {
31+
List<LitterModel> litters = litterService.getAllLitters();
3432
model.addAttribute("litters", litters);
3533
return "litter_list"; // Replace with the actual view name for displaying the list of litters
34+
} catch (NoSuchElementException e) {
35+
model.addAttribute("errorMessage", e.getMessage());
36+
return "error_page"; // Redirect to the error page to display the error message
3637
}
3738
}
3839

3940
@GetMapping("/{id}")
4041
public String getLitterById(@PathVariable("id") UUID id, Model model) {
41-
LitterModel litter = litterService.getLitterById(id);
42-
model.addAttribute("litter", litter);
43-
return "litter_details";
42+
43+
try {
44+
LitterModel litter = litterService.getLitterById(id);
45+
model.addAttribute("litter", litter);
46+
return "litter_details";
47+
} catch (NoSuchElementException e) {
48+
model.addAttribute("errorMessage", e.getMessage());
49+
return "error_page"; // Redirect to the error page to display the error message
50+
}
4451
}
4552

4653
@GetMapping("/create")
4754
public String showAddLitterForm(Model model) {
48-
model.addAttribute("litter", new LitterCreateOrModifyModel());
49-
return "litter_add_form";
55+
try {
56+
model.addAttribute("litter", new LitterCreateOrModifyModel());
57+
return "litter_add_form";
58+
} catch (NoSuchElementException e) {
59+
model.addAttribute("errorMessage", e.getMessage());
60+
return "error_page"; // Redirect to the error page to display the error message
61+
}
5062
}
5163

5264
@PostMapping("/create")
5365
public String addNewLitter(@ModelAttribute("litter") LitterCreateOrModifyModel litterModel,
5466
@ModelAttribute("address") Address address,
55-
@RequestParam("file") MultipartFile file) {
56-
litterService.addNewLitter(litterModel, address, file);
57-
return "redirect:/thy/litter";
67+
@RequestParam("file") MultipartFile file,Model model) {
68+
try {
69+
litterService.addNewLitter(litterModel, address, file);
70+
return "redirect:/thy/litter";
71+
} catch (NoSuchElementException e) {
72+
model.addAttribute("errorMessage", e.getMessage());
73+
return "error_page"; // Redirect to the error page to display the error message
74+
}
5875
}
5976

6077
@GetMapping("/edit/{id}")
6178
public String showEditForm(@PathVariable UUID id, Model model) {
62-
LitterCreateOrModifyModel currentLitter = Mapper.mapModelToLitterCreateOrModifyModel(litterService.getLitterById(id));
63-
if (currentLitter == null) {
64-
return "error_page";
65-
} else {
79+
try {
80+
LitterCreateOrModifyModel currentLitter = Mapper.mapModelToLitterCreateOrModifyModel(litterService.getLitterById(id));
6681
model.addAttribute("litter", currentLitter);
6782
return "litter_edit_form";
83+
} catch (NoSuchElementException e) {
84+
model.addAttribute("errorMessage", e.getMessage());
85+
return "error_page"; // Redirect to the error page to display the error message
6886
}
6987
}
7088

7189
@PostMapping("/edit/{id}")
72-
public String updateLitter(@PathVariable UUID id, @ModelAttribute("report") LitterCreateOrModifyModel litterModel) {
73-
litterService.updateExistingLitter(id,litterModel);
74-
return "redirect:/reports"; // Redirect to the URL for displaying all reports after successful update
90+
public String updateLitter(@PathVariable UUID id, @ModelAttribute("report") LitterCreateOrModifyModel litterModel, Model model) {
91+
try {
92+
litterService.updateExistingLitter(id, litterModel);
93+
return "redirect:/reports"; // Redirect to the URL for displaying all reports after successful update
94+
} catch (NoSuchElementException e) {
95+
model.addAttribute("errorMessage", e.getMessage());
96+
return "error_page"; // Redirect to the error page to display the error message
97+
}
7598
}
7699

100+
77101
@GetMapping("/delete/{id}")
78-
public String deleteLitter(@PathVariable UUID id) {
79-
litterService.deleteLitter(id);
80-
return "redirect:/reports"; // Redirect to the URL for displaying all reports after successful deletion
102+
public String deleteLitter(@PathVariable UUID id,Model model) {
103+
104+
try {
105+
litterService.deleteLitter(id);
106+
return "redirect:/reports"; // Redirect to the URL for displaying all reports after successful deletion
107+
} catch (NoSuchElementException e) {
108+
model.addAttribute("errorMessage", e.getMessage());
109+
return "error_page"; // Redirect to the error page to display the error message
110+
}
111+
81112
}
82113

83114

src/main/resources/static/style.css

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,45 @@ body {
6262
width: 100%;
6363
}
6464

65-
.input-label-logo-welcome-page {
65+
/*Logo Control when user will be logged in*/
66+
.logged-logo {
67+
position: relative;
68+
}
69+
70+
.logged-logo .logged-logo-name {
71+
position: absolute;
72+
bottom: 20px;
73+
left: 0;
74+
right: 0;
75+
font-family: 'Niagara Solid', cursive;
76+
font-size: 60px;
77+
color: #1c5486;
78+
text-align: center; /* Center the text horizontally */
79+
}
80+
81+
.logged-logo .logged-logo-slogan {
82+
position: absolute;
83+
bottom: 1px;
84+
left: 0;
85+
right: 0;
86+
font-family: 'Arial Black', sans-serif;
87+
font-size: 10px;
88+
color: darkmagenta;
89+
text-align: center; /* Center the text horizontally */
90+
}
6691

92+
.logged-logo .man-with-the-phone-icon {
93+
width: 300px;
94+
opacity: 0.965;
6795
}
6896

97+
.fixed-buttons {
98+
position: fixed;
99+
top: 0;
100+
right: 0;
101+
}
102+
103+
69104
/*Modal Container on index page*/
70105

71106
/*.modal-dialog {*/

src/main/resources/templates/error_page.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
</head>
1717

1818
<body>
19-
<!-- Your 404 content here -->
19+
2020
<div class="container d-flex flex-column justify-content-center align-items-center" style="min-height: 100vh;">
21-
<h1>404 - Page Not Found</h1>
21+
<h1>Error Page</h1>
2222
<br>
23+
<p th:text = "${errorMessage}" ></p>
2324
<p class="text-center">Vasile, Csaba and Kev are on it now...</p>
2425
<img th:src="@{/images/404.jpg}" alt="Image" class="no_page_found_icon">
2526
</div>

src/main/resources/templates/litter_list.html

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -63,69 +63,71 @@
6363
<div class="container mt-4 bg-white">
6464
<div class="row">
6565
<div class="col">
66-
<table class="table table-striped table-responsive">
67-
<thead>
68-
<tr>
69-
<th>Date</th>
70-
<th>By</th>
71-
<th>Address</th>
72-
<th>PostCode</th>
73-
<th>Status</th>
74-
<th>Action</th>
75-
</tr>
76-
</thead>
77-
<tbody>
78-
<div th:if="${not #lists.isEmpty(litters)}">
79-
<tr th:each="litter : ${litters}">
80-
<td th:text="${litter.getCreatedAt()}"></td>
81-
<td th:text="${litter.getCreatedBy()}"></td>
82-
<td th:text="${litter.getAddress().firstLine}"></td>
83-
<td th:text="${litter.getAddress().postCode}"></td>
84-
<td th:text="${litter.getStatus()}"></td>
85-
<td>
86-
<div class="btn-group">
87-
<a th:href="@{/thy/{id}(id=${report.id})}" class="btn btn-success btn-sm">View</a>
88-
<a th:href="@{/thy/{id}/edit(id=${report.id})}" class="btn btn-warning btn-sm">Edit</a>
89-
<a th:href="@{/thy/{id}/delete(id=${report.id})}" class="btn btn-danger btn-sm">Delete</a>
90-
</div>
91-
</td>
92-
</tr>
93-
</div>
94-
<div th:if="${#lists.isEmpty(reports)}">
66+
<div class="table-responsive">
67+
<table class="table table-striped">
68+
<thead>
9569
<tr>
96-
<td colspan="6">
97-
<p>No data</p>
98-
</td>
70+
<th>Date</th>
71+
<th>By</th>
72+
<th>Address</th>
73+
<th>PostCode</th>
74+
<th>Status</th>
75+
<th>Action</th>
9976
</tr>
100-
</div>
101-
</tbody>
102-
</table>
77+
</thead>
78+
<tbody>
79+
<div th:if="${not #lists.isEmpty(litters)}">
80+
<tr th:each="litter : ${litters}">
81+
<td th:text="${litter.getCreatedAt()}"></td>
82+
<td th:text="${litter.getCreatedBy()}"></td>
83+
<td th:text="${litter.getAddress().firstLine}"></td>
84+
<td th:text="${litter.getAddress().postCode}"></td>
85+
<td th:text="${litter.getStatus()}"></td>
86+
<td>
87+
<div class="btn-group">
88+
<a th:href="@{/thy/{id}(id=${litter.id})}"
89+
class="btn btn-success btn-sm">View</a>
90+
<a th:href="@{/thy/{id}/edit(id=${litter.id})}" class="btn btn-warning btn-sm">Edit</a>
91+
<a th:href="@{/thy/{id}/delete(id=${litter.id})}" class="btn btn-danger btn-sm">Delete</a>
92+
</div>
93+
</td>
94+
</tr>
95+
</div>
96+
<div th:if="${#lists.isEmpty(litters)}">
97+
<tr>
98+
<td colspan="6">
99+
100+
</td>
101+
</tr>
102+
</div>
103+
</tbody>
104+
</table>
105+
</div>
103106
</div>
104107
</div>
105108
</div>
106109

107110

108-
109-
<!-- Middle Part -->
110-
<div class="row justify-content-center align-items-end min-vh-100">
111-
<div class="col-12 col-md-8 col-lg-6">
112-
<div class="container logo-and-slogo-container">
111+
<div class="row d-flex align-items-end justify-content-end min-vh-100">
112+
<div class="col-12 col-md-8 col-lg-6 ">
113+
<div class="container logged-logo">
113114
<div class="row">
114-
<div class="col-7 col-md-7 col-lg-7">
115+
<div class="col-10 col-md-9 col-lg-9">
115116
<!-- Content of the first column -->
116-
<span class="input-label-logo-welcome-page">LitterSnap</span>
117-
<br>
117+
<span class="logged-logo-name">LitterSnap</span>
118118
<br>
119-
<span class="input-label-slogan-welcome-page">IF YOU CAN SEE IT, YOU CAN FIX IT...</span>
119+
<span class="logged-logo-slogan">IF YOU CAN SEE IT, YOU CAN FIX IT...</span>
120120
</div>
121-
<div class="col-5 col-md-5 col-lg-5 d-flex justify-content-end align-items-end">
121+
<div class="col-2 col-md-3 col-lg-3 d-flex justify-content-end align-items-end">
122122
<img th:src="@{/images/ManAndPhone.png}" alt="Image" class="man-with-the-phone-icon">
123123
</div>
124124
</div>
125125
</div>
126126
</div>
127127
</div>
128128

129+
130+
129131
</div>
130132

131133

0 commit comments

Comments
 (0)