Skip to content

Commit

Permalink
Merge pull request #167 from Team-HMH/feat/#166-check-day-lock-api
Browse files Browse the repository at this point in the history
feat - ๋‹น์ผ ์ž ๊ธˆ ์—ฌ๋ถ€ ์ „์†ก ๋ฐ ํ™•์ธ api ์ถ”๊ฐ€
  • Loading branch information
kseysh authored Jun 15, 2024
2 parents 1e54d66 + c2df7fd commit f219314
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class Challenge extends BaseTimeEntity {
private Integer period;
private Long goalTime;

private boolean isChallengeFailedToday;

@OneToMany(mappedBy = "challenge", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<ChallengeApp> apps = new ArrayList<>();

Expand All @@ -39,7 +37,6 @@ private Challenge(Integer period, Long userId, Long goalTime, List<ChallengeApp>
this.period = period;
this.userId = userId;
this.goalTime = goalTime;
this.isChallengeFailedToday = false;
this.apps = apps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import sopt.org.hmh.domain.challenge.dto.response.DailyChallengeResponse;
import sopt.org.hmh.domain.challenge.repository.ChallengeRepository;
import sopt.org.hmh.domain.dailychallenge.domain.DailyChallenge;
import sopt.org.hmh.domain.dailychallenge.domain.Status;
import sopt.org.hmh.domain.dailychallenge.repository.DailyChallengeRepository;
import sopt.org.hmh.domain.user.domain.User;
import sopt.org.hmh.domain.user.service.UserService;
Expand Down Expand Up @@ -100,9 +99,6 @@ public DailyChallengeResponse getDailyChallenge(Long userId) {
Challenge challenge = findCurrentChallengeByUserId(userId);

return DailyChallengeResponse.builder()
.status(Boolean.TRUE.equals(challenge.isChallengeFailedToday())
? Status.FAILURE
: Status.NONE)
.goalTime(challenge.getGoalTime())
.apps(challenge.getApps().stream()
.map(app -> new ChallengeAppResponse(app.getAppCode(), app.getGoalTime())).toList())
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/sopt/org/hmh/domain/user/controller/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import java.time.LocalDate;
import org.springframework.http.ResponseEntity;
import sopt.org.hmh.global.auth.UserId;
import sopt.org.hmh.domain.user.dto.request.UserRequest.LockDateRequest;
import sopt.org.hmh.domain.user.dto.response.UserResponse.IsLockTodayResponse;
import sopt.org.hmh.domain.user.dto.response.UserResponse.UserInfoResponse;
import sopt.org.hmh.global.common.response.BaseResponse;
import sopt.org.hmh.global.common.response.EmptyJsonResponse;

public interface UserApi {

@Operation(summary = "๋กœ๊ทธ์•„์›ƒ")
ResponseEntity<BaseResponse<?>> orderLogout();
ResponseEntity<BaseResponse<EmptyJsonResponse>> orderLogout();

@Operation(summary = "์œ ์ € ์ •๋ณด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ")
ResponseEntity<BaseResponse<?>> orderGetUserInfo(@UserId @Parameter(hidden = true) final Long userId);
ResponseEntity<BaseResponse<UserInfoResponse>> orderGetUserInfo(@Parameter(hidden = true) final Long userId);

@Operation(summary = "์œ ์ € ํฌ์ธํŠธ ์ •๋ณด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ")
public ResponseEntity<BaseResponse<?>> orderGetUserPoint(@UserId final Long userId);
ResponseEntity<BaseResponse<Integer>> orderGetUserPoint(@Parameter(hidden = true) final Long userId);

@Operation(
summary = "ํšŒ์› ํƒˆํ‡ด")
ResponseEntity<BaseResponse<?>> orderWithdraw(@UserId @Parameter(hidden = true) final Long userId);
@Operation(summary = "ํšŒ์› ํƒˆํ‡ด")
ResponseEntity<BaseResponse<EmptyJsonResponse>> orderWithdraw(@Parameter(hidden = true) final Long userId);

@Operation(summary = "๋‹น์ผ ์ž ๊ธˆ ์—ฌ๋ถ€ ์ „์†ก")
ResponseEntity<BaseResponse<EmptyJsonResponse>> orderChangeRecentLockDate(
@Parameter(hidden = true) final Long userId, final LockDateRequest request);

@Operation(summary = "๋‹น์ผ ์ž ๊ธˆ ์—ฌ๋ถ€ ํ™•์ธ")
ResponseEntity<BaseResponse<IsLockTodayResponse>> orderGetRecentLockDate(
@Parameter(hidden = true) final Long userId, final LocalDate lockCheckDate);

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package sopt.org.hmh.domain.user.controller;

import jakarta.validation.Valid;
import java.time.LocalDate;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sopt.org.hmh.domain.user.domain.exception.UserSuccess;
import sopt.org.hmh.domain.user.dto.request.UserRequest.LockDateRequest;
import sopt.org.hmh.domain.user.dto.response.UserResponse.IsLockTodayResponse;
import sopt.org.hmh.domain.user.dto.response.UserResponse.UserInfoResponse;
import sopt.org.hmh.domain.user.service.UserService;
import sopt.org.hmh.global.auth.UserId;
import sopt.org.hmh.global.common.response.BaseResponse;
Expand All @@ -16,40 +24,62 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/user")
public class UserController implements UserApi{
public class UserController implements UserApi {

private final UserService userService;

@PostMapping("/logout")
@Override
public ResponseEntity<BaseResponse<?>> orderLogout() {
public ResponseEntity<BaseResponse<EmptyJsonResponse>> orderLogout() {
return ResponseEntity
.status(UserSuccess.LOGOUT_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.LOGOUT_SUCCESS, new EmptyJsonResponse()));
}

@GetMapping
@Override
public ResponseEntity<BaseResponse<?>> orderGetUserInfo(@UserId final Long userId) {
public ResponseEntity<BaseResponse<UserInfoResponse>> orderGetUserInfo(@UserId final Long userId) {
return ResponseEntity
.status(UserSuccess.GET_USER_INFO_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.GET_USER_INFO_SUCCESS, userService.getUserInfo(userId)));
}

@GetMapping("/point")
@Override
public ResponseEntity<BaseResponse<?>> orderGetUserPoint(@UserId final Long userId) {
public ResponseEntity<BaseResponse<Integer>> orderGetUserPoint(@UserId final Long userId) {
return ResponseEntity
.status(UserSuccess.GET_USER_POINT_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.GET_USER_POINT_SUCCESS, userService.getUserInfo(userId).point()));
.body(BaseResponse.success(UserSuccess.GET_USER_POINT_SUCCESS,
userService.getUserInfo(userId).point()));
}

@DeleteMapping
public ResponseEntity<BaseResponse<?>> orderWithdraw(@UserId final Long userId) {
public ResponseEntity<BaseResponse<EmptyJsonResponse>> orderWithdraw(@UserId final Long userId) {
userService.withdraw(userId);
return ResponseEntity
.status(UserSuccess.WITHDRAW_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.WITHDRAW_SUCCESS, new EmptyJsonResponse()));
}

@PostMapping("/daily/lock")
@Override
public ResponseEntity<BaseResponse<EmptyJsonResponse>> orderChangeRecentLockDate(
@UserId final Long userId, @Valid @RequestBody final LockDateRequest request) {
userService.changeRecentLockDate(userId, request.lockDate());
return ResponseEntity
.status(UserSuccess.CHANGE_RECENT_LOCK_DATE_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.CHANGE_RECENT_LOCK_DATE_SUCCESS, new EmptyJsonResponse()));
}

@GetMapping("/daily/lock")
@Override
public ResponseEntity<BaseResponse<IsLockTodayResponse>> orderGetRecentLockDate(
@UserId final Long userId,
@RequestParam(name = "lockCheckDate") @DateTimeFormat(pattern = "yyyy-MM-dd") final LocalDate lockCheckDate) {
return ResponseEntity
.status(UserSuccess.GET_RECENT_LOCK_DATE_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.GET_RECENT_LOCK_DATE_SUCCESS,
userService.checkIsTodayLock(userId, lockCheckDate)));
}

}
7 changes: 7 additions & 0 deletions src/main/java/sopt/org/hmh/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Min;
import java.time.LocalDate;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class User extends BaseTimeEntity {
@Column(columnDefinition = "TEXT")
private String profileImageUrl;

private LocalDate recentLockDate;

private boolean isDeleted = false;
private LocalDateTime deletedAt;

Expand Down Expand Up @@ -88,4 +91,8 @@ public Integer increasePoint(Integer earnedPoint) {
public void changeCurrentChallengeId(Long currentChallengeId) {
this.currentChallengeId = currentChallengeId;
}

public void changeRecentLockDate(LocalDate recentLockDate) {
this.recentLockDate = recentLockDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public enum UserSuccess implements SuccessBase {
GET_USER_POINT_SUCCESS(HttpStatus.OK, "์œ ์ €์˜ ํฌ์ธํŠธ ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š”๋ฐ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค."),
LOGOUT_SUCCESS(HttpStatus.OK, "๋กœ๊ทธ์•„์›ƒ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค."),
WITHDRAW_SUCCESS(HttpStatus.OK, "ํšŒ์› ํƒˆํ‡ด๋ฅผ ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค."),
CHANGE_RECENT_LOCK_DATE_SUCCESS(HttpStatus.OK, "๋‹น์ผ ์ž ๊ธˆ ์—ฌ๋ถ€ ์ „์†ก์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค."),
GET_RECENT_LOCK_DATE_SUCCESS(HttpStatus.OK, "๋‹น์ผ ์ž ๊ธˆ ์—ฌ๋ถ€ ํ™•์ธ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค."),
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sopt.org.hmh.domain.user.dto.request;

import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserRequest {

public record LockDateRequest(
@DateTimeFormat(pattern = "yyyy-MM-dd")
@NotNull(message = "์ž ๊ธˆ ๋‚ ์งœ๋Š” null์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
LocalDate lockDate
) {
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sopt.org.hmh.domain.user.dto.response;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import sopt.org.hmh.domain.user.domain.User;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserResponse {

public record UserInfoResponse(
String name,
Integer point
) {
public static UserInfoResponse of(User user) {
return new UserInfoResponse(
user.getName(),
user.getPoint()
);
}
}

public record IsLockTodayResponse(
boolean isLockToday
) {
}
}
19 changes: 18 additions & 1 deletion src/main/java/sopt/org/hmh/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sopt.org.hmh.domain.user.service;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

Expand All @@ -18,7 +19,8 @@
import sopt.org.hmh.domain.user.domain.UserConstants;
import sopt.org.hmh.domain.user.domain.exception.UserError;
import sopt.org.hmh.domain.user.domain.exception.UserException;
import sopt.org.hmh.domain.user.dto.response.UserInfoResponse;
import sopt.org.hmh.domain.user.dto.response.UserResponse.IsLockTodayResponse;
import sopt.org.hmh.domain.user.dto.response.UserResponse.UserInfoResponse;
import sopt.org.hmh.domain.user.repository.UserRepository;
import sopt.org.hmh.global.auth.social.SocialPlatform;

Expand Down Expand Up @@ -100,4 +102,19 @@ public Long getCurrentChallengeIdByUserId(Long userId) {
return Optional.ofNullable(this.findByIdOrThrowException(userId).getCurrentChallengeId())
.orElseThrow(() -> new UserException(UserError.NOT_FOUND_CURRENT_CHALLENGE_ID));
}

@Transactional
public void changeRecentLockDate(Long userId, LocalDate localDate) {
this.findByIdOrThrowException(userId).changeRecentLockDate(localDate);
}

public IsLockTodayResponse checkIsTodayLock(Long userId, LocalDate lockCheckDate) {
LocalDate userRecentLockDate = this.findByIdOrThrowException(userId).getRecentLockDate();

if (userRecentLockDate == null) {
return new IsLockTodayResponse(false);
}

return new IsLockTodayResponse(userRecentLockDate.equals(lockCheckDate));
}
}

0 comments on commit f219314

Please sign in to comment.