Skip to content

Commit

Permalink
feat: 클래스 상세보기시 해당 클래스 호스트가 로그인한 유저인지 확인 및 boolean 전달
Browse files Browse the repository at this point in the history
  • Loading branch information
yubin-im committed Jul 5, 2024
1 parent 32a0a6f commit c61530c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public ResponseEntity<ApiResponse> lessonDetail(@PathVariable Long lessonId) {

// 클래스 상세보기
@GetMapping("/lesson/{lessonId}")
public ResponseEntity<ApiResponse> lessonInfo(@PathVariable Long lessonId) {
LessonInfoResDto lessonInfoResDto = lessonService.lessonInfo(lessonId);
public ResponseEntity<ApiResponse> lessonInfo(@AuthenticationPrincipal Long userId, @PathVariable Long lessonId) {
LessonInfoResDto lessonInfoResDto = lessonService.lessonInfo(userId, lessonId);
return ResponseEntity.ok(new ApiResponse<>(true, "ok", lessonInfoResDto));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class LessonInfoResDto {
private String materials;
private int capacity;
private String categoryName;
private boolean isHostMe;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface LessonService {
List<OpenedLessonsResDto> openedLessons(Long userId);

// 클래스 상세보기
LessonInfoResDto lessonInfo(Long lessonId);
LessonInfoResDto lessonInfo(Long userId, Long lessonId);

// 클래스 등록하기
void createLesson(Long userId, CreateLessonReqDto createLessonReqDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.hanaro.hanafun.lesson.service.LessonService;
import com.hanaro.hanafun.lessondate.domain.LessonDateEntity;
import com.hanaro.hanafun.lessondate.domain.LessonDateRepository;
import com.hanaro.hanafun.user.domain.UserEntity;
import com.hanaro.hanafun.user.domain.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -55,9 +57,17 @@ public List<OpenedLessonsResDto> openedLessons(Long userId) {
// 클래스 상세보기
@Transactional
@Override
public LessonInfoResDto lessonInfo(Long lessonId) {
public LessonInfoResDto lessonInfo(Long userId, Long lessonId) {
LessonEntity lesson = lessonRepository.findById(lessonId).orElseThrow(() -> new LessonNotFoundException());

// 해당 클래스의 호스트가 로그인한 유저인지 확인
boolean isHostMe;
if(userId.equals(lesson.getHostEntity().getUserEntity().getUserId())) {
isHostMe = true;
} else {
isHostMe = false;
}

LessonInfoResDto lessonInfoResDto = LessonInfoResDto.builder()
.lessonId(lessonId)
.image(lesson.getImage())
Expand All @@ -68,6 +78,7 @@ public LessonInfoResDto lessonInfo(Long lessonId) {
.materials(lesson.getMaterials())
.capacity(lesson.getCapacity())
.categoryName(lesson.getCategoryEntity().getCategoryName())
.isHostMe(isHostMe)
.build();

return lessonInfoResDto;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/hanaro/hanafun/LessonServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void lessonInfoTest_Success() {
when(lessonRepository.findById(lessonId1)).thenReturn(Optional.of(lesson1));

// When
LessonInfoResDto result = lessonService.lessonInfo(lessonId1);
LessonInfoResDto result = lessonService.lessonInfo(userId1, lessonId1);

// Then
assertEquals(lessonId1, result.getLessonId());
Expand All @@ -164,7 +164,7 @@ void lessonInfoTest_Fail() {

// When, Then
assertThrows(LessonNotFoundException.class, () -> {
lessonService.lessonInfo(lessonId1);
lessonService.lessonInfo(userId1, lessonId1);
});
}

Expand Down

0 comments on commit c61530c

Please sign in to comment.