-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feat] Log 생성 API 구현
- Loading branch information
Showing
16 changed files
with
270 additions
and
21 deletions.
There are no files selected for viewing
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
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
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
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
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
29 changes: 29 additions & 0 deletions
29
src/main/java/org/moonshot/server/domain/log/controller/LogController.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,29 @@ | ||
package org.moonshot.server.domain.log.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.moonshot.server.domain.log.dto.request.LogCreateRequestDto; | ||
import org.moonshot.server.domain.log.service.LogService; | ||
import org.moonshot.server.global.auth.jwt.JwtTokenProvider; | ||
import org.moonshot.server.global.common.response.ApiResponse; | ||
import org.moonshot.server.global.common.response.SuccessType; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.security.Principal; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/v1/log") | ||
public class LogController { | ||
|
||
private final LogService logService; | ||
|
||
@PostMapping | ||
public ApiResponse<?> create(Principal principal, @RequestBody LogCreateRequestDto logCreateRequestDto) { | ||
logService.createRecordLog(JwtTokenProvider.getUserIdFromPrincipal(principal), logCreateRequestDto); | ||
return ApiResponse.success(SuccessType.POST_LOG_SUCCESS); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/moonshot/server/domain/log/dto/request/LogCreateRequestDto.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 org.moonshot.server.domain.log.dto.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
import org.moonshot.server.global.common.model.validator.ValidLimitValue; | ||
|
||
public record LogCreateRequestDto( | ||
Long keyResultId, | ||
|
||
@NotNull(message = "Log의 수치를 입력해주세요.") | ||
@ValidLimitValue | ||
long logNum, | ||
|
||
@NotNull(message = "Log의 체크인 본문을 입력해주세요.") | ||
@Size(min = 1, max = 100, message = "본문은 100자 이하여야 합니다.") | ||
String logContent | ||
) { | ||
} |
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
18 changes: 18 additions & 0 deletions
18
src/main/java/org/moonshot/server/domain/log/repository/LogRepository.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 org.moonshot.server.domain.log.repository; | ||
|
||
import org.moonshot.server.domain.keyresult.model.KeyResult; | ||
import org.moonshot.server.domain.log.model.Log; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.util.List; | ||
|
||
public interface LogRepository extends JpaRepository<Log, Long> { | ||
|
||
List<Log> findAllByKeyResult(KeyResult keyResult); | ||
|
||
@Query("select l FROM Log l JOIN FETCH l.keyResult k WHERE l.keyResult.id = :keyResultId ORDER BY l.id DESC LIMIT 1") | ||
List<Log> findLatestLogByKeyResultId(@Param("keyResultId") Long keyResultId); | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/org/moonshot/server/domain/log/service/LogService.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,99 @@ | ||
package org.moonshot.server.domain.log.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.moonshot.server.domain.keyresult.dto.request.KeyResultCreateRequestDto; | ||
import org.moonshot.server.domain.keyresult.dto.request.KeyResultCreateRequestInfoDto; | ||
import org.moonshot.server.domain.keyresult.dto.request.KeyResultModifyRequestDto; | ||
import org.moonshot.server.domain.keyresult.exception.KeyResultNotFoundException; | ||
import org.moonshot.server.domain.keyresult.model.KeyResult; | ||
import org.moonshot.server.domain.keyresult.repository.KeyResultRepository; | ||
import org.moonshot.server.domain.log.dto.request.LogCreateRequestDto; | ||
import org.moonshot.server.domain.log.model.Log; | ||
import org.moonshot.server.domain.log.model.LogState; | ||
import org.moonshot.server.domain.log.repository.LogRepository; | ||
import org.moonshot.server.domain.user.exception.UserNotFoundException; | ||
import org.moonshot.server.domain.user.model.User; | ||
import org.moonshot.server.domain.user.repository.UserRepository; | ||
import org.moonshot.server.global.auth.exception.AccessDeniedException; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class LogService { | ||
|
||
private final UserRepository userRepository; | ||
private final KeyResultRepository keyResultRepository; | ||
private final LogRepository logRepository; | ||
|
||
@Transactional | ||
public void createRecordLog(Long userId, LogCreateRequestDto request) { | ||
User user = userRepository.findById(userId) | ||
.orElseThrow(UserNotFoundException::new); | ||
KeyResult keyResult = keyResultRepository.findById(request.keyResultId()) | ||
.orElseThrow(KeyResultNotFoundException::new); | ||
if (!keyResult.getObjective().getUser().getId().equals(userId)) { | ||
throw new AccessDeniedException(); | ||
} | ||
List<Log> prevLog = logRepository.findLatestLogByKeyResultId(request.keyResultId()); | ||
long prevNum = -1; | ||
if (!prevLog.isEmpty()) { | ||
prevNum = prevLog.get(0).getCurrNum(); | ||
} | ||
logRepository.save(Log.builder() | ||
.date(LocalDateTime.now()) | ||
.state(LogState.RECORD) | ||
.currNum(request.logNum()) | ||
.prevNum(prevNum) | ||
.content(request.logContent()) | ||
.keyResult(keyResult) | ||
.build()); | ||
} | ||
|
||
@Transactional | ||
public void createUpdateLog(KeyResultModifyRequestDto request, Long keyResultId) { | ||
KeyResult keyResult = keyResultRepository.findById(keyResultId) | ||
.orElseThrow(KeyResultNotFoundException::new); | ||
|
||
logRepository.save(Log.builder() | ||
.date(LocalDateTime.now()) | ||
.state(LogState.UPDATE) | ||
.currNum(request.target()) | ||
.prevNum(keyResult.getTarget()) | ||
.content(request.logContent()) | ||
.keyResult(keyResult) | ||
.build()); | ||
} | ||
|
||
@Transactional | ||
public void createKRLog(Object request, Long keyResultId) { | ||
KeyResult keyResult = keyResultRepository.findById(keyResultId) | ||
.orElseThrow(KeyResultNotFoundException::new); | ||
|
||
if (request instanceof KeyResultCreateRequestInfoDto) { | ||
KeyResultCreateRequestInfoDto dto = (KeyResultCreateRequestInfoDto) request; | ||
logRepository.save(Log.builder() | ||
.date(LocalDateTime.now()) | ||
.state(LogState.CREATE) | ||
.currNum(dto.target()) | ||
.content("") | ||
.keyResult(keyResult) | ||
.build()); | ||
} | ||
if (request instanceof KeyResultCreateRequestDto) { | ||
KeyResultCreateRequestDto dto = (KeyResultCreateRequestDto) request; | ||
logRepository.save(Log.builder() | ||
.date(LocalDateTime.now()) | ||
.state(LogState.CREATE) | ||
.currNum(dto.target()) | ||
.content("") | ||
.keyResult(keyResult) | ||
.build()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.