Commit 5e9b943d authored by Ishankha K.C's avatar Ishankha K.C

baby wet checker

parent e6183d6e
......@@ -48,7 +48,8 @@ public class SecurityConfig {
"/swagger-ui.html",
"/emotional/video-process",
"/test/**",
"/activity-logs/add"
"/activity-logs/add",
"/baby/**"
};
@Bean
......
......@@ -10,5 +10,5 @@ import java.util.List;
public interface ActivityLogDao extends JpaRepository<ActivityLog, Long> {
List<ActivityLog> findAllByOrderByActivityLogIdDesc();
List<ActivityLog> findAllByActivityLogTypeOrderByActivityLogIdDesc(String activityLogType);
List<ActivityLog> findTop5ByActivityLogTypeOrderByActivityLogIdDesc(String activityLogType);
}
\ No newline at end of file
package com.kaluwa.enterprises.babycarebackendservice.dao;
import com.kaluwa.enterprises.babycarebackendservice.dto.BabyDto;
import com.kaluwa.enterprises.babycarebackendservice.model.Baby;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface BabyDao extends JpaRepository<Baby, Long> {
boolean existsByDeviceUidAndBabyIdNot(String deviceUid, Long babyId);
boolean existsByDeviceUid(String deviceUid);
List<Baby> findByUserUserId(Long userId);
Optional<Baby> findByDeviceUid(String deviceId);
}
......@@ -57,6 +57,7 @@ public class BabyDto {
private String uniqKey;
private UserDto user;
private byte[] imageData;
private boolean wet;
@NotNull(message = "Logged user id is required")
@NotEmpty(message = "Logged user id is required")
......
......@@ -51,6 +51,7 @@ public class Baby {
private String secondaryEmergencyContactNumber;
private String notes;
private String uniqKey;
private boolean wet;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", referencedColumnName = "userId", nullable = false)
private User user;
......
......@@ -28,9 +28,11 @@ public class BabyController {
}
@GetMapping()
public ResponseEntity<List<BabyDto>> getAllbabies() {
public ResponseEntity<List<BabyDto>> getAllbabies(
@RequestParam(value = "userId", required = false) Long userId
) {
log.info("Inside baby controller getAllbabies method");
return ResponseEntity.ok(babyService.getAllBabies());
return ResponseEntity.ok(babyService.getAllBabies(userId));
}
@GetMapping("/{babyId}")
......@@ -50,4 +52,10 @@ public class BabyController {
log.info("Inside baby controller deleteBaby method");
return ResponseEntity.ok(babyService.deleteBaby(babyId));
}
@GetMapping("/byDeviceId/{deviceId}")
public ResponseEntity<BabyDto> getBabyByDeviceId(@PathVariable String deviceId) {
log.info("Inside baby controller getBabyByDeviceId method");
return ResponseEntity.ok(babyService.getBabyByDeviceId(deviceId));
}
}
......@@ -8,11 +8,13 @@ import java.util.List;
public interface BabyService {
BabyDto createBaby(BabyDto babyDto);
List<BabyDto> getAllBabies();
List<BabyDto> getAllBabies(Long userId);
BabyDto getBabyById(Long babyId);
BabyDto updateBaby(Long babyId, BabyDto babyDto);
ResponseDto deleteBaby(Long babyId);
BabyDto getBabyByDeviceId(String deviceId);
}
......@@ -43,7 +43,7 @@ public class ActivityLogServiceImpl implements ActivityLogService {
log.info("Inside getAllActivityLogs method in ActivityLogServiceImpl");
try {
if (activityLogType != null && !activityLogType.isEmpty()) {
return ResponseEntity.ok(activityLogMapper.listToDto(activityLogDao.findAllByActivityLogTypeOrderByActivityLogIdDesc(activityLogType)));
return ResponseEntity.ok(activityLogMapper.listToDto(activityLogDao.findTop5ByActivityLogTypeOrderByActivityLogIdDesc(activityLogType)));
} else {
return ResponseEntity.ok(activityLogMapper.listToDto(activityLogDao.findAllByOrderByActivityLogIdDesc()));
}
......
......@@ -12,13 +12,8 @@ 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;
......@@ -68,6 +63,7 @@ public class BabyServiceImpl implements BabyService {
if (babyDto.getUserId() == null) {
throw new BadRequestAlertException("User id is required", "baby", "baby.error");
}
babyDto.setWet(false);
return babyMapper.toDto(babyDao.save(babyMapper.toEntity(babyDto)));
} catch (Exception e) {
e.printStackTrace();
......@@ -76,21 +72,25 @@ public class BabyServiceImpl implements BabyService {
}
@Override
public List<BabyDto> getAllBabies() {
public List<BabyDto> getAllBabies(Long userId) {
log.info("Inside baby service getAllBabies method");
try {
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);
}
List<BabyDto> babyList;
if (userId != null) {
babyList = babyMapper.toDtoList(babyDao.findByUserUserId(userId));
} else {
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();
......@@ -176,4 +176,20 @@ public class BabyServiceImpl implements BabyService {
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
}
}
@Override
public BabyDto getBabyByDeviceId(String deviceId) {
log.info("Inside baby service getBabyByDeviceId method");
try {
Optional<Baby> babyOp = babyDao.findByDeviceUid(deviceId);
if (babyOp.isEmpty()) {
throw new BadRequestAlertException("Baby not found", "baby", "baby.error");
} else {
return babyMapper.toDto(babyOp.get());
}
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
}
}
}
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