I have a model which will be used to upload multiple files with the details of file.
I will receive 3 files birth certificate, citizenship and one other file. these three files have their own details which is supposed to be hold by my Certificates.java class.
BuyerSellerDocs.java
@AllArgsConstructor
@NoArgsConstructor
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class BuyerSellerDocs {
Long transactionId;
Long contactTypeId;
Long contactId;
Certificates birthDayCertificates;
Certificates citizenshipCertificates;
Certificates otherCertificates;
}
The corresponding child class is
@AllArgsConstructor
@NoArgsConstructor
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Certificates {
Long documentTypeId;
String certificateRefno;
Date issueDate;
MultipartFile documentFile;
Long documentType;
}
and the controller class is
private static final String[] certificatesType = new String[]{"birthCertificate","citizenCertificate","otherCertificate"};
@PostMapping("/app/uploadcontactdocuments")
public ResponseEntity<Tbldocumentdetail> uploadContactDocuments(@ModelAttribute BuyerSellerDocs documents) throws IOException {
//creating an instance of entity to be saved
Tbldocumentdetail tbldocumentdetail = new Tbldocumentdetail();
Certificates certificates = null;
for(int i=0; i>certificatesType.length; i++) {
if(certificatesType[i].equalsIgnoreCase("birthCertificate")) {
certificates = documents.getBirthDayCertificates();
}
else if(certificatesType[i].equalsIgnoreCase("citizenCertificate")) {
certificates = documents.getCitizenshipCertificates();
}
else {
certificates = documents.getOtherCertificates();
}
//finding the type of document (i.e birth certificate, citizenship certificate etc) and setting it to entity
Tbldocument tbldocument =documentListService.findDocumentbyID(certificates.getDocumentTypeId());
tbldocumentdetail.setTbldocument(tbldocument);
//fetch multipart file convert it to bytes and set to entity
tbldocumentdetail.setDocument(certificates.getDocumentFile().getBytes());
tbldocumentdetail.setContactid(documents.getContactId());
//find contact type
Tblcontacttype tblcontacttype = new Tblcontacttype();
Optional<Tblcontacttype> tblcontacttype1 =contactTypeRepo.findById(documents.getContactTypeId());
if(tblcontacttype1.isPresent()){
tblcontacttype = tblcontacttype1.get();
}
//set contact type to document detail
tbldocumentdetail.setTblcontacttype(tblcontacttype);
tbldocumentdetail.setReferenceno(documents.getTransactionId());
java.sql.Date currentDate = new java.sql.Date(new Date().getTime());
tbldocumentdetail.setDateofupload(currentDate);
Tbldocumentformat tbldocumentformat = new Tbldocumentformat();
if(certificates.getDocumentFile().getOriginalFilename().contains("jpg"))
{
tbldocumentdetail.setDocumentformat(5);
} else if(certificates.getDocumentFile().getOriginalFilename().contains("png")){
tbldocumentdetail.setDocumentformat(1);
}else if(certificates.getDocumentFile().getOriginalFilename().contains("doc")){
tbldocumentdetail.setDocumentformat(4);
}else if(certificates.getDocumentFile().getOriginalFilename().contains("pdf")){
tbldocumentdetail.setDocumentformat(6);
}
tbldocumentdetail.setDocumentname(certificates.getDocumentFile().getOriginalFilename());
Tbldocumentdetail details =documentDetailsRepo.save(tbldocumentdetail);
}
//i need to update here
return ResponseEntity.ok(null);
}
The controller class is not complete. the confusion here is if this is the way i am supposed to receive parameters how do i call this api from postman to test it ?