Skip to content

Commit

Permalink
feat: walk CRUD 구현
Browse files Browse the repository at this point in the history
산책 CRUD를 위한 entity, service, repository, mapper,reponse,request 구현
Related to: #38
  • Loading branch information
jieunjin committed Sep 25, 2024
1 parent 73a77b7 commit aaa3d9e
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 0 deletions.
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
) {}
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
) {}
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
) {}
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;
}
}
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();
}
}
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);

}
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);
}
}

0 comments on commit aaa3d9e

Please sign in to comment.