Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import hanium.modic.backend.domain.post.entity.PostEntity;
import hanium.modic.backend.domain.post.repository.PostEntityRepository;
import hanium.modic.backend.domain.user.service.UserCoinService;
import hanium.modic.backend.web.ai.dto.response.GetRemainingGenerationsResponse;
import lombok.RequiredArgsConstructor;

@Service
Expand Down Expand Up @@ -71,4 +72,18 @@ public void consumeRemainingGenerations(final Long userId, final Long postId) {
throw new AppException(AI_IMAGE_PERMISSION_FAIL_EXCEPTION);
}
}

// 유저의 특정 포스트에 대한 남은 생성 횟수 조회
@Transactional(readOnly = true)
public GetRemainingGenerationsResponse getRemainingGenerations(final Long userId, final Long postId) {
AiImagePermissionEntity aiImagePermission = aiImagePermissionRepository.findByUserIdAndPostId(userId,
postId)
.orElseThrow(() -> new AppException(AI_IMAGE_PERMISSION_NOT_FOUND));

return new GetRemainingGenerationsResponse(
aiImagePermission.getId(),
aiImagePermission.getRemainingGenerations()
);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
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 hanium.modic.backend.common.annotation.user.CurrentUser;
import hanium.modic.backend.domain.ai.service.AiImagePermissionService;
import hanium.modic.backend.domain.user.entity.UserEntity;
import hanium.modic.backend.web.ai.dto.request.BuyAiImagePermissionRequest;
import hanium.modic.backend.web.ai.dto.response.GetRemainingGenerationsResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -67,4 +70,22 @@ public ResponseEntity<Void> buyAiImagePermissionWithTicket(

return ResponseEntity.ok().build();
}

@Operation(
summary = "AI 이미지 생성권 남은 횟수 조회",
description = "특정 포스트에 대한 사용자의 AI 이미지 생성권 남은 횟수를 조회합니다.(구매한 이력이 없으면 AI-004 에러)",
responses = {
@ApiResponse(responseCode = "404", description = "해당 유저를 찾을 수 없습니다.[U-002]"),
@ApiResponse(responseCode = "404", description = "AI 이미지 생성권을 구매한 이력이 없습니다.[AI-004]")
}
)
@GetMapping("/remaining-generations")
public ResponseEntity<GetRemainingGenerationsResponse> getRemainingGenerations(
@CurrentUser UserEntity user,
@RequestParam Long postId
) {
GetRemainingGenerationsResponse response = aiImagePermissionService.getRemainingGenerations(user.getId(), postId);

return ResponseEntity.ok(response);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package hanium.modic.backend.web.ai.dto.response;

public record GetRemainingGenerationsResponse(
Long aiImagePermissionId,
Integer remainingGenerations
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,65 @@ void buyAiImagePermission_InvalidRequest_NullPostId() throws Exception {
// then
resultActions.andExpect(status().isBadRequest());
}

@Test
@WithCustomUser(email = "test@test.com")
@DisplayName("AI 이미지 생성권 남은 횟수 조회 - 성공")
void getRemainingGenerations_Success() throws Exception {
// given
UserEntity user = getCurrentUserWithCoin(0L);
PostEntity post = createTestPost(user);

// AI 이미지 권한 생성
AiImagePermissionEntity permission = AiImagePermissionEntity.builder()
.userId(user.getId())
.postId(post.getId())
.remainingGenerations(15)
.build();
aiImagePermissionRepository.save(permission);

// when
ResultActions resultActions = mockMvc.perform(get("/api/ai/image-permissions/remaining-generations")
.param("postId", post.getId().toString()));

// then
resultActions.andExpect(status().isOk())
.andExpect(jsonPath("$.aiImagePermissionId").value(permission.getId()))
.andExpect(jsonPath("$.remainingGenerations").value(15));
}

@Test
@WithCustomUser(email = "test@test.com")
@DisplayName("AI 이미지 생성권 남은 횟수 조회 - 구매한 이력이 없음")
void getRemainingGenerations_PermissionNotFound() throws Exception {
// given
UserEntity user = getCurrentUserWithCoin(0L);
PostEntity post = createTestPost(user);

// when
ResultActions resultActions = mockMvc.perform(get("/api/ai/image-permissions/remaining-generations")
.param("postId", post.getId().toString()));

// then
resultActions.andExpect(status().isNotFound())
.andExpect(jsonPath("$.code").value(AI_IMAGE_PERMISSION_NOT_FOUND.getCode()))
.andExpect(jsonPath("$.message").value(AI_IMAGE_PERMISSION_NOT_FOUND.getMessage()));
}

@Test
@WithCustomUser(email = "test@test.com")
@DisplayName("AI 이미지 생성권 남은 횟수 조회 - 존재하지 않는 포스트")
void getRemainingGenerations_PostNotExist() throws Exception {
// given
Long nonExistentPostId = 9999L;

// when
ResultActions resultActions = mockMvc.perform(get("/api/ai/image-permissions/remaining-generations")
.param("postId", nonExistentPostId.toString()));

// then
resultActions.andExpect(status().isNotFound())
.andExpect(jsonPath("$.code").value(AI_IMAGE_PERMISSION_NOT_FOUND.getCode()))
.andExpect(jsonPath("$.message").value(AI_IMAGE_PERMISSION_NOT_FOUND.getMessage()));
}
}