Skip to content

Commit

Permalink
feat: #25 운동 종료 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
bngsh committed Sep 7, 2023
1 parent d3ff2c7 commit cd3f188
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.whyranoid.walkie.controller;

import com.whyranoid.walkie.dto.HistoryDto;
import com.whyranoid.walkie.dto.WalkingDto;
import com.whyranoid.walkie.dto.WalkingLikeDto;
import com.whyranoid.walkie.service.WalkingService;
Expand Down Expand Up @@ -47,4 +48,11 @@ public ResponseEntity<Long> countWalkingLike(@RequestParam Long walkieId, @Reque
public ResponseEntity<WalkingLikeDto> getTotalWalkingLike(@RequestParam Long walkieId, @RequestParam String authId) {
return ResponseEntity.ok(walkingService.getTotalWalkingLike(walkieId, authId));
}

@Operation(description = "운동 종료 시 데이터 저장하기")
@ApiResponse(description = "저장 성공 시 기록의 DB pk값, 실패 시 -1")
@PostMapping("/save")
public ResponseEntity<Long> saveWalkingHistory(@RequestBody HistoryDto historyDto) {
return ResponseEntity.ok(walkingService.saveWalkingHistory(historyDto));
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/whyranoid/walkie/dto/HistoryDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.whyranoid.walkie.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class HistoryDto {

@Schema(description = "요청 필수 파라미터 - 워키 아이디", requiredMode = Schema.RequiredMode.REQUIRED, example = "3")
private Long walkieId;

@Schema(description = "요청 필수 파라미터 - 구글 uid", requiredMode = Schema.RequiredMode.REQUIRED, example = "super-secret-key")
private String authId;

@Schema(description = "요청 필수 파라미터 - 히스토리 아이디", requiredMode = Schema.RequiredMode.REQUIRED, example = "57245")
private Long historyId;

@Schema(description = "요청 필수 파라미터 - 운동을 끝낸 시각", requiredMode = Schema.RequiredMode.REQUIRED, example = "2023-09-09 09:09:09")
private String endTime;

@Schema(description = "요청 필수 파라미터 - 운동한 시간(초)", requiredMode = Schema.RequiredMode.REQUIRED, example = "39431")
private Integer totalTime;

@Schema(description = "요청 파라미터 - 운동한 거리(미터)", requiredMode = Schema.RequiredMode.REQUIRED, example = "1842.5")
private Double distance;

@Schema(description = "요청 파라미터 - 소비 칼로리", requiredMode = Schema.RequiredMode.REQUIRED, example = "300")
private Integer calorie;

@Schema(description = "요청 파라미터 - 걸음 수", requiredMode = Schema.RequiredMode.REQUIRED, example = "3094")
private Integer step;

@Schema(description = "요청 필수 파라미터 - 달린 경로", requiredMode = Schema.RequiredMode.REQUIRED)
private String path;

@Builder
public HistoryDto(Long walkieId, String authId, Long historyId, String date, String endTime, Integer totalTime, Double distance, Integer calorie, Integer step, String path) {
this.walkieId = walkieId;
this.authId = authId;
this.historyId = historyId;
this.endTime = endTime;
this.totalTime = totalTime;
this.distance = distance;
this.calorie = calorie;
this.step = step;
this.path = path;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.whyranoid.walkie.repository.querydsl;

import com.whyranoid.walkie.dto.HistoryDto;
import com.whyranoid.walkie.dto.WalkingLikeDto;

public interface WalkingLikeRepositoryCustom {

Long findWalkingLikeCount(Long walkieId);

WalkingLikeDto findWalkingLikePeople(Long walkieId);

Long updateCurrentWalkingHistory(HistoryDto historyDto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.JPQLQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.whyranoid.walkie.dto.HistoryDto;
import com.whyranoid.walkie.dto.QWalkieDto;
import com.whyranoid.walkie.dto.WalkieDto;
import com.whyranoid.walkie.dto.WalkingLikeDto;
Expand All @@ -11,6 +12,7 @@
import java.util.List;

import static com.whyranoid.walkie.domain.QHistory.history;
import static com.whyranoid.walkie.domain.QWalkie.walkie;
import static com.whyranoid.walkie.domain.QWalkingLike.walkingLike;

@RequiredArgsConstructor
Expand All @@ -37,6 +39,27 @@ public WalkingLikeDto findWalkingLikePeople(Long walkieId) {
.build();
}

@Override
public Long updateCurrentWalkingHistory(HistoryDto historyDto) {
Long updatedHistory = queryFactory.update(history)
.set(history.distance, historyDto.getDistance())
.set(history.endTime, historyDto.getEndTime())
.set(history.totalTime, historyDto.getTotalTime())
.set(history.calorie, historyDto.getCalorie())
.set(history.step, historyDto.getStep())
.set(history.path, historyDto.getPath())
.where(history.historyId.eq(historyDto.getHistoryId()))
.execute();

Long updatedWalkie = queryFactory.update(walkie)
.set(walkie.status, 'N')
.where(walkie.userId.eq(historyDto.getWalkieId()))
.execute();

if (updatedHistory == 1 && updatedWalkie == 1) return historyDto.getHistoryId();
return -1L;
}

public JPQLQuery<Long> findCurrentHistory(Long walkieId) {
return JPAExpressions
.select(history.historyId)
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/whyranoid/walkie/service/WalkingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.whyranoid.walkie.domain.History;
import com.whyranoid.walkie.domain.Walkie;
import com.whyranoid.walkie.domain.WalkingLike;
import com.whyranoid.walkie.dto.HistoryDto;
import com.whyranoid.walkie.dto.WalkingDto;
import com.whyranoid.walkie.dto.WalkingLikeDto;
import com.whyranoid.walkie.repository.HistoryRepository;
Expand Down Expand Up @@ -33,7 +34,8 @@ public Long startWalking(WalkingDto walkingDto) {
Walkie walkie = walkieRepository.findById(walkingDto.getWalkieId()).orElseThrow(EntityNotFoundException::new);

History input = History.builder()
.startTime(walkingDto.getStartTime().toString())
//.date(walkingDto.getStartTime()) // TODO 날짜 형식 반영
.startTime(walkingDto.getStartTime()) // TODO 날짜 형식 반영
.user(walkie)
.build();

Expand Down Expand Up @@ -75,4 +77,13 @@ public WalkingLikeDto getTotalWalkingLike(Long walkieId, String authId) {

return walkingLikeRepository.findWalkingLikePeople(walkieId);
}

public Long saveWalkingHistory(HistoryDto historyDto) {
Walkie authWalkie = walkieRepository.findByAuthId(historyDto.getAuthId()).orElseThrow(EntityNotFoundException::new);

if (!authWalkie.getUserId().equals(historyDto.getWalkieId())) throw new InvalidParameterException();

return walkingLikeRepository.updateCurrentWalkingHistory(historyDto);

}
}

0 comments on commit cd3f188

Please sign in to comment.