Skip to content

Commit dfb95d4

Browse files
committed
updated controller
1 parent f59b095 commit dfb95d4

File tree

8 files changed

+150
-32
lines changed

8 files changed

+150
-32
lines changed

src/main/java/com/csaba79coder/littersnap/controller/LitterController.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.csaba79coder.littersnap.controller;
22

3+
import com.csaba79coder.littersnap.model.address.entity.Address;
34
import com.csaba79coder.littersnap.model.litter.dto.LitterCreateOrModifyModel;
45
import com.csaba79coder.littersnap.model.litter.dto.LitterModel;
56
import com.csaba79coder.littersnap.model.litter.service.LitterService;
67
import lombok.RequiredArgsConstructor;
78
import org.springframework.http.HttpStatus;
89
import org.springframework.http.ResponseEntity;
910
import org.springframework.web.bind.annotation.*;
11+
import org.springframework.web.multipart.MultipartFile;
1012

1113
import java.util.List;
1214
import java.util.UUID;
@@ -24,8 +26,10 @@ public List<LitterModel> getAllLitters() {
2426
}
2527

2628
@PostMapping
27-
public ResponseEntity<LitterModel> addNewLitter(@RequestBody LitterCreateOrModifyModel litterModel) {
28-
return ResponseEntity.status(201).body(litterService.addNewLitter(litterModel));
29+
public ResponseEntity<LitterModel> addNewLitter(@RequestBody LitterCreateOrModifyModel litterModel,
30+
@RequestBody Address address,
31+
@RequestParam("file") MultipartFile file) {
32+
return ResponseEntity.status(201).body(litterService.addNewLitter(litterModel, address, file));
2933
}
3034

3135
@GetMapping("/{id}")
@@ -38,7 +42,8 @@ public ResponseEntity<LitterModel> getLitterById(@PathVariable("id") UUID id) {
3842
}
3943

4044
@PutMapping("/{id}")
41-
public ResponseEntity<LitterModel> updateExistingLitter(@PathVariable("id") UUID id, @RequestBody LitterCreateOrModifyModel model) {
45+
public ResponseEntity<LitterModel> updateExistingLitter(@PathVariable("id") UUID id,
46+
@RequestBody LitterCreateOrModifyModel model) {
4247
return ResponseEntity.status(200).body(litterService.updateExistingLitter(id, model));
4348
}
4449

src/main/java/com/csaba79coder/littersnap/model/address/entity/Address.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public class Address extends Auditable {
3030
@Column(name = "country")
3131
private String country;
3232

33-
@ManyToMany
33+
@OneToMany(mappedBy = "address")
3434
private List<Litter> litters;
35-
3635
}

src/main/java/com/csaba79coder/littersnap/model/address/persitence/AddressRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
66

7+
import java.util.Optional;
78
import java.util.UUID;
89

910
@Repository
1011
public interface AddressRepository extends JpaRepository<Address, UUID> {
12+
13+
Optional<Address> findAddressByCityContainingIgnoreCaseAndCountryContainingIgnoreCaseAndPostCodeAndFirstLineContainsIgnoreCase(
14+
String city, String country, String postCode, String firstLine);
1115
}

src/main/java/com/csaba79coder/littersnap/model/litter/entity/Litter.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
import com.csaba79coder.littersnap.model.address.entity.Address;
44
import com.csaba79coder.littersnap.model.base.entity.Auditable;
5-
import com.csaba79coder.littersnap.model.report.entity.Report;
65
import com.csaba79coder.littersnap.value.LitterStatus;
76
import jakarta.persistence.*;
87
import lombok.AllArgsConstructor;
98
import lombok.Getter;
109
import lombok.NoArgsConstructor;
1110
import lombok.Setter;
1211

13-
import java.util.List;
14-
1512
@Entity
1613
@Table(name = "litter")
1714
@Getter
@@ -31,10 +28,7 @@ public class Litter extends Auditable {
3128
@Column(name = "image", length = Integer.MAX_VALUE, nullable = false)
3229
private byte[] image;
3330

34-
@OneToMany(mappedBy = "litter")
35-
private List<Report> reports;
36-
37-
@ManyToMany
38-
private List<Address> addresses;
39-
31+
@ManyToOne(cascade = CascadeType.ALL)
32+
@JoinColumn(name = "address_id", referencedColumnName = "id")
33+
private Address address;
4034
}

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package com.csaba79coder.littersnap.model.litter.service;
22

3+
import com.csaba79coder.littersnap.model.address.entity.Address;
4+
import com.csaba79coder.littersnap.model.address.persitence.AddressRepository;
35
import com.csaba79coder.littersnap.model.litter.dto.LitterCreateOrModifyModel;
46
import com.csaba79coder.littersnap.model.litter.dto.LitterModel;
57
import com.csaba79coder.littersnap.model.litter.entity.Litter;
68
import com.csaba79coder.littersnap.model.litter.persistence.LitterRepository;
9+
import com.csaba79coder.littersnap.util.ImageUtil;
710
import com.csaba79coder.littersnap.util.Mapper;
811
import lombok.RequiredArgsConstructor;
912
import lombok.extern.slf4j.Slf4j;
1013
import org.springframework.stereotype.Service;
14+
import org.springframework.web.multipart.MultipartFile;
15+
16+
import java.io.IOException;
1117
import java.util.List;
1218
import java.util.Optional;
1319
import java.util.UUID;
@@ -20,6 +26,8 @@ public class LitterService {
2026

2127
private final LitterRepository litterRepository;
2228

29+
private final AddressRepository addressRepository;
30+
2331
//Image i will need @RequestParam("image") MultipartFile file
2432

2533

@@ -30,15 +38,35 @@ public List<LitterModel> getAllLitters() {
3038
.collect(Collectors.toList());
3139
}
3240

33-
public LitterModel addNewLitter(LitterCreateOrModifyModel litterModel) {
34-
Litter litterEntity = Mapper.mapLitterCreateOrModifyModelToEntity(litterModel);
41+
public LitterModel addNewLitter(LitterCreateOrModifyModel litterModel, Address address, MultipartFile file) {
42+
Optional<Address> addressOptional = addressRepository.findAddressByCityContainingIgnoreCaseAndCountryContainingIgnoreCaseAndPostCodeAndFirstLineContainsIgnoreCase(
43+
address.getCity(), address.getCountry(), address.getPostCode(), address.getFirstLine());
44+
if (addressOptional.isPresent()) {
45+
address = addressOptional.get();
46+
} else {
47+
address = addressRepository.save(address);
48+
}
49+
Litter litterEntity = new Litter();
50+
litterEntity.setAddress(address); //TODO set an incoming address also here! if address is not found in entiy new must be created
51+
litterEntity.setDescription(litterModel.getDescription());
52+
try {
53+
litterEntity.setImage(ImageUtil.decompressImage(file.getBytes())); // saving to database, so decompressing!!!
54+
} catch (IOException e) {
55+
throw new RuntimeException(e);
56+
}
3557
Litter savedLitterEntity = litterRepository.save(litterEntity);
3658
return Mapper.mapLitterEntityToModel(savedLitterEntity);
3759
}
3860

3961
public LitterModel getLitterById(UUID id) {
40-
Optional<Litter> optionalLitter = litterRepository.findById(id);
41-
return optionalLitter.map(Mapper::mapLitterEntityToModel).orElse(null);
62+
Optional<LitterModel> model = litterRepository.findById(id).map(Mapper::mapLitterEntityToModel);
63+
if (model.isPresent()) {
64+
return model.get();
65+
} else {
66+
String message = String.format("Litter with id %s not found", id);
67+
log.error(message);
68+
throw new RuntimeException(message);
69+
}
4270
}
4371

4472
public LitterModel updateExistingLitter(UUID id, LitterCreateOrModifyModel model) {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.csaba79coder.littersnap.view;
22

3+
import com.csaba79coder.littersnap.model.address.entity.Address;
34
import com.csaba79coder.littersnap.model.litter.dto.LitterCreateOrModifyModel;
45
import com.csaba79coder.littersnap.model.litter.dto.LitterModel;
56
import com.csaba79coder.littersnap.model.litter.service.LitterService;
67
import com.csaba79coder.littersnap.util.Mapper;
78
import org.springframework.stereotype.Controller;
89
import org.springframework.ui.Model;
910
import org.springframework.web.bind.annotation.*;
11+
import org.springframework.web.multipart.MultipartFile;
1012

1113
import java.util.List;
1214
import java.util.UUID;
@@ -47,8 +49,10 @@ public String showAddLitterForm(Model model) {
4749
}
4850

4951
@PostMapping("/create")
50-
public String addNewLitter(@ModelAttribute("litterModel") LitterCreateOrModifyModel litterModel) {
51-
litterService.addNewLitter(litterModel);
52+
public String addNewLitter(@ModelAttribute("litter") LitterCreateOrModifyModel litterModel,
53+
@ModelAttribute("address") Address address,
54+
@RequestParam("file") MultipartFile file) {
55+
litterService.addNewLitter(litterModel, address, file);
5256
return "redirect:/litters";
5357
}
5458

@@ -64,13 +68,13 @@ public String showEditForm(@PathVariable UUID id, Model model) {
6468
}
6569

6670
@PostMapping("/edit/{id}")
67-
public String updateReport(@PathVariable UUID id, @ModelAttribute("report") LitterCreateOrModifyModel litterModel) {
71+
public String updateLitter(@PathVariable UUID id, @ModelAttribute("report") LitterCreateOrModifyModel litterModel) {
6872
litterService.updateExistingLitter(id,litterModel);
6973
return "redirect:/reports"; // Redirect to the URL for displaying all reports after successful update
7074
}
7175

7276
@GetMapping("/delete/{id}")
73-
public String deleteReport(@PathVariable UUID id) {
77+
public String deleteLitter(@PathVariable UUID id) {
7478
litterService.deleteLitter(id);
7579
return "redirect:/reports"; // Redirect to the URL for displaying all reports after successful deletion
7680
}

src/main/resources/templates/index.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
</div>
3535
</div>
3636

37-
<!-- &lt;!&ndash; modal&ndash;&gt;-->
38-
<!-- <div id="addReportModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="addReportModalLabel"-->
39-
<!-- aria-hidden="true">-->
40-
<!-- <div class="modal-dialog" role="document">-->
41-
<!-- <div class="modal-content">-->
42-
<!-- &lt;!&ndash; Content of the modal from add-report.html &ndash;&gt;-->
43-
<!-- <div th:include="add_litter_form :: modal-content"></div>-->
44-
<!-- </div>-->
45-
<!-- </div>-->
46-
<!-- </div>-->
37+
<!-- modal-->
38+
<div id="addReportModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="addReportModalLabel"
39+
aria-hidden="true">
40+
<div class="modal-dialog" role="document">
41+
<div class="modal-content">
42+
<!-- Content of the modal from add-report.html -->
43+
<div th:include="add_litter_form :: modal-content"></div>
44+
</div>
45+
</div>
46+
</div>
4747

4848
<!-- Middle Part -->
4949
<div class="row justify-content-center align-items-center min-vh-100">
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"
7+
integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
8+
9+
<!-- Font Awesome CSS -->
10+
<link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.3.1/css/all.css'>
11+
<link rel="preconnect" href="https://fonts.googleapis.com">
12+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
13+
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
14+
<!-- Include jQuery from Google CDN -->
15+
<link rel="stylesheet" th:href="@{/style.css}">
16+
<link th:src="@{/index.js}">
17+
18+
<title>Litter Snap</title>
19+
</head>
20+
21+
<body>
22+
23+
<div class="background-overlay"></div>
24+
<div class="container-fluid">
25+
<!-- Button to trigger the modal -->
26+
<div class="row justify-content-end m-3">
27+
<div class="btn-group col-12 col-md-4 col-lg-2 fixed-buttons">
28+
<div class="d-flex justify-content-end align-items-center">
29+
<div class="me-2">Welcome, <span th:text="${username}"></span></div>
30+
<div class="dropdown">
31+
<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
32+
<i class="fas fa-bars"></i>
33+
</button>
34+
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdownMenuButton">
35+
<li><a class="dropdown-item" href="#">Logout</a></li>
36+
<li><a class="dropdown-item" href="#">View Reports</a></li>
37+
</ul>
38+
</div>
39+
</div>
40+
</div>
41+
</div>
42+
43+
44+
<!-- Middle Part -->
45+
<div class="row justify-content-center align-items-center min-vh-100">
46+
<div class="col-12 col-md-8 col-lg-6">
47+
48+
<div class="container logo-and-slogo-container">
49+
<div class="row ">
50+
<div class="col-7 col-md-7 col-lg-7">
51+
<!-- Content of the first column -->
52+
<span class="input-label-logo">LitterSnap</span>
53+
<br>
54+
<br>
55+
<span class="input-label-slogan">IF YOU CAN SEE IT, YOU CAN FIX IT...</span>
56+
57+
</div>
58+
<div class="col-5 col-md-5 col-lg-5">
59+
<img th:src="@{/images/ManAndPhone.png}" alt="Image" class="man-with-the-phone-icon">
60+
</div>
61+
</div>
62+
</div>
63+
</div>
64+
</div>
65+
</div>
66+
67+
68+
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
69+
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
70+
crossorigin="anonymous"></script>
71+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
72+
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
73+
crossorigin="anonymous"></script>
74+
75+
<div class="footer-overlay">
76+
<footer>
77+
<div class="container-fluid">
78+
<p>&copy; 2023 Vasile, Csaba and Kevin. Wiley Edge. All rights reserved.</p>
79+
</div>
80+
</footer>
81+
</div>
82+
83+
</body>
84+
</html>

0 commit comments

Comments
 (0)