-
Notifications
You must be signed in to change notification settings - Fork 1
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] #39 - Log 생성 API 구현 #43
Changes from 22 commits
2b12dd1
a2f133c
3114349
2a5faac
302ae6f
358bc0a
849aae7
a3f84fa
71881c8
4299cf8
680318c
dce1bbe
a575330
dd338d4
9895da1
438dbce
9647200
e0a0cc8
8cbfa38
af316b8
0296c9f
02e1f96
91bf6df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
|
||
} |
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 | ||
) { | ||
} |
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 칭찬 스티커 0.5개 붙여드리겠습니다~ |
||
List<Log> findLatestLogByKeyResultId(@Param("keyResultId") Long keyResultId); | ||
|
||
} |
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()) // 바꾸는 값 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p5; |
||
.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()); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p5; |
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p2;
이거 남기고 if(dto.taskList() !=null) 내 로직 지워야할것 같습니다!