diff --git a/be/src/main/java/buddyguard/mybuddyguard/walk/controller/request/WalkRecordCreateRequest.java b/be/src/main/java/buddyguard/mybuddyguard/walk/controller/request/WalkRecordCreateRequest.java new file mode 100644 index 00000000..20cf1200 --- /dev/null +++ b/be/src/main/java/buddyguard/mybuddyguard/walk/controller/request/WalkRecordCreateRequest.java @@ -0,0 +1,16 @@ +package buddyguard.mybuddyguard.walk.controller.request; + +import java.time.LocalDate; +import java.time.LocalTime; +import buddyguard.mybuddyguard.s3.entity.S3Images; + +public record WalkRecordCreateRequest( + Long petId, + LocalTime startTime, + LocalTime endTime, + LocalTime duration, + LocalDate createdAt, + Double distance, + String description, + S3Images image +) {} diff --git a/be/src/main/java/buddyguard/mybuddyguard/walk/controller/request/WalkRecordUpdateRequest.java b/be/src/main/java/buddyguard/mybuddyguard/walk/controller/request/WalkRecordUpdateRequest.java new file mode 100644 index 00000000..c70d5716 --- /dev/null +++ b/be/src/main/java/buddyguard/mybuddyguard/walk/controller/request/WalkRecordUpdateRequest.java @@ -0,0 +1,15 @@ +package buddyguard.mybuddyguard.walk.controller.request; + +import java.time.LocalDate; +import java.time.LocalTime; +import buddyguard.mybuddyguard.s3.entity.S3Images; + +public record WalkRecordUpdateRequest( + LocalTime startTime, + LocalTime endTime, + LocalTime duration, + LocalDate createdAt, + Double distance, + String description, + S3Images image +) {} diff --git a/be/src/main/java/buddyguard/mybuddyguard/walk/controller/response/WalkRecordResponse.java b/be/src/main/java/buddyguard/mybuddyguard/walk/controller/response/WalkRecordResponse.java new file mode 100644 index 00000000..08571fb9 --- /dev/null +++ b/be/src/main/java/buddyguard/mybuddyguard/walk/controller/response/WalkRecordResponse.java @@ -0,0 +1,18 @@ +package buddyguard.mybuddyguard.walk.controller.response; + +import java.time.LocalDate; +import java.time.LocalTime; +import lombok.Builder; + +@Builder +public record WalkRecordResponse( + Long id, + Long petId, + LocalTime startTime, + LocalTime endTime, + LocalTime duration, + LocalDate createdAt, + Double distance, + String description, + String imageUrl // S3 이미지 URL +) {} diff --git a/be/src/main/java/buddyguard/mybuddyguard/walk/entity/WalkRecord.java b/be/src/main/java/buddyguard/mybuddyguard/walk/entity/WalkRecord.java new file mode 100644 index 00000000..ef158c1f --- /dev/null +++ b/be/src/main/java/buddyguard/mybuddyguard/walk/entity/WalkRecord.java @@ -0,0 +1,67 @@ +package buddyguard.mybuddyguard.walk.entity; + +import buddyguard.mybuddyguard.s3.entity.S3Images; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; +import java.time.LocalDate; +import java.time.LocalTime; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Table(name = "WALK_RECORDS") +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class WalkRecord { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "pet_id", nullable = false) + private Long petId; + + @Column(name = "start_time", nullable = false) + private LocalTime startTime; + + @Column(name = "end_time", nullable = false) + private LocalTime endTime; + + @Column(name = "duration", nullable = false) + private LocalTime duration; + + @Column(name = "created_at", nullable = false) + private LocalDate createdAt; + + @Column(name = "distance", nullable = false) + private Double distance; + + @Column(name = "description", nullable = false, length = 500) + private String description; + + @OneToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "image_id", nullable = false) + private S3Images image; + + public void update(LocalTime startTime, LocalTime endTime, LocalTime duration, LocalDate createdAt, Double distance, String description, S3Images image) { + this.startTime = startTime; + this.endTime = endTime; + this.duration = duration; + this.createdAt = createdAt; + this.distance = distance; + this.description = description; + this.image = image; + } +} diff --git a/be/src/main/java/buddyguard/mybuddyguard/walk/mapper/WalkRecordMapper.java b/be/src/main/java/buddyguard/mybuddyguard/walk/mapper/WalkRecordMapper.java new file mode 100644 index 00000000..07d87650 --- /dev/null +++ b/be/src/main/java/buddyguard/mybuddyguard/walk/mapper/WalkRecordMapper.java @@ -0,0 +1,35 @@ +package buddyguard.mybuddyguard.walk.mapper; + +import buddyguard.mybuddyguard.walk.controller.request.WalkRecordCreateRequest; +import buddyguard.mybuddyguard.walk.controller.response.WalkRecordResponse; +import buddyguard.mybuddyguard.walk.entity.WalkRecord; + +public class WalkRecordMapper { + + public static WalkRecord toEntity(WalkRecordCreateRequest request) { + return WalkRecord.builder() + .petId(request.petId()) + .startTime(request.startTime()) + .endTime(request.endTime()) + .duration(request.duration()) + .createdAt(request.createdAt()) + .distance(request.distance()) + .description(request.description()) + .image(request.image()) // S3Image 연결 + .build(); + } + + public static WalkRecordResponse toResponse(WalkRecord walkRecord) { + return WalkRecordResponse.builder() + .id(walkRecord.getId()) + .petId(walkRecord.getPetId()) + .startTime(walkRecord.getStartTime()) + .endTime(walkRecord.getEndTime()) + .duration(walkRecord.getDuration()) + .createdAt(walkRecord.getCreatedAt()) + .distance(walkRecord.getDistance()) + .description(walkRecord.getDescription()) + .imageUrl(walkRecord.getImage().getImageUrl()) + .build(); + } +} diff --git a/be/src/main/java/buddyguard/mybuddyguard/walk/repository/WalkRecordRepository.java b/be/src/main/java/buddyguard/mybuddyguard/walk/repository/WalkRecordRepository.java new file mode 100644 index 00000000..3c006864 --- /dev/null +++ b/be/src/main/java/buddyguard/mybuddyguard/walk/repository/WalkRecordRepository.java @@ -0,0 +1,15 @@ +package buddyguard.mybuddyguard.walk.repository; + +import buddyguard.mybuddyguard.walk.entity.WalkRecord; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public class WalkRecordRepository extends JpaRepository { + + List findByPetId(Long petId); + + Optional findByIdAndPetId(Long id, Long petId); + +} diff --git a/be/src/main/java/buddyguard/mybuddyguard/walk/service/WalkRecordService.java b/be/src/main/java/buddyguard/mybuddyguard/walk/service/WalkRecordService.java new file mode 100644 index 00000000..1718eb0e --- /dev/null +++ b/be/src/main/java/buddyguard/mybuddyguard/walk/service/WalkRecordService.java @@ -0,0 +1,72 @@ +package buddyguard.mybuddyguard.walk.service; + +import buddyguard.mybuddyguard.exception.RecordNotFoundException; +import buddyguard.mybuddyguard.walk.controller.response.WalkRecordResponse; +import buddyguard.mybuddyguard.walk.controller.request.WalkRecordCreateRequest; +import buddyguard.mybuddyguard.walk.controller.request.WalkRecordUpdateRequest; +import buddyguard.mybuddyguard.walk.entity.WalkRecord; +import buddyguard.mybuddyguard.walk.mapper.WalkRecordMapper; +import buddyguard.mybuddyguard.walk.repository.WalkRecordRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.stream.Collectors; + +@Slf4j +@Service +@RequiredArgsConstructor +public class WalkRecordService { + + private final WalkRecordRepository walkRecordRepository; + + public List getAllWalkRecords(Long petId) { + List records = walkRecordRepository.findByPetId(petId); + return records.stream() + .map(WalkRecordMapper::toResponse) + .collect(Collectors.toList()); + } + + public WalkRecordResponse getWalkRecord(Long id, Long petId) { + return walkRecordRepository.findByIdAndPetId(id, petId) + .map(WalkRecordMapper::toResponse) + .orElseThrow(RecordNotFoundException::new); + } + + @Transactional + public void createWalkRecord(WalkRecordCreateRequest request) { + WalkRecord walkRecord = WalkRecordMapper.toEntity(request); + + WalkRecord savedWalkRecord = walkRecordRepository.save(walkRecord); + + log.info("SAVED WALK RECORD: {}", savedWalkRecord); + } + + @Transactional + public void updateWalkRecord(Long id, Long petId, WalkRecordUpdateRequest request) { + WalkRecord walkRecord = walkRecordRepository.findByIdAndPetId(id, petId) + .orElseThrow(RecordNotFoundException::new); // 예외 처리 + + walkRecord.update( + request.startTime(), + request.endTime(), + request.duration(), + request.createdAt(), + request.distance(), + request.description(), + request.image() + ); + + walkRecordRepository.save(walkRecord); + } + + @Transactional + public void deleteWalkRecord(Long id, Long petId) { + WalkRecord walkRecord = walkRecordRepository.findByIdAndPetId(id, petId) + .orElseThrow(RecordNotFoundException::new); // 예외 처리 + + walkRecordRepository.delete(walkRecord); + } +}