Skip to content

Commit

Permalink
[Feat] Ai count api #215 (#216)
Browse files Browse the repository at this point in the history
* test(UserAcceptanceTest): 사용자의 질문 가능 횟수를 조회하는 인수테스트 작성

* feat(UserQueryApiV2): 사용자 질문 횟수 조회 API 구현 완료
  • Loading branch information
zbqmgldjfh authored Aug 25, 2024
1 parent ab1563d commit e7221c5
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum ResponseCodeAndMessages {
BOOKMARK_LOOKUP_SUCCESS(HttpStatus.OK.value(), "북마크 조회에 성공하였습니다"),
FEEDBACK_SAVE_SUCCESS(HttpStatus.OK.value(), "피드백 저장에 성공하였습니다"),
FEEDBACK_SEARCH_SUCCESS(HttpStatus.OK.value(), "피드백 조회에 성공하였습니다"),
ASK_COUNT_LOOKUP_SUCCESS(HttpStatus.OK.value(), "질문 가능 횟수 조회에 성공하였습니다"),

/* Alert */
ALERT_SEARCH_SUCCESS(HttpStatus.OK.value(), "예약 알림 조회에 성공하였습니다"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.kustacks.kuring.common.annotation.RestWebAdapter;
import com.kustacks.kuring.common.dto.BaseResponse;
import com.kustacks.kuring.user.adapter.in.web.dto.UserAIAskCountResponse;
import com.kustacks.kuring.user.adapter.in.web.dto.UserBookmarkResponse;
import com.kustacks.kuring.user.adapter.in.web.dto.UserCategoryNameResponse;
import com.kustacks.kuring.user.adapter.in.web.dto.UserDepartmentNameResponse;
import com.kustacks.kuring.user.application.port.in.UserQueryUseCase;
import com.kustacks.kuring.user.application.port.in.dto.UserAIAskCountResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -72,4 +74,15 @@ public ResponseEntity<BaseResponse<List<UserBookmarkResponse>>> lookupUserBookma

return ResponseEntity.ok().body(new BaseResponse<>(BOOKMARK_LOOKUP_SUCCESS, responses));
}

@Operation(summary = "사용자 질문 가능횟수 조회", description = "사용자의 남은 질문횟수와 가능한 질문 횟수를 조회합니다")
@SecurityRequirement(name = USER_TOKEN_HEADER_KEY)
@GetMapping("/ask-counts")
public ResponseEntity<BaseResponse<UserAIAskCountResponse>> lookupUserAIAskCount(
@RequestHeader(USER_TOKEN_HEADER_KEY) String userToken
) {
UserAIAskCountResult result = userQueryUseCase.lookupUserAIAskCount(userToken);
UserAIAskCountResponse response = UserAIAskCountResponse.from(result);
return ResponseEntity.ok().body(new BaseResponse<>(ASK_COUNT_LOOKUP_SUCCESS, response));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kustacks.kuring.user.adapter.in.web.dto;

import com.kustacks.kuring.user.application.port.in.dto.UserAIAskCountResult;

public record UserAIAskCountResponse(
int leftAskCount,
int maxAskCount
) {
public static UserAIAskCountResponse from(UserAIAskCountResult result) {
return new UserAIAskCountResponse(result.leftAskCount(), result.maxAskCount());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kustacks.kuring.user.application.port.in;

import com.kustacks.kuring.user.application.port.in.dto.UserAIAskCountResult;
import com.kustacks.kuring.user.application.port.in.dto.UserBookmarkResult;
import com.kustacks.kuring.user.application.port.in.dto.UserCategoryNameResult;
import com.kustacks.kuring.user.application.port.in.dto.UserDepartmentNameResult;
Expand All @@ -10,4 +11,6 @@ public interface UserQueryUseCase {
List<UserCategoryNameResult> lookupSubscribeCategories(String userToken);
List<UserDepartmentNameResult> lookupSubscribeDepartments(String userToken);
List<UserBookmarkResult> lookupUserBookmarkedNotices(String userToken);

UserAIAskCountResult lookupUserAIAskCount(String userToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kustacks.kuring.user.application.port.in.dto;

public record UserAIAskCountResult(
int leftAskCount,
int maxAskCount
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.kustacks.kuring.notice.domain.CategoryName;
import com.kustacks.kuring.notice.domain.DepartmentName;
import com.kustacks.kuring.user.application.port.in.UserQueryUseCase;
import com.kustacks.kuring.user.application.port.in.dto.UserAIAskCountResult;
import com.kustacks.kuring.user.application.port.in.dto.UserBookmarkResult;
import com.kustacks.kuring.user.application.port.in.dto.UserCategoryNameResult;
import com.kustacks.kuring.user.application.port.in.dto.UserDepartmentNameResult;
Expand Down Expand Up @@ -55,6 +56,12 @@ public List<UserBookmarkResult> lookupUserBookmarkedNotices(String userToken) {
return lookupAllBookmarkByIds(bookmarkIds);
}

@Override
public UserAIAskCountResult lookupUserAIAskCount(String userToken) {
User user = findUserByToken(userToken);
return new UserAIAskCountResult(user.getQuestionCount(), User.MONTHLY_QUESTION_COUNT);
}

private List<UserBookmarkResult> lookupAllBookmarkByIds(List<String> bookmarkIds) {
return noticeQueryPort.findAllByBookmarkIds(bookmarkIds)
.stream()
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/kustacks/kuring/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class User implements Serializable {
@Column(name = "deleted", nullable = false)
private boolean deleted = Boolean.FALSE;

@Getter(AccessLevel.PUBLIC)
@Column(columnDefinition = "integer default 0")
private Integer questionCount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,22 @@ void lookup_bookmark() {
// then
북마크_조회_응답_확인(북마크_조회_응답);
}

/**
* Given : 사용자가 AI 질문을 한적이 있다
* When : 남은 질문 가능 횟수를 조회한다
* Then : 성공적으로 질문 가능횟수와 가능한 최대 질문 횟수를 반환한다
*/
@DisplayName("[v2] 사용자는 자신의 남은 질문 가능 횟수를 조회할 수 있다")
@Test
void lookup_ask_count() {
// given
doNothing().when(firebaseSubscribeService).validationToken(anyString());

// when
var 질문_횟수_조회_응답 = 남은_질문_횟수_조회(USER_FCM_TOKEN);

// then
질문_횟수_응답_검증(질문_횟수_조회_응답);
}
}
19 changes: 19 additions & 0 deletions src/test/java/com/kustacks/kuring/acceptance/UserStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,23 @@ public class UserStep {
.then().log().all()
.extract();
}

public static ExtractableResponse<Response> 남은_질문_횟수_조회(String userToken) {
return RestAssured
.given().log().all()
.header("User-Token", userToken)
.when().get("/api/v2/users/ask-counts")
.then().log().all()
.extract();
}

public static void 질문_횟수_응답_검증(ExtractableResponse<Response> 질문_횟수_조회_응답) {
assertAll(
() -> assertThat(질문_횟수_조회_응답.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(질문_횟수_조회_응답.jsonPath().getInt("code")).isEqualTo(200),
() -> assertThat(질문_횟수_조회_응답.jsonPath().getString("message")).isEqualTo("질문 가능 횟수 조회에 성공하였습니다"),
() -> assertThat(질문_횟수_조회_응답.jsonPath().getInt("data.leftAskCount")).isEqualTo(2),
() -> assertThat(질문_횟수_조회_응답.jsonPath().getInt("data.maxAskCount")).isEqualTo(2)
);
}
}

0 comments on commit e7221c5

Please sign in to comment.