Few improvements such as
application.properties.prod file new DTO for response when adding notice
This commit is contained in:
@@ -77,8 +77,12 @@ public class ImageController {
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public ResponseEntity<RequestResponseDTO> deleteImage(@PathVariable("id") String filename) {
|
||||
@DeleteMapping("/delete/{filename}")
|
||||
public ResponseEntity<RequestResponseDTO> deleteImage(@PathVariable("filename") String filename) {
|
||||
if(filename == null) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RequestResponseDTO("Filename is empty."));
|
||||
}
|
||||
|
||||
try {
|
||||
imageService.deleteImage(uploadDir, filename);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new RequestResponseDTO("Image deleted successfully."));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package _11.asktpk.artisanconnectbackend.controller;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeAdditionDTO;
|
||||
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
@@ -37,22 +38,22 @@ public class NoticeController {
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public ResponseEntity<String> addNotice(@RequestBody NoticeDTO dto) {
|
||||
public ResponseEntity<NoticeAdditionDTO> addNotice(@RequestBody NoticeDTO dto) {
|
||||
if (!clientService.clientExists(dto.getClientId())) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body("Nie znaleziono klienta o ID: " + dto.getClientId());
|
||||
.body(new NoticeAdditionDTO("Nie znaleziono klienta o ID: " + dto.getClientId()));
|
||||
}
|
||||
|
||||
if (dto.getCategory() == null) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Nie ma takiej kategorii");
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new NoticeAdditionDTO("Nie ma takiej kategorii"));
|
||||
}
|
||||
|
||||
dto.setPublishDate(java.time.LocalDateTime.now());
|
||||
|
||||
noticeService.addNotice(dto);
|
||||
Long newNoticeId = noticeService.addNotice(dto);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("Dodano ogłoszenie.");
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(new NoticeAdditionDTO(newNoticeId ,"Dodano ogłoszenie."));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter @Setter
|
||||
public class NoticeAdditionDTO {
|
||||
public Long noticeId;
|
||||
public String message;
|
||||
|
||||
public NoticeAdditionDTO(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public NoticeAdditionDTO(Long noticeId, String message) {
|
||||
this.noticeId = noticeId;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,8 @@ import java.util.List;
|
||||
|
||||
public interface ImageRepository extends JpaRepository<Image, Long> {
|
||||
List<Image> findByNoticeId(Long noticeId);
|
||||
|
||||
boolean existsImageByImageNameEqualsIgnoreCase(String imageName);
|
||||
|
||||
void deleteByImageNameEquals(String imageName);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import _11.asktpk.artisanconnectbackend.repository.ImageRepository;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -17,6 +18,7 @@ import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class ImageService {
|
||||
private final ImageRepository imageRepository;
|
||||
|
||||
@@ -63,6 +65,8 @@ public class ImageService {
|
||||
public void deleteImage(String imageDirectory, String imageName) throws IOException {
|
||||
Path imagePath = Path.of(imageDirectory, imageName);
|
||||
|
||||
deleteImageRecordFromDB(imageName);
|
||||
|
||||
if (Files.exists(imagePath)) {
|
||||
Files.delete(imagePath);
|
||||
} else {
|
||||
@@ -80,4 +84,10 @@ public class ImageService {
|
||||
.map(Image::getImageName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void deleteImageRecordFromDB(String imageName) {
|
||||
if(imageRepository.existsImageByImageNameEqualsIgnoreCase(imageName)) {
|
||||
imageRepository.deleteByImageNameEquals(imageName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,8 +68,8 @@ public class NoticeService {
|
||||
return toDTO(notice);
|
||||
}
|
||||
|
||||
public void addNotice(NoticeDTO dto) {
|
||||
noticeRepository.save(fromDTO(dto));
|
||||
public Long addNotice(NoticeDTO dto) {
|
||||
return noticeRepository.save(fromDTO(dto)).getIdNotice();
|
||||
}
|
||||
|
||||
public boolean noticeExists(Long id) {
|
||||
|
||||
@@ -15,5 +15,5 @@ spring.jpa.defer-datasource-initialization=true
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
|
||||
file.upload-dir=/Users/andsol/Desktop/uploads
|
||||
spring.servlet.multipart.max-file-size=5MB
|
||||
spring.servlet.multipart.max-request-size=5MB
|
||||
spring.servlet.multipart.max-file-size=10MB
|
||||
spring.servlet.multipart.max-request-size=10MB
|
||||
20
src/main/resources/application.properties.prod
Normal file
20
src/main/resources/application.properties.prod
Normal file
@@ -0,0 +1,20 @@
|
||||
spring.application.name=ArtisanConnectBackend
|
||||
|
||||
## PostgreSQL
|
||||
spring.datasource.url=jdbc:postgresql://db:5432/postgres
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
|
||||
#initial data for db injection
|
||||
spring.sql.init.data-locations=classpath:sql/data.sql
|
||||
spring.sql.init.mode=always
|
||||
spring.jpa.defer-datasource-initialization=true
|
||||
|
||||
# create and drop table, good for testing, production set to none or comment it
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
|
||||
#file.upload-dir=/Users/andsol/Desktop/uploads
|
||||
file.upload-dir=/app/images
|
||||
spring.servlet.multipart.max-file-size=10MB
|
||||
spring.servlet.multipart.max-request-size=10MB
|
||||
Reference in New Issue
Block a user