Skip to content

Commit

Permalink
feat: 수업 API 구현 (#76)
Browse files Browse the repository at this point in the history
* feat: Added CustomLessonRequest class to represent API parameters

* feat: Added Lesson related API

* feat: Add error status enum value to describe Lesson related errors

* test: Add tests for lesson related logic using Mockito

* feat: 수업 관련 Api 설계 (#26)

* feat: Added CustomLessonRequest class to represent API parameters

* feat: Added Lesson related API

* feat: Add error status enum value to describe Lesson related errors

* test: Add tests for lesson related logic using Mockito

* feat: Add exception and error status enum value to describe Lesson related errors

* remove: remove test class files

* fix: change CustomLessonRequest fields

* feat: Implement delete endpoint for userlesson

* test: Add tests for deleteUserLesson using Mockito

* refactor: lesson 관련 dto, converter 통일화 (#67) (#75)

---------

Co-authored-by: Gyuhyeok99 <[email protected]>
  • Loading branch information
leesewon00 and Gyuhyeok99 authored Feb 12, 2024
1 parent 1e271e8 commit 6541532
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 242 deletions.
15 changes: 9 additions & 6 deletions src/main/java/UMC/campusNote/common/code/status/ErrorStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ public enum ErrorStatus implements BaseErrorCode {

// 가장 일반적인 응답
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON500", "서버 에러, 관리자에게 문의 바랍니다."),
BAD_REQUEST(HttpStatus.BAD_REQUEST,"COMMON400","잘못된 요청입니다."),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED,"COMMON401","로그인 인증이 필요합니다."),
BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "잘못된 요청입니다."),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "로그인 인증이 필요합니다."),
FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "금지된 요청입니다."),

// 레슨 관련 에러
LESSON_NOT_FOUND(HttpStatus.BAD_REQUEST, "LESSON4001","존재하지 않는 수업."),
LESSONS_NOT_FOUND(HttpStatus.BAD_REQUEST, "LESSON4002","해당학기 수업 정보가 없습니다."),
LESSONS_ALREADY_HAVE(HttpStatus.BAD_REQUEST, "LESSON4003","중복된 수업."),
LESSON_NOT_FOUND(HttpStatus.BAD_REQUEST, "LESSON4001", "존재하지 않는 수업."),
LESSONS_NOT_FOUND(HttpStatus.BAD_REQUEST, "LESSON4002", "해당학기 수업 정보가 없습니다."),
LESSONS_ALREADY_HAVE(HttpStatus.BAD_REQUEST, "LESSON4003", "중복된 수업."),
LESSON_REQUEST_CREATE_BINDING_FAULT(HttpStatus.BAD_REQUEST, "LESSON4004", "파라미터 바인딩 실패."),

// 유저레슨 관련 에러
USERLESSON_NOT_FOUND(HttpStatus.BAD_REQUEST, "USERLESSON4001", "존재하지 않는 유저레슨."),

// 크롤링 관련 에러
CRAWLING_URL_INVALID(HttpStatus.BAD_REQUEST, "URL4001", "적절하지 않은 url."),
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/UMC/campusNote/lesson/TestDataInit.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import UMC.campusNote.common.ApiResponse;
import UMC.campusNote.common.code.status.ErrorStatus;
import UMC.campusNote.common.code.status.SuccessStatus;
import UMC.campusNote.lesson.dto.CrawlingRequest;
import UMC.campusNote.lesson.dto.LessonDto;
import UMC.campusNote.lesson.dto.LessonRequestDTO;
import UMC.campusNote.lesson.dto.LessonResponseDTO;
import UMC.campusNote.lesson.exception.CrawlingException;
import UMC.campusNote.lesson.service.CrawlingService;
import UMC.campusNote.user.entity.User;
Expand All @@ -30,19 +30,17 @@ public class CrawlingController {
//https://everytime.kr/@zjmhATXF5czcDHm78zDZ
//https://everytime.kr/@MVXWO0FP3qdoA0tGis4c

//warn id gap
@PostMapping(value = "/api/v1/crawl")
public ApiResponse<List<LessonDto>> crawling(@AuthenticationPrincipal User user,
@Valid @RequestBody CrawlingRequest crawlingRequest,
BindingResult bindingResult) {
public ApiResponse<List<LessonResponseDTO.FindResultDTO>> crawling(@AuthenticationPrincipal User user,
@Valid @RequestBody LessonRequestDTO.CrawlingDTO crawlingDTO,
BindingResult bindingResult) {

log.info("enter CrawlngController : [post] /api/v1/crawl");

if (bindingResult.hasErrors()) {
// ex. null, invalid request body data
throw new CrawlingException(ErrorStatus.CRAWLING_URL_BINDING_FAULT);
}
List<LessonDto> results = crawlingService.action(crawlingRequest.getUrl(), user.getId());
List<LessonResponseDTO.FindResultDTO> results = crawlingService.action(crawlingDTO.getUrl(), user.getId());

return ApiResponse.of(SuccessStatus.CRAWLING_OK, results);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package UMC.campusNote.lesson.controller;

import UMC.campusNote.common.ApiResponse;
import UMC.campusNote.common.code.status.ErrorStatus;
import UMC.campusNote.common.code.status.SuccessStatus;
import UMC.campusNote.lesson.dto.CustomLessonRequest;
import UMC.campusNote.lesson.dto.LessonDto;
import UMC.campusNote.lesson.dto.LessonRequestDTO;
import UMC.campusNote.lesson.dto.LessonResponseDTO;
import UMC.campusNote.lesson.exception.LessonException;
import UMC.campusNote.lesson.service.LessonService;
import UMC.campusNote.user.entity.User;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -19,33 +23,46 @@
public class LessonController {
private final LessonService lessonService;

// 특정 학기 수업 가져오기
@GetMapping(value = "/api/v1/lessons")
public ApiResponse<List<LessonDto>> findLessons(@AuthenticationPrincipal User user,
@RequestParam(name = "semester") String semester) {
log.info("enter LessonController : [get] /api/v1/lessons?semester={}", semester);
public ApiResponse<List<LessonResponseDTO.FindResultDTO>> findLessons(@AuthenticationPrincipal User user,
@RequestParam(name = "attendedSemester") String attendedSemester) {
log.info("enter LessonController : [get] /api/v1/lessons?attendedSemester={}", attendedSemester);

List<LessonDto> result = lessonService.findLessons(user.getId(), semester);
List<LessonResponseDTO.FindResultDTO> result = lessonService.findLessons(user.getId(), attendedSemester);

return ApiResponse.of(SuccessStatus.OK, result);
}

@GetMapping(value = "/api/v1/lessons/{lessonId}")
public ApiResponse<LessonDto> findLessonDetails(@PathVariable(name = "lessonId") Long lessonId) {
public ApiResponse<LessonResponseDTO.FindResultDTO> findLessonDetails(@PathVariable(name = "lessonId") Long lessonId) {
log.info("enter LessonController : [get] /api/v1/lessons/{}", lessonId);

LessonDto result = lessonService.findLessonDetails(lessonId);
LessonResponseDTO.FindResultDTO result = lessonService.findLessonDetails(lessonId);

return ApiResponse.of(SuccessStatus.OK, result);
}

@PostMapping(value = "/api/v1/lessons/new")
public ApiResponse<Long> createCustomLesson(@AuthenticationPrincipal User user,
@RequestBody CustomLessonRequest customLessonRequest) {
public ApiResponse<Long> createLesson(@AuthenticationPrincipal User user,
@Valid @RequestBody LessonRequestDTO.CreateDTO createDTO,
BindingResult bindingResult) {
log.info("enter LessonController : [post] /api/v1/lessons/new");
if (bindingResult.hasErrors()) {
throw new LessonException(ErrorStatus.LESSON_REQUEST_CREATE_BINDING_FAULT);
}

Long lessonId = lessonService.createCustomLesson(user.getId(), customLessonRequest);
Long lessonId = lessonService.createLesson(user.getId(), createDTO);

return ApiResponse.of(SuccessStatus.LESSON_CREATE, lessonId);
}

@DeleteMapping(value = "/api/v1/lessons/{lessonId}")
public ApiResponse<Long> deleteUserLesson(@AuthenticationPrincipal User user,
@PathVariable(name = "lessonId") Long lessonId) {
log.info("enter LessonController : [delete] /api/v1/lessons/{}", lessonId);

Long id = lessonService.deleteUserLesson(user.getId(), lessonId);

return ApiResponse.of(SuccessStatus.OK, id);
}
}
37 changes: 17 additions & 20 deletions src/main/java/UMC/campusNote/lesson/converter/LessonConverter.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
package UMC.campusNote.lesson.converter;


import UMC.campusNote.lesson.dto.LessonDto;
import UMC.campusNote.lesson.dto.LessonDetailsDto;
import UMC.campusNote.lesson.dto.LessonResponseDTO;
import UMC.campusNote.lesson.entity.Lesson;
import UMC.campusNote.mapping.UserLesson;

import java.util.ArrayList;
import java.util.List;

public class LessonConverter {
public static List<LessonDto> userLessonsToLessonDtos(List<UserLesson> action) {
public static List<LessonResponseDTO.FindResultDTO> toCreateResultDTOList(List<UserLesson> action) {

List<LessonDto> responses = new ArrayList<>();
List<LessonResponseDTO.FindResultDTO> responses = new ArrayList<>();

for (UserLesson memberLesson : action) {
// Member member = memberLesson.getMember();
Lesson lesson = memberLesson.getLesson();

boolean dup = false;
LessonDto dupResponse = null;
LessonResponseDTO.FindResultDTO dupResponse = null;
if (!responses.isEmpty()) {
for (LessonDto lessonDto : responses) {
if (lessonDto.getLessonName().equals(lesson.getLessonName())) {
for (LessonResponseDTO.FindResultDTO findResultDTO : responses) {
if (findResultDTO.getLessonName().equals(lesson.getLessonName())) {
dup = true;
dupResponse = lessonDto;
dupResponse = findResultDTO;
}
}
}

LessonDetailsDto lessonDetailsDto = LessonDetailsDto.builder()
LessonResponseDTO.FindResultDetailsDTO findResultDetailsDTO = LessonResponseDTO.FindResultDetailsDTO.builder()
.professorName(lesson.getProfessorName())
.location(lesson.getLocation())
.startTime(lesson.getStartTime())
Expand All @@ -38,39 +35,39 @@ public static List<LessonDto> userLessonsToLessonDtos(List<UserLesson> action) {
.build();

if (dup) {
dupResponse.getLessonDetailsDtoList().add(lessonDetailsDto);
dupResponse.getFindResultDetailsDTOList().add(findResultDetailsDTO);
} else {
LessonDto lessonDto = LessonDto.builder()
LessonResponseDTO.FindResultDTO findResultDTO = LessonResponseDTO.FindResultDTO.builder()
.id(lesson.getId())
.university(lesson.getUniversity())
.semester(lesson.getSemester())
.lessonName(lesson.getLessonName())
.build();
lessonDto.getLessonDetailsDtoList().add(lessonDetailsDto);
findResultDTO.getFindResultDetailsDTOList().add(findResultDetailsDTO);

responses.add(lessonDto);
responses.add(findResultDTO);
}
}
return responses;
}

public static LessonDto oneLessonToLessonDto(Lesson lesson) {
public static LessonResponseDTO.FindResultDTO toCreateResultDTO(Lesson lesson) {

LessonDetailsDto build = LessonDetailsDto.builder()
LessonResponseDTO.FindResultDetailsDTO build = LessonResponseDTO.FindResultDetailsDTO.builder()
.professorName(lesson.getProfessorName())
.location(lesson.getLocation())
.startTime(lesson.getStartTime())
.runningTime(lesson.getRunningTime())
.dayOfWeek(lesson.getDayOfWeek())
.build();
List<LessonDetailsDto> list = new ArrayList<>();
List<LessonResponseDTO.FindResultDetailsDTO> list = new ArrayList<>();
list.add(build);
return LessonDto.builder()
return LessonResponseDTO.FindResultDTO.builder()
.id(lesson.getId())
.university(lesson.getUniversity())
.semester(lesson.getSemester())
.lessonName(lesson.getLessonName())
.lessonDetailsDtoList(list)
.findResultDetailsDTOList(list)
.build();
}

Expand Down
17 changes: 0 additions & 17 deletions src/main/java/UMC/campusNote/lesson/dto/CrawlingRequest.java

This file was deleted.

17 changes: 0 additions & 17 deletions src/main/java/UMC/campusNote/lesson/dto/LessonDetailsDto.java

This file was deleted.

23 changes: 0 additions & 23 deletions src/main/java/UMC/campusNote/lesson/dto/LessonDto.java

This file was deleted.

40 changes: 40 additions & 0 deletions src/main/java/UMC/campusNote/lesson/dto/LessonRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package UMC.campusNote.lesson.dto;

import jakarta.validation.constraints.NotNull;
import lombok.*;

public class LessonRequestDTO {
@AllArgsConstructor
@Getter
@Setter
@NoArgsConstructor
@Builder
public static class CreateDTO {
@NotNull
private String attendedSemester;
@NotNull
private String lessonName;
@NotNull
private String semester;
@NotNull
private String professorName;
@NotNull
private String location;
@NotNull
private String startTime;
@NotNull
private String runningTime;
@NotNull
private String dayOfWeek;
}

@AllArgsConstructor
@Getter
@Setter
@NoArgsConstructor
public static class CrawlingDTO {
@NotNull
private String url;
}

}
35 changes: 35 additions & 0 deletions src/main/java/UMC/campusNote/lesson/dto/LessonResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package UMC.campusNote.lesson.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

public class LessonResponseDTO {
@Data
@AllArgsConstructor
@Builder
public static class FindResultDTO {
private Long id;
private String university;
private String semester;
private String lessonName;

@Builder.Default
private List<FindResultDetailsDTO> findResultDetailsDTOList = new ArrayList<>();
}

@Data
@AllArgsConstructor
@Builder
public static class FindResultDetailsDTO {
private String professorName;
private String location;
private String startTime;
private String runningTime;
private String dayOfWeek;
}

}
Loading

0 comments on commit 6541532

Please sign in to comment.