Commit 404837a9 authored by Ishankha K.C's avatar Ishankha K.C

attach image to babyDto

parent e3c40e4e
...@@ -6,6 +6,7 @@ import lombok.AllArgsConstructor; ...@@ -6,6 +6,7 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.io.InputStream;
import java.time.LocalDate; import java.time.LocalDate;
import static com.kaluwa.enterprises.babycarebackendservice.constants.Status.STATUS_NEW; import static com.kaluwa.enterprises.babycarebackendservice.constants.Status.STATUS_NEW;
...@@ -54,6 +55,7 @@ public class BabyDto { ...@@ -54,6 +55,7 @@ public class BabyDto {
private String notes; private String notes;
private String uniqKey; private String uniqKey;
private UserDto user; private UserDto user;
private byte[] imageData;
@NotNull(message = "Logged user id is required") @NotNull(message = "Logged user id is required")
@NotEmpty(message = "Logged user id is required") @NotEmpty(message = "Logged user id is required")
......
...@@ -9,14 +9,20 @@ import com.kaluwa.enterprises.babycarebackendservice.mappers.BabyMapper; ...@@ -9,14 +9,20 @@ import com.kaluwa.enterprises.babycarebackendservice.mappers.BabyMapper;
import com.kaluwa.enterprises.babycarebackendservice.model.Baby; import com.kaluwa.enterprises.babycarebackendservice.model.Baby;
import com.kaluwa.enterprises.babycarebackendservice.model.Document; import com.kaluwa.enterprises.babycarebackendservice.model.Document;
import com.kaluwa.enterprises.babycarebackendservice.service.BabyService; import com.kaluwa.enterprises.babycarebackendservice.service.BabyService;
import com.kaluwa.enterprises.babycarebackendservice.service.DocumentService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import static com.kaluwa.enterprises.babycarebackendservice.constants.DocumentTypes.IMAGE;
import static com.kaluwa.enterprises.babycarebackendservice.constants.TableNames.BABY_TABLE;
import static com.kaluwa.enterprises.babycarebackendservice.utils.Utils.uniqKeyGenerator; import static com.kaluwa.enterprises.babycarebackendservice.utils.Utils.uniqKeyGenerator;
@Service @Service
...@@ -25,12 +31,13 @@ public class BabyServiceImpl implements BabyService { ...@@ -25,12 +31,13 @@ public class BabyServiceImpl implements BabyService {
private final BabyDao babyDao; private final BabyDao babyDao;
private final BabyMapper babyMapper; private final BabyMapper babyMapper;
private final DocumentDao documentDao; private final DocumentDao documentDao;
private final DocumentService documentService;
public BabyServiceImpl(BabyDao babyDao, BabyMapper babyMapper, DocumentDao documentDao, DocumentService documentService) {
public BabyServiceImpl(BabyDao babyDao, BabyMapper babyMapper, DocumentDao documentDao) {
this.babyDao = babyDao; this.babyDao = babyDao;
this.babyMapper = babyMapper; this.babyMapper = babyMapper;
this.documentDao = documentDao; this.documentDao = documentDao;
this.documentService = documentService;
} }
@Override @Override
...@@ -49,7 +56,19 @@ public class BabyServiceImpl implements BabyService { ...@@ -49,7 +56,19 @@ public class BabyServiceImpl implements BabyService {
public List<BabyDto> getAllBabies() { public List<BabyDto> getAllBabies() {
log.info("Inside baby service getAllBabies method"); log.info("Inside baby service getAllBabies method");
try { try {
return babyMapper.toDtoList(babyDao.findAll());
List<BabyDto> babyList = babyMapper.toDtoList(babyDao.findAll());
babyList.forEach(babyDto -> {
Optional<Document> documentOp = documentDao.findByTableNameAndUniqKeyAndDocumentType(BABY_TABLE, babyDto.getUniqKey(), IMAGE);
documentOp.ifPresent(document -> {
byte[] imageData = documentService.getImage(BABY_TABLE, babyDto.getUniqKey()).getBody();
if (imageData != null) {
babyDto.setImageData(imageData);
}
});
});
return babyList;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error"); throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
...@@ -100,10 +119,8 @@ public class BabyServiceImpl implements BabyService { ...@@ -100,10 +119,8 @@ public class BabyServiceImpl implements BabyService {
if (babyOp.isEmpty()) { if (babyOp.isEmpty()) {
throw new BadRequestAlertException("Baby not found", "baby", "baby.error"); throw new BadRequestAlertException("Baby not found", "baby", "baby.error");
} else { } else {
Optional<Document> documentOp = documentDao.findById(babyOp.get().getDocumentId()); Optional<Document> documentOp = documentDao.findByTableNameAndUniqKeyAndDocumentType(BABY_TABLE, babyOp.get().getUniqKey(), IMAGE);
if (documentOp.isPresent()) { documentOp.ifPresent(document -> documentDao.deleteById(document.getDocumentId()));
documentDao.deleteById(babyOp.get().getDocumentId());
}
babyDao.deleteById(babyId); babyDao.deleteById(babyId);
ResponseDto responseDto = new ResponseDto(); ResponseDto responseDto = new ResponseDto();
responseDto.setId(babyId); responseDto.setId(babyId);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment