Skip to content

Commit

Permalink
[FEAT] 유저의 현재 학기 수정 API 개발 (#99)
Browse files Browse the repository at this point in the history
* feat: 유저의 현재 학기 수정 API 개발 (#98)

* feat: 유저의 현재 학기 수정 API 스웨거 명세 추가 (#98)
  • Loading branch information
Gyuhyeok99 authored Feb 15, 2024
1 parent 5684966 commit 1780e47
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public enum SuccessStatus implements BaseCode {
IMAGE_GET_ONE(HttpStatus.OK, "IMAGE200", "노트의 특정 이미지 조회 성공"),
IMAGE_GET_ALL(HttpStatus.OK, "IMAGE200", "노트의 모든 이미지 조회 성공"),
IMAGE_DELETE_ONE(HttpStatus.OK, "IMAGE202", "노트의 특정 이미지 삭제 성공"),
IMAGE_DELETE_ALL(HttpStatus.OK, "IMAGE202", "노트의 모든 이미지 삭제 성공"),
IMAGE_DELETE_ALL(HttpStatus.OK, "IMAGE203", "노트의 모든 이미지 삭제 성공"),

// 수강 학기
ATTENDED_SEMESTER_UPDATE(HttpStatus.OK, "ATTENDED_SEMESTER200", "수강 학기 업데이트 성공"),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public ApiResponse<ImageResponseDTO.GetImageResultDTO> getImageOne(@PathVariable
@DeleteMapping("/{noteId}")
@Operation(summary = "특정 노트의 모든 이미지를 삭제하는 API", description = "특정 노트의 모든 이미지를 삭제하는 API")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "IMAGE202",description = "노트의 모든 이미지 삭제 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "IMAGE203",description = "노트의 모든 이미지 삭제 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "NOTE4001", description = "존재하지 않는 노트.",content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "IMAGE4001", description = "존재하지 않는 이미지.",content = @Content(schema = @Schema(implementation = ApiResponse.class)))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import UMC.campusNote.mapping.UserLesson;
import UMC.campusNote.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;
Expand All @@ -16,4 +19,9 @@ public interface UserLessonRepository extends JpaRepository<UserLesson, Long> {
Optional<UserLesson> findByUserAndAttendedSemesterAndLesson(User user, String attendedSemester, Lesson lesson);

Optional<UserLesson> findByUserAndLesson(User user, Lesson lesson);

@Modifying
@Query("UPDATE UserLesson u SET u.attendedSemester = :attendedSemester WHERE u.user.id = :userId")
int updateAttendedSemester(@Param("userId") Long userId, @Param("attendedSemester") String attendedSemester);

}
19 changes: 18 additions & 1 deletion src/main/java/UMC/campusNote/user/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package UMC.campusNote.user.controller;

import UMC.campusNote.common.ApiResponse;
import UMC.campusNote.user.dto.UserRequestDTO;
import UMC.campusNote.user.dto.UserResponseDTO;
import UMC.campusNote.user.entity.User;
import UMC.campusNote.user.service.UserService;
import lombok.Getter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import static UMC.campusNote.common.code.status.SuccessStatus.ATTENDED_SEMESTER_UPDATE;


@RestController
@RequiredArgsConstructor
Expand All @@ -19,4 +26,14 @@ public ApiResponse<String> get() {
return ApiResponse.onSuccess("GET:: user controller");
}


@PatchMapping("/attended-semester")
@Operation(summary = "유저의 수강 학기 수정 API",description = "유저의 수강 학기 수정 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "ATTENDED_SEMESTER200",description = "수강 학기 업데이트 성공"),
})
public ApiResponse<UserResponseDTO.AttendedSemesterUpdateDTO> updateAttendedSemester(@AuthenticationPrincipal User user, @RequestBody UserRequestDTO.AttendedSemesterUpdateDTO request) {
return ApiResponse.of(ATTENDED_SEMESTER_UPDATE ,userService.updateAttendedSemester(user.getId(), request));
}

}
17 changes: 17 additions & 0 deletions src/main/java/UMC/campusNote/user/converter/UserConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package UMC.campusNote.user.converter;

import UMC.campusNote.user.dto.UserResponseDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class UserConverter {

public static UserResponseDTO.AttendedSemesterUpdateDTO toAttendedSemesterUpdateDTO(String attendedSemester, int count) {
return UserResponseDTO.AttendedSemesterUpdateDTO.builder()
.count(count)
.attendedSemester(attendedSemester)
.build();
}
}
22 changes: 22 additions & 0 deletions src/main/java/UMC/campusNote/user/dto/UserRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package UMC.campusNote.user.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class UserRequestDTO {

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
public static class AttendedSemesterUpdateDTO{

@Schema(description = "수정할 수강 학기", example = "4-2")
@NotNull
String attendedSemester;
}
}
18 changes: 18 additions & 0 deletions src/main/java/UMC/campusNote/user/dto/UserResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package UMC.campusNote.user.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class UserResponseDTO {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class AttendedSemesterUpdateDTO {
int count;
String attendedSemester;
}
}
9 changes: 4 additions & 5 deletions src/main/java/UMC/campusNote/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package UMC.campusNote.user.service;

import UMC.campusNote.user.dto.UserRequestDTO;
import UMC.campusNote.user.dto.UserResponseDTO;
import UMC.campusNote.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
public interface UserService {

UserResponseDTO.AttendedSemesterUpdateDTO updateAttendedSemester(Long userId, UserRequestDTO.AttendedSemesterUpdateDTO request);
}
26 changes: 26 additions & 0 deletions src/main/java/UMC/campusNote/user/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package UMC.campusNote.user.service;


import UMC.campusNote.mapping.repository.UserLessonRepository;
import UMC.campusNote.user.converter.UserConverter;
import UMC.campusNote.user.dto.UserRequestDTO;
import UMC.campusNote.user.dto.UserResponseDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService{

private final UserLessonRepository userLessonRepository;

@Override
@Transactional
public UserResponseDTO.AttendedSemesterUpdateDTO updateAttendedSemester(Long userId, UserRequestDTO.AttendedSemesterUpdateDTO request) {
int count = userLessonRepository.updateAttendedSemester(userId, request.getAttendedSemester());
return UserConverter.toAttendedSemesterUpdateDTO(request.getAttendedSemester(), count);
}
}

0 comments on commit 1780e47

Please sign in to comment.