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;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.InputStream;
import java.time.LocalDate;
import static com.kaluwa.enterprises.babycarebackendservice.constants.Status.STATUS_NEW;
......@@ -54,6 +55,7 @@ public class BabyDto {
private String notes;
private String uniqKey;
private UserDto user;
private byte[] imageData;
@NotNull(message = "Logged user id is required")
@NotEmpty(message = "Logged user id is required")
......
......@@ -9,14 +9,20 @@ import com.kaluwa.enterprises.babycarebackendservice.mappers.BabyMapper;
import com.kaluwa.enterprises.babycarebackendservice.model.Baby;
import com.kaluwa.enterprises.babycarebackendservice.model.Document;
import com.kaluwa.enterprises.babycarebackendservice.service.BabyService;
import com.kaluwa.enterprises.babycarebackendservice.service.DocumentService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
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;
@Service
......@@ -25,12 +31,13 @@ public class BabyServiceImpl implements BabyService {
private final BabyDao babyDao;
private final BabyMapper babyMapper;
private final DocumentDao documentDao;
private final DocumentService documentService;
public BabyServiceImpl(BabyDao babyDao, BabyMapper babyMapper, DocumentDao documentDao) {
public BabyServiceImpl(BabyDao babyDao, BabyMapper babyMapper, DocumentDao documentDao, DocumentService documentService) {
this.babyDao = babyDao;
this.babyMapper = babyMapper;
this.documentDao = documentDao;
this.documentService = documentService;
}
@Override
......@@ -49,7 +56,19 @@ public class BabyServiceImpl implements BabyService {
public List<BabyDto> getAllBabies() {
log.info("Inside baby service getAllBabies method");
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) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
......@@ -100,10 +119,8 @@ public class BabyServiceImpl implements BabyService {
if (babyOp.isEmpty()) {
throw new BadRequestAlertException("Baby not found", "baby", "baby.error");
} else {
Optional<Document> documentOp = documentDao.findById(babyOp.get().getDocumentId());
if (documentOp.isPresent()) {
documentDao.deleteById(babyOp.get().getDocumentId());
}
Optional<Document> documentOp = documentDao.findByTableNameAndUniqKeyAndDocumentType(BABY_TABLE, babyOp.get().getUniqKey(), IMAGE);
documentOp.ifPresent(document -> documentDao.deleteById(document.getDocumentId()));
babyDao.deleteById(babyId);
ResponseDto responseDto = new ResponseDto();
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