Skip to content

Commit 6f2df88

Browse files
authored
Merge pull request #40 from volo-db/document-del-route
Document del route
2 parents 18b43c8 + 6039411 commit 6f2df88

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dev.urner.volodb.exception;
2+
3+
public class VolunteerDocumentNotFoundException extends RuntimeException {
4+
public VolunteerDocumentNotFoundException(String message) {
5+
super(message);
6+
}
7+
}

src/main/java/dev/urner/volodb/rest/VolunteerRestController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ public VolunteerDocument postNewDocument(
216216
return volunteerService.saveDocument(document, documentTypeId, volunteerId, documentName, principal.getUsername());
217217
}
218218

219+
@PatchMapping("/{volunteerId}/documents/{documentId}")
220+
public VolunteerDocument deleteDocument(@PathVariable int volunteerId, @PathVariable int documentId,
221+
@RequestBody Map<String, Object> fields) {
222+
223+
return volunteerService.updateDocument(documentId, volunteerId, fields);
224+
}
225+
226+
@DeleteMapping("/{volunteerId}/documents/{documentId}")
227+
public String deleteDocument(@PathVariable int volunteerId, @PathVariable int documentId) {
228+
229+
return volunteerService.deleteDocument(documentId, volunteerId);
230+
}
231+
219232
// **********************************
220233
// Exceptionhandling:
221234
// **********************************

src/main/java/dev/urner/volodb/service/FileService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.minio.MakeBucketArgs;
1111
import io.minio.MinioClient;
1212
import io.minio.PutObjectArgs;
13+
import io.minio.RemoveObjectArgs;
1314
import lombok.RequiredArgsConstructor;
1415

1516
@Service
@@ -47,4 +48,15 @@ public void saveFile(MultipartFile file, String bucket, String object) {
4748
throw new RuntimeException(e.getMessage());
4849
}
4950
}
51+
52+
public void deleteFile(String bucket, String object) {
53+
try {
54+
minioClient.removeObject(RemoveObjectArgs.builder()
55+
.bucket(bucket)
56+
.object(object)
57+
.build());
58+
} catch (Exception e) {
59+
throw new RuntimeException(e.getMessage());
60+
}
61+
}
5062
}

src/main/java/dev/urner/volodb/service/VolunteerDocumentService.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
import java.util.Map;
44
import java.util.List;
55

6-
import org.springframework.data.domain.Page;
7-
import org.springframework.data.domain.PageRequest;
86
import org.springframework.data.domain.Sort;
97
import org.springframework.stereotype.Service;
108

119
import dev.urner.volodb.dao.VolunteerDocumentDAO;
1210
import dev.urner.volodb.entity.VolunteerDocument;
13-
import dev.urner.volodb.entity.VolunteerNote;
14-
import dev.urner.volodb.exception.ContactTypeNotFoundException;
1511
import dev.urner.volodb.exception.DocumentNotFoundException;
1612
import jakarta.transaction.Transactional;
1713
import lombok.RequiredArgsConstructor;
@@ -52,7 +48,7 @@ public VolunteerDocument update(int documentId, Map<String, Object> fields) {
5248

5349
@Transactional
5450
public void deleteById(int documentId) {
55-
documentTypeService.deleteById(documentId);
51+
volunteerDocumentDAO.deleteById(documentId);
5652
}
5753

5854
public List<VolunteerDocument> findAllByVolunteerId(int volunteerId, String sortField, boolean descending,

src/main/java/dev/urner/volodb/service/VolunteerService.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import dev.urner.volodb.entity.Gender;
55
import dev.urner.volodb.entity.Volunteer;
66
import dev.urner.volodb.entity.VolunteerDocument;
7+
import dev.urner.volodb.entity.VolunteerDocumentType;
78
import dev.urner.volodb.entity.Enums.OngoingLegalProceedingsState;
9+
import dev.urner.volodb.exception.VolunteerDocumentNotFoundException;
810
import dev.urner.volodb.exception.VolunteerInvalidFormatException;
911
import dev.urner.volodb.exception.VolunteerNotFoundException;
1012

@@ -278,4 +280,44 @@ public VolunteerDocument saveDocument(MultipartFile file, int documentTypeId, in
278280
return volunteerDocumentService.save(voloDoc);
279281
}
280282

283+
public VolunteerDocument updateDocument(int documentId, int volunteerId, Map<String, Object> fields) {
284+
285+
VolunteerDocument dbDocument = volunteerDocumentService.findById(documentId);
286+
287+
if (dbDocument == null)
288+
throw new VolunteerDocumentNotFoundException("Document with Id " + documentId + " not found.");
289+
290+
fields.forEach((key, value) -> {
291+
292+
// Name
293+
if (key.toLowerCase() == "name") {
294+
dbDocument.setName(value.toString());
295+
}
296+
297+
// documentType
298+
if (key.toLowerCase().equals("documenttype")) {
299+
VolunteerDocumentType dbType = volunteerDocumentTypeService.findById(Integer.parseInt(value.toString()));
300+
dbDocument.setDocumentType(dbType);
301+
}
302+
303+
});
304+
305+
return volunteerDocumentService.save(dbDocument);
306+
}
307+
308+
public String deleteDocument(int documentId, int volunteerId) {
309+
VolunteerDocument dbDocument = volunteerDocumentService.findById(documentId);
310+
311+
if (dbDocument.getVolunteerId() != volunteerId)
312+
throw new VolunteerInvalidFormatException("Volunteer do not have a document with this id.");
313+
314+
String bucket = dbDocument.getPath().split("/")[0];
315+
String object = dbDocument.getPath().replace(bucket + "/", "");
316+
317+
volunteerDocumentService.deleteById(documentId);
318+
fileService.deleteFile(bucket, object);
319+
320+
return "File with Id " + documentId + " deleted.";
321+
}
322+
281323
}

0 commit comments

Comments
 (0)