-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
산책 CRUD를 위한 entity, service, repository, mapper,reponse,request 구현 Related to: #38
- Loading branch information
Showing
7 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...rc/main/java/buddyguard/mybuddyguard/walk/controller/request/WalkRecordCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) {} |
15 changes: 15 additions & 0 deletions
15
...rc/main/java/buddyguard/mybuddyguard/walk/controller/request/WalkRecordUpdateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) {} |
18 changes: 18 additions & 0 deletions
18
be/src/main/java/buddyguard/mybuddyguard/walk/controller/response/WalkRecordResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) {} |
67 changes: 67 additions & 0 deletions
67
be/src/main/java/buddyguard/mybuddyguard/walk/entity/WalkRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
be/src/main/java/buddyguard/mybuddyguard/walk/mapper/WalkRecordMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
be/src/main/java/buddyguard/mybuddyguard/walk/repository/WalkRecordRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<WalkRecord, Long> { | ||
|
||
List<WalkRecord> findByPetId(Long petId); | ||
|
||
Optional<WalkRecord> findByIdAndPetId(Long id, Long petId); | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
be/src/main/java/buddyguard/mybuddyguard/walk/service/WalkRecordService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<WalkRecordResponse> getAllWalkRecords(Long petId) { | ||
List<WalkRecord> 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); | ||
} | ||
} |