Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 코드 리팩터링 진행 #27

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public ResponseDto<?> saveBoardingRecord (
return ResponseDto.created(boardingRecordService
.saveBoardingRecord(userId, imageId, boardingRecordRequest));
}

@GetMapping("/boarding-record")
public ResponseDto<?> getBoardingRecord (@UserId Long userId) {
return ResponseDto.ok(boardingRecordService.boardingRecordList(userId));
}
}
14 changes: 12 additions & 2 deletions src/main/java/ice/spot/domain/BoardingRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -22,11 +24,17 @@ public class BoardingRecord {
@Column(name = "time")
private Integer time;

@Column(name = "point")
private Integer point;

@Column(name = "createdAt")
private LocalDateTime createdAt;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@OneToOne
@ManyToOne
@JoinColumn(name = "parking_lot_id")
private ParkingLot parkingLot;

Expand All @@ -35,9 +43,11 @@ public class BoardingRecord {
private Image image;

@Builder
public BoardingRecord(Double distance, Integer time, User user, ParkingLot parkingLot, Image image) {
public BoardingRecord(Double distance, Integer time, Integer point, User user, ParkingLot parkingLot, Image image) {
this.distance = distance;
this.time = time;
this.point = point;
this.createdAt = LocalDateTime.now();
this.user = user;
this.parkingLot = parkingLot;
this.image = image;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/ice/spot/domain/ParkingLot.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -27,6 +30,6 @@ public class ParkingLot {
@Column(name = "detail_address")
private String detailAddress;

@OneToOne(mappedBy = "parkingLot")
private BoardingRecord boardingRecord;
@OneToMany(mappedBy = "parkingLot")
private List<BoardingRecord> boardingRecords = new ArrayList<>();
}
4 changes: 4 additions & 0 deletions src/main/java/ice/spot/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ public void register(String nickname) {
public void updateRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}

public void plusPoint() {
this.point += 100L;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ice.spot.dto.boardingrecord.response;

import ice.spot.dto.user.response.PersonResponse;
import lombok.Builder;

import java.util.List;

@Builder
public record BoardingRecordListResponse(
PersonResponse personResponse,

List<BoardingRecordResponse> boardingRecordResponseList
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ public record BoardingRecordResponse(
Integer time,

@JsonProperty("point")
Integer point,

@JsonProperty("is_parking_lot")
Boolean isParkingLot
Integer point
) {
public static BoardingRecordResponse of(String createdAt, String image, Double distance, Integer time, Integer point, Boolean isParkingLot) {
return new BoardingRecordResponse(createdAt, image, distance, time, point, isParkingLot);
public static BoardingRecordResponse of(String createdAt, String image, Double distance, Integer time, Integer point) {
return new BoardingRecordResponse(createdAt, image, distance, time, point);
}
}
11 changes: 11 additions & 0 deletions src/main/java/ice/spot/dto/user/response/PersonResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ice.spot.dto.user.response;

import lombok.Builder;

@Builder
public record PersonResponse(
String nickname,
Integer recordCount,
Long point
) {
}
29 changes: 27 additions & 2 deletions src/main/java/ice/spot/service/BoardingRecordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import ice.spot.domain.ParkingLot;
import ice.spot.domain.User;
import ice.spot.dto.boardingrecord.request.BoardingRecordRequest;
import ice.spot.dto.boardingrecord.response.BoardingRecordListResponse;
import ice.spot.dto.boardingrecord.response.BoardingRecordResponse;
import ice.spot.dto.user.response.PersonResponse;
import ice.spot.exception.CommonException;
import ice.spot.exception.ErrorCode;
import ice.spot.repository.BoardingRecordRepository;
Expand Down Expand Up @@ -43,16 +45,39 @@ public Boolean saveBoardingRecord(Long userId, Long imageId, BoardingRecordReque
boardingRecordRepository.save(BoardingRecord.builder()
.distance(boardingRecordRequest.distance())
.time(boardingRecordRequest.time())
.point(100)
.user(user)
.image(image)
.parkingLot(parkingLot)
.build());

user.plusPoint();

return Boolean.TRUE;
}

@Transactional(readOnly = true)
public List<BoardingRecordResponse> boardingRecordList(Long userId) {
return null;
public BoardingRecordListResponse boardingRecordList(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_USER));

BoardingRecordListResponse boardingRecordListResponse = BoardingRecordListResponse.builder()
.personResponse(PersonResponse.builder()
.nickname(user.getNickname())
.recordCount(user.getBoardingRecords().size())
.point(user.getPoint())
.build())
.boardingRecordResponseList(user.getBoardingRecords().stream()
.map(boardingRecord ->
BoardingRecordResponse.builder()
.createdAt(boardingRecord.getCreatedAt().toString())
.image(boardingRecord.getImage().getImageUrl())
.distance(boardingRecord.getDistance())
.time(boardingRecord.getTime())
.point(boardingRecord.getPoint())
.build())
.toList())
.build();
return boardingRecordListResponse;
}
}