dodawanie zdjęć i pobieranie ścieżek do nich
więcej kategorii
This commit is contained in:
@@ -7,7 +7,11 @@ import jakarta.persistence.EntityNotFoundException;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -44,6 +48,8 @@ public class NoticeController {
|
|||||||
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dto.setPublishDate(java.time.LocalDateTime.now());
|
||||||
|
|
||||||
noticeService.addNotice(dto);
|
noticeService.addNotice(dto);
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body("Dodano ogłoszenie.");
|
return ResponseEntity.status(HttpStatus.CREATED).body("Dodano ogłoszenie.");
|
||||||
@@ -66,42 +72,81 @@ public class NoticeController {
|
|||||||
isError = true;
|
isError = true;
|
||||||
errors.add(dto.getClientId().toString());
|
errors.add(dto.getClientId().toString());
|
||||||
} else {
|
} else {
|
||||||
if(!isError){
|
if (!isError) {
|
||||||
noticeService.addNotice(dto);
|
noticeService.addNotice(dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isError) {
|
if (isError) {
|
||||||
return response.status(HttpStatus.BAD_REQUEST).body("Nie znaleziono klientów: " + errors);
|
return response.status(HttpStatus.BAD_REQUEST).body("Nie znaleziono klientów: " + errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/edit/{id}")
|
@PutMapping("/edit/{id}")
|
||||||
public ResponseEntity<Object> editNotice(@PathVariable("id") long id, @RequestBody NoticeDTO dto) {
|
public ResponseEntity<Object> editNotice(@PathVariable("id") long id, @RequestBody NoticeDTO dto) {
|
||||||
if (noticeService.noticeExists(id)) {
|
if (noticeService.noticeExists(id)) {
|
||||||
try {
|
try {
|
||||||
return new ResponseEntity<>(noticeService.updateNotice(id, dto), HttpStatus.OK);
|
return new ResponseEntity<>(noticeService.updateNotice(id, dto), HttpStatus.OK);
|
||||||
} catch (EntityNotFoundException e) {
|
} catch (EntityNotFoundException e) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Nie znaleziono ogłoszenia o ID: " + id);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Nie znaleziono ogłoszenia o ID: " + id);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/delete/{id}")
|
@DeleteMapping("/delete/{id}")
|
||||||
public ResponseEntity deleteNotice(@PathVariable("id") long id) {
|
public ResponseEntity deleteNotice(@PathVariable("id") long id) {
|
||||||
if(noticeService.noticeExists(id)) {
|
if (noticeService.noticeExists(id)) {
|
||||||
noticeService.deleteNotice(id);
|
noticeService.deleteNotice(id);
|
||||||
return new ResponseEntity<>(HttpStatus.OK);
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
} else {
|
} else {
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
@PostMapping("/upload/{id}")
|
||||||
|
public ResponseEntity<String> uploadImage(@PathVariable("id") Long id, @RequestParam("file") MultipartFile file) {
|
||||||
|
if (!noticeService.noticeExists(id)) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Nie znaleziono ogłoszenia o ID: " + id);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
String filePath = noticeService.saveImage(id, file);
|
||||||
|
return ResponseEntity.ok("Zdjęcie zapisane pod ścieżką: " + filePath);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Błąd podczas zapisywania zdjęcia: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/images/{id}")
|
||||||
|
public ResponseEntity<List<String>> getAllImages(@PathVariable("id") Long id) {
|
||||||
|
try {
|
||||||
|
Path directoryPath = Paths.get("src/main/resources/static/images/notices/" + id);
|
||||||
|
if (!Files.exists(directoryPath) || !Files.isDirectory(directoryPath)) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> imagePaths = new ArrayList<>();
|
||||||
|
Files.list(directoryPath).forEach(file -> {
|
||||||
|
if (Files.isRegularFile(file) && Files.isReadable(file)) {
|
||||||
|
imagePaths.add(file.toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (imagePaths.isEmpty()) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok(imagePaths);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// @GetMapping("/check/{id}")
|
// @GetMapping("/check/{id}")
|
||||||
// public ResponseEntity<String> checkNotice(@PathVariable("id") long id) {
|
// public ResponseEntity<String> checkNotice(@PathVariable("id") long id) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import _11.asktpk.artisanconnectbackend.utils.Enums;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Getter @Setter
|
@Getter @Setter
|
||||||
@@ -26,7 +26,7 @@ public class NoticeDTO {
|
|||||||
|
|
||||||
private Enums.Status status;
|
private Enums.Status status;
|
||||||
|
|
||||||
private LocalDate publishDate;
|
private LocalDateTime publishDate;
|
||||||
|
|
||||||
private List<AttributesNotice> attributesNotices;
|
private List<AttributesNotice> attributesNotices;
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ public class NoticeDTO {
|
|||||||
|
|
||||||
public NoticeDTO(Long noticeId, String title, Long clientId, String description, Double price,
|
public NoticeDTO(Long noticeId, String title, Long clientId, String description, Double price,
|
||||||
Enums.Category category, List<String> images, Enums.Status status,
|
Enums.Category category, List<String> images, Enums.Status status,
|
||||||
LocalDate publishDate, List<AttributesNotice> attributesNotices) {
|
LocalDateTime publishDate, List<AttributesNotice> attributesNotices) {
|
||||||
this.noticeId = noticeId;
|
this.noticeId = noticeId;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.clientId = clientId;
|
this.clientId = clientId;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package _11.asktpk.artisanconnectbackend.entities;
|
|||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import _11.asktpk.artisanconnectbackend.utils.Enums.*;
|
import _11.asktpk.artisanconnectbackend.utils.Enums.*;
|
||||||
@@ -36,7 +36,7 @@ public class Notice {
|
|||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private Status status;
|
private Status status;
|
||||||
|
|
||||||
private LocalDate publishDate;
|
private LocalDateTime publishDate;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||||
private List<AttributesNotice> attributesNotices;
|
private List<AttributesNotice> attributesNotices;
|
||||||
|
|||||||
@@ -7,7 +7,13 @@ import _11.asktpk.artisanconnectbackend.repository.NoticeRepository;
|
|||||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -106,4 +112,51 @@ public class NoticeService {
|
|||||||
throw new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id);
|
throw new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String saveImage(Long noticeId, MultipartFile file) throws IOException {
|
||||||
|
String uploadDir = "src/main/resources/static/images/notices/" + noticeId;
|
||||||
|
Path uploadPath = Paths.get(uploadDir);
|
||||||
|
|
||||||
|
if (!Files.exists(uploadPath)) {
|
||||||
|
Files.createDirectories(uploadPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// szukanie nazwy pliku
|
||||||
|
String fileName = file.getOriginalFilename();
|
||||||
|
if (fileName != null) {
|
||||||
|
String extension = fileName.substring(fileName.lastIndexOf('.'));
|
||||||
|
String baseName = fileName.substring(0, fileName.lastIndexOf('.'));
|
||||||
|
|
||||||
|
List<Path> filesInDirectory = Files.list(uploadPath)
|
||||||
|
.filter(Files::isRegularFile)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
int maxNumber = filesInDirectory.stream()
|
||||||
|
.map(path -> path.getFileName().toString())
|
||||||
|
.filter(name -> name.startsWith(baseName) && name.endsWith(extension))
|
||||||
|
.map(name -> name.substring(baseName.length(), name.length() - extension.length()))
|
||||||
|
.filter(number -> number.matches("\\d+"))
|
||||||
|
.mapToInt(Integer::parseInt)
|
||||||
|
.max()
|
||||||
|
.orElse(0);
|
||||||
|
|
||||||
|
fileName = baseName + (maxNumber + 1) + extension;
|
||||||
|
}
|
||||||
|
//koniec szukania nazwy pliku
|
||||||
|
|
||||||
|
if(fileName == null) {
|
||||||
|
throw new IOException("Nie można znaleźć nazwy pliku");
|
||||||
|
}
|
||||||
|
Path filePath = uploadPath.resolve(fileName);
|
||||||
|
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
|
||||||
|
Notice notice = noticeRepository.findById(noticeId)
|
||||||
|
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + noticeId));
|
||||||
|
List<String> images = notice.getImages();
|
||||||
|
images.add(filePath.toString());
|
||||||
|
notice.setImages(images);
|
||||||
|
noticeRepository.save(notice);
|
||||||
|
|
||||||
|
return filePath.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,14 @@ public class Enums {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum Category {
|
public enum Category {
|
||||||
Electronics, Artwork, Kitchen, Buildings, Home, Fashion // Replace with actual categories
|
Handmade, Woodworking, Metalworking, Ceramics,
|
||||||
|
Textiles, Jewelry, Leatherwork, Painting, Sculpture,
|
||||||
|
Glasswork, Furniture, Restoration, Tailoring, Weaving,
|
||||||
|
Calligraphy, Pottery, Blacksmithing, Basketry, Embroidery,
|
||||||
|
Knitting, Carpentry, Other
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Status {
|
public enum Status {
|
||||||
ACTIVE, INACTIVE // Replace with actual statuses
|
ACTIVE, INACTIVE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,6 @@ spring.sql.init.mode=always
|
|||||||
spring.jpa.defer-datasource-initialization=true
|
spring.jpa.defer-datasource-initialization=true
|
||||||
|
|
||||||
# create and drop table, good for testing, production set to none or comment it
|
# create and drop table, good for testing, production set to none or comment it
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
|
||||||
|
spring.web.resources.static-locations=classpath:/static/,file:images/
|
||||||
@@ -5,3 +5,11 @@ VALUES
|
|||||||
('jane.smith@example.com', 'Jane', 'null', 'Smith', 'securepass', 'USER'),
|
('jane.smith@example.com', 'Jane', 'null', 'Smith', 'securepass', 'USER'),
|
||||||
('michael.brown@example.com', 'Michael', 'null', 'Brown', 'mypassword', 'USER'),
|
('michael.brown@example.com', 'Michael', 'null', 'Brown', 'mypassword', 'USER'),
|
||||||
('emily.jones@example.com', 'Emily', 'null', 'Jones', 'passw0rd', 'USER');
|
('emily.jones@example.com', 'Emily', 'null', 'Jones', 'passw0rd', 'USER');
|
||||||
|
|
||||||
|
|
||||||
|
INSERT INTO notice (title, description, client_id, price, category, status, publish_date) VALUES
|
||||||
|
('Ręcznie robiona biżuteria', 'Unikalna biżuteria wykonana ręcznie z najwyższej jakości materiałów.', 5, 150.00, 'Jewelry', 'ACTIVE', '2023-10-01'),
|
||||||
|
('Drewniany stół', 'Solidny stół wykonany z litego drewna dębowego.', 3, 1200.00, 'Furniture', 'ACTIVE', '2023-09-15'),
|
||||||
|
('Ceramiczna waza', 'Piękna waza ceramiczna, idealna na prezent.', 2, 300.00, 'Ceramics', 'INACTIVE', '2023-08-20'),
|
||||||
|
('Obraz olejny', 'Obraz olejny przedstawiający krajobraz górski.', 4, 800.00, 'Painting', 'ACTIVE', '2023-07-10'),
|
||||||
|
('Skórzany portfel', 'Ręcznie wykonany portfel ze skóry naturalnej.', 1, 250.00, 'Leatherwork', 'ACTIVE', '2023-06-05');
|
||||||
Reference in New Issue
Block a user