diff --git a/backend/build.gradle b/backend/build.gradle index d03d17ea..e7a90836 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -21,7 +21,6 @@ configurations { repositories { mavenCentral() - maven { url 'https://jitpack.io' } } dependencies { @@ -41,9 +40,6 @@ dependencies { implementation 'io.jsonwebtoken:jjwt-impl:0.11.2' implementation 'io.jsonwebtoken:jjwt-jackson:0.11.2' - // sj-auth - implementation 'com.github.urinaner:sejong-auth:v0.4.4' - // email implementation 'org.springframework.boot:spring-boot-starter-mail' diff --git a/backend/src/main/java/org/example/backend/admin/service/AdminService.java b/backend/src/main/java/org/example/backend/admin/service/AdminService.java index c3a2d6ce..1b4dd7d8 100644 --- a/backend/src/main/java/org/example/backend/admin/service/AdminService.java +++ b/backend/src/main/java/org/example/backend/admin/service/AdminService.java @@ -2,7 +2,6 @@ import static org.example.backend.admin.exception.AdminExceptionType.ALREADY_EXIST_LOGIN_ID; import static org.example.backend.admin.exception.AdminExceptionType.INVALID_ACCESS_TOKEN; -import static org.example.backend.user.exception.UserExceptionType.NOT_FOUND_USER; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -11,7 +10,6 @@ import org.example.backend.admin.domain.entity.Admin; import org.example.backend.admin.exception.AdminException; import org.example.backend.admin.repository.AdminRepository; -import org.example.backend.user.exception.UserException; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/backend/src/main/java/org/example/backend/global/aop/AuthAdmin.java b/backend/src/main/java/org/example/backend/global/aop/AuthAdmin.java deleted file mode 100644 index c39674ce..00000000 --- a/backend/src/main/java/org/example/backend/global/aop/AuthAdmin.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.example.backend.global.aop; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.PARAMETER) -@Retention(RetentionPolicy.RUNTIME) -public @interface AuthAdmin { -} diff --git a/backend/src/main/java/org/example/backend/global/aop/AuthAdminResolver.java b/backend/src/main/java/org/example/backend/global/aop/AuthAdminResolver.java deleted file mode 100644 index 4ca9542e..00000000 --- a/backend/src/main/java/org/example/backend/global/aop/AuthAdminResolver.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.example.backend.global.aop; - -import static org.example.backend.common.exception.auth.AuthExceptionType.PLEASE_LOGIN; - -import jakarta.servlet.http.HttpServletRequest; -import lombok.RequiredArgsConstructor; -import org.example.backend.admin.service.AdminService; -import org.example.backend.common.exception.auth.AuthException; -import org.example.backend.jwt.JWTUtil; -import org.springframework.core.MethodParameter; -import org.springframework.stereotype.Component; -import org.springframework.web.method.support.HandlerMethodArgumentResolver; -import org.springframework.web.method.support.ModelAndViewContainer; -import org.springframework.web.context.request.NativeWebRequest; - - -@Component -@RequiredArgsConstructor -public class AuthAdminResolver implements HandlerMethodArgumentResolver { - - private final JWTUtil jwtUtil; - private final HttpServletRequest request; - private final AdminService adminService; - - @Override - public boolean supportsParameter(MethodParameter parameter) { - return parameter.hasParameterAnnotation(AuthUser.class); - } - - @Override - public Object resolveArgument( - MethodParameter parameter, - ModelAndViewContainer mavContainer, - NativeWebRequest webRequest, - org.springframework.web.bind.support.WebDataBinderFactory binderFactory) throws Exception { - - String authorizationHeader = request.getHeader("Authorization"); - if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) { - throw new AuthException(PLEASE_LOGIN); - } - - String token = authorizationHeader.substring(7); - if (!jwtUtil.validateToken(token)) { - throw new AuthException(PLEASE_LOGIN); - } - - String loginId = jwtUtil.extractClaims(token).get("loginId", String.class); - return adminService.getAdminById(Long.valueOf(loginId)); - } -} \ No newline at end of file diff --git a/backend/src/main/java/org/example/backend/global/aop/AuthUser.java b/backend/src/main/java/org/example/backend/global/aop/AuthUser.java deleted file mode 100644 index c955ad12..00000000 --- a/backend/src/main/java/org/example/backend/global/aop/AuthUser.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.example.backend.global.aop; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.PARAMETER) -@Retention(RetentionPolicy.RUNTIME) -public @interface AuthUser { -} diff --git a/backend/src/main/java/org/example/backend/global/aop/AuthUserResolver.java b/backend/src/main/java/org/example/backend/global/aop/AuthUserResolver.java deleted file mode 100644 index 73434cf6..00000000 --- a/backend/src/main/java/org/example/backend/global/aop/AuthUserResolver.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.example.backend.global.aop; - -import static org.example.backend.common.exception.auth.AuthExceptionType.PLEASE_LOGIN; - -import jakarta.servlet.http.HttpServletRequest; -import lombok.RequiredArgsConstructor; -import org.example.backend.common.exception.auth.AuthException; -import org.example.backend.jwt.JWTUtil; -import org.example.backend.user.service.UserService; -import org.springframework.core.MethodParameter; -import org.springframework.stereotype.Component; -import org.springframework.web.method.support.HandlerMethodArgumentResolver; -import org.springframework.web.method.support.ModelAndViewContainer; -import org.springframework.web.context.request.NativeWebRequest; - - -@Component -@RequiredArgsConstructor -public class AuthUserResolver implements HandlerMethodArgumentResolver { - - private final JWTUtil jwtUtil; - private final HttpServletRequest request; - private final UserService userService; - - @Override - public boolean supportsParameter(MethodParameter parameter) { - return parameter.hasParameterAnnotation(AuthUser.class); - } - - @Override - public Object resolveArgument( - MethodParameter parameter, - ModelAndViewContainer mavContainer, - NativeWebRequest webRequest, - org.springframework.web.bind.support.WebDataBinderFactory binderFactory) throws Exception { - - String authorizationHeader = request.getHeader("Authorization"); - if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) { - throw new AuthException(PLEASE_LOGIN); - } - - String token = authorizationHeader.substring(7); - if (!jwtUtil.validateToken(token)) { - throw new AuthException(PLEASE_LOGIN); - } - - String loginId = jwtUtil.extractClaims(token).get("loginId", String.class); - return userService.getUserById(Long.valueOf(loginId)); - } -} diff --git a/backend/src/main/java/org/example/backend/global/config/sj/SjConfig.java b/backend/src/main/java/org/example/backend/global/config/sj/SjConfig.java deleted file mode 100644 index ebaf4f4b..00000000 --- a/backend/src/main/java/org/example/backend/global/config/sj/SjConfig.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.example.backend.global.config.sj; - -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.yj.sejongauth.config.SjSettingConfig; - -@Configuration -@Import(SjSettingConfig.class) -public class SjConfig { -} \ No newline at end of file diff --git a/backend/src/main/java/org/example/backend/global/config/web/WebConfig.java b/backend/src/main/java/org/example/backend/global/config/web/WebConfig.java index d2d4a803..ca3d8ab6 100644 --- a/backend/src/main/java/org/example/backend/global/config/web/WebConfig.java +++ b/backend/src/main/java/org/example/backend/global/config/web/WebConfig.java @@ -1,6 +1,5 @@ package org.example.backend.global.config.web; -import org.example.backend.global.aop.AuthUserResolver; import org.springframework.context.annotation.Configuration; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.CorsRegistry; @@ -10,18 +9,6 @@ @Configuration public class WebConfig implements WebMvcConfigurer { - - private final AuthUserResolver authUserResolver; - - public WebConfig(AuthUserResolver authUserResolver) { - this.authUserResolver = authUserResolver; - } - - @Override - public void addArgumentResolvers(List resolvers) { - resolvers.add(authUserResolver); - } - @Override public void addCorsMappings(CorsRegistry corsRegistry) { diff --git a/backend/src/main/java/org/example/backend/reservation/controller/ReservationController.java b/backend/src/main/java/org/example/backend/reservation/controller/ReservationController.java index dd60be43..c185cebd 100644 --- a/backend/src/main/java/org/example/backend/reservation/controller/ReservationController.java +++ b/backend/src/main/java/org/example/backend/reservation/controller/ReservationController.java @@ -4,13 +4,10 @@ import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.example.backend.reservation.domain.dto.ReservationReqDto; +import org.example.backend.reservation.domain.dto.ReservationCreateDto; +import org.example.backend.reservation.domain.dto.ReservationDeleteRequest; import org.example.backend.reservation.domain.dto.ReservationResDto; import org.example.backend.reservation.service.ReservationService; -import org.example.backend.user.domain.entity.User; -import org.example.backend.user.exception.UserException; -import org.example.backend.user.exception.UserExceptionType; -import org.example.backend.user.repository.UserRepository; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -27,15 +24,12 @@ @RequestMapping("/api/room") public class ReservationController { private final ReservationService reservationService; - private final UserRepository userRepository; @PostMapping("/{roomId}/reservation") public ResponseEntity> createReservation( @PathVariable(value = "roomId") Long roomId, - @RequestBody @Valid ReservationReqDto reqDto) { - User user = userRepository.findById(1L) - .orElseThrow(() -> new UserException(UserExceptionType.NOT_FOUND_USER)); - List resDtos = reservationService.createReservation(roomId, reqDto, user); + @RequestBody @Valid ReservationCreateDto reqDto) { + List resDtos = reservationService.createReservation(roomId, reqDto); log.debug("Reservation created successfully: {}", resDtos); return ResponseEntity.ok(resDtos); } @@ -44,7 +38,7 @@ public ResponseEntity> createReservation( @GetMapping("/{roomId}/reservation") public ResponseEntity> getReservationsByRoomAndDate( @PathVariable(name = "roomId") Long roomId, - @RequestParam(value = "date") String date + @RequestParam(value = "date", required = false) String date ) { List reservations = reservationService.getReservationsByRoomAndDate(roomId, date); return ResponseEntity.ok(reservations); @@ -75,8 +69,10 @@ public ResponseEntity getReservation( @DeleteMapping("/{roomId}/reservation/{reservationId}") public ResponseEntity deleteReservation( @PathVariable(value = "roomId") Long roomId, - @PathVariable(value = "reservationId") Long reservationId) { - reservationService.deleteReservation(reservationId); + @PathVariable(value = "reservationId") Long reservationId, + @RequestBody ReservationDeleteRequest request) { + reservationService.deleteReservation(reservationId, request.getPassword()); return ResponseEntity.ok().build(); } + } \ No newline at end of file diff --git a/backend/src/main/java/org/example/backend/reservation/domain/Reservation.java b/backend/src/main/java/org/example/backend/reservation/domain/Reservation.java index d7a4e32b..e82bcb1d 100644 --- a/backend/src/main/java/org/example/backend/reservation/domain/Reservation.java +++ b/backend/src/main/java/org/example/backend/reservation/domain/Reservation.java @@ -17,10 +17,8 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.example.backend.common.domain.BaseEntity; -import org.example.backend.common.utils.TimeParsingUtils; -import org.example.backend.reservation.domain.dto.ReservationReqDto; +import org.example.backend.reservation.domain.dto.ReservationCreateDto; import org.example.backend.room.domain.Room; -import org.example.backend.user.domain.entity.User; @Entity @Getter @@ -49,29 +47,37 @@ public class Reservation extends BaseEntity { @JoinColumn(name = "room_id") private Room room; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id") - private User user; + @Column(name = "user_name") + private String username; + + @Column(name = "password") + private String password; + + public boolean isPasswordMatch(String inputPassword) { + return this.password.equals(inputPassword); + } @Builder private Reservation(LocalDateTime startTime, LocalDateTime endTime, ReservationPurpose purpose, - String etc, Room room, User user) { + String etc, Room room, String username, String password) { this.startTime = startTime; this.endTime = endTime; this.purpose = purpose; this.etc = etc; this.room = room; - this.user = user; + this.username = username; + this.password = password; } - public static Reservation of(ReservationReqDto dto, Room room, User user) { + public static Reservation of(ReservationCreateDto dto, Room room) { return Reservation.builder() - .startTime(TimeParsingUtils.formatterLocalDateTime(String.valueOf(dto.getStartTime()))) - .endTime(TimeParsingUtils.formatterLocalDateTime(String.valueOf(dto.getEndTime()))) + .startTime(dto.getStartTime()) + .endTime(dto.getEndTime()) .purpose(ReservationPurpose.valueOf(dto.getPurpose())) .etc(dto.getEtc()) .room(room) - .user(user) + .username(dto.getUserName()) + .password(dto.getPassword()) .build(); } diff --git a/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationReqDto.java b/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationCreateDto.java similarity index 69% rename from backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationReqDto.java rename to backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationCreateDto.java index eb665aac..1fa03a7b 100644 --- a/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationReqDto.java +++ b/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationCreateDto.java @@ -3,6 +3,7 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import java.time.LocalDateTime; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -10,15 +11,15 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class ReservationReqDto { +public class ReservationCreateDto { - @JsonFormat(pattern = "yyyy-MM-dd HH:mm") @NotNull(message = "시작 시간은 필수 입력값입니다.") - private String startTime; - @JsonFormat(pattern = "yyyy-MM-dd HH:mm") + private LocalDateTime startTime; + @NotNull(message = "종료 시간은 필수 입력값입니다.") - private String endTime; + @JsonFormat(pattern = "yyyy-MM-dd HH:mm") + private LocalDateTime endTime; @NotBlank(message = "목적은 필수 입력값입니다.") @Size(max = 100, message = "목적은 최대 100자까지 입력 가능합니다.") @@ -27,18 +28,22 @@ public class ReservationReqDto { @Size(max = 200, message = "기타 내용은 최대 200자까지 입력 가능합니다.") private String etc; + private String userName; + + private String password; + @Builder - private ReservationReqDto(String startTime, String endTime, - String purpose, String etc) { + private ReservationCreateDto(LocalDateTime startTime, LocalDateTime endTime, + String purpose, String etc) { this.startTime = startTime; this.endTime = endTime; this.purpose = purpose; this.etc = etc; } - public static ReservationReqDto of(String startTime, String endTime, - String purpose, String etc) { - return ReservationReqDto.builder() + public static ReservationCreateDto of(LocalDateTime startTime, LocalDateTime endTime, + String purpose, String etc) { + return ReservationCreateDto.builder() .startTime(startTime) .endTime(endTime) .purpose(purpose) diff --git a/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationDeleteRequest.java b/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationDeleteRequest.java new file mode 100644 index 00000000..7f85b8b5 --- /dev/null +++ b/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationDeleteRequest.java @@ -0,0 +1,8 @@ +package org.example.backend.reservation.domain.dto; + +import lombok.Data; + +@Data +public class ReservationDeleteRequest { + private String password; +} diff --git a/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationResDto.java b/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationResDto.java index 0e01313e..64dd8bff 100644 --- a/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationResDto.java +++ b/backend/src/main/java/org/example/backend/reservation/domain/dto/ReservationResDto.java @@ -1,11 +1,10 @@ package org.example.backend.reservation.domain.dto; import com.fasterxml.jackson.annotation.JsonFormat; +import java.time.LocalDateTime; import lombok.AccessLevel; -import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.example.backend.common.utils.TimeParsingUtils; import org.example.backend.reservation.domain.Reservation; import org.example.backend.reservation.domain.ReservationPurpose; @@ -13,37 +12,39 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) public class ReservationResDto { private Long id; - @JsonFormat(pattern = "yyyy-MM-dd HH:mm") - private String startTime; - @JsonFormat(pattern = "yyyy-MM-dd HH:mm") - private String endTime; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm") + private LocalDateTime startTime; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm") + private LocalDateTime endTime; private ReservationPurpose purpose; private String etc; private Long roomId; - private Long userId; + private String userName; + private String password; - @Builder - private ReservationResDto(Long id, String startTime, String endTime, - ReservationPurpose purpose, String etc, - Long roomId, Long userId) { + public ReservationResDto(Long id, LocalDateTime startTime, LocalDateTime endTime, + ReservationPurpose purpose, String etc, + Long roomId, String userName, String password) { this.id = id; this.startTime = startTime; this.endTime = endTime; this.purpose = purpose; this.etc = etc; this.roomId = roomId; - this.userId = userId; + this.userName = userName; + this.password = password; } public static ReservationResDto of(Reservation reservation) { - return ReservationResDto.builder() - .id(reservation.getId()) - .startTime(TimeParsingUtils.formatterString(reservation.getStartTime())) - .endTime(TimeParsingUtils.formatterString(reservation.getEndTime())) - .purpose(reservation.getPurpose()) - .etc(reservation.getEtc()) - .roomId(reservation.getRoom().getId()) - .userId(reservation.getUser().getId()) - .build(); + return new ReservationResDto( + reservation.getId(), + reservation.getStartTime(), + reservation.getEndTime(), + reservation.getPurpose(), + reservation.getEtc(), + reservation.getRoom().getId(), + reservation.getUsername(), + reservation.getPassword() + ); } -} \ No newline at end of file +} diff --git a/backend/src/main/java/org/example/backend/reservation/exception/ReservationExceptionType.java b/backend/src/main/java/org/example/backend/reservation/exception/ReservationExceptionType.java index 5305e913..b9401f87 100644 --- a/backend/src/main/java/org/example/backend/reservation/exception/ReservationExceptionType.java +++ b/backend/src/main/java/org/example/backend/reservation/exception/ReservationExceptionType.java @@ -11,6 +11,7 @@ public enum ReservationExceptionType implements BaseExceptionType { NOT_FOUND_RESERVATION(NOT_FOUND, "예약을 찾을 수 없습니다"), + NOT_VALID_RESERVATION_PASSWORD(BAD_REQUEST, "비밀번호가 일치하지 않습니다."), EXIST_ALREADY_RESERVATION(BAD_REQUEST, "해당 시간에 이미 승인된 예약이 존재합니다."), CONFLICT_TIMETABLE(BAD_REQUEST, "해당 시간에 정기 수업이 있어 예약이 불가능합니다."), INVALID_TIME_ORDER(BAD_REQUEST, "시작 시간은 종료 시간보다 이후일 수 없습니다."), diff --git a/backend/src/main/java/org/example/backend/reservation/service/ReservationService.java b/backend/src/main/java/org/example/backend/reservation/service/ReservationService.java index 21f4aefe..d1b5d952 100644 --- a/backend/src/main/java/org/example/backend/reservation/service/ReservationService.java +++ b/backend/src/main/java/org/example/backend/reservation/service/ReservationService.java @@ -4,21 +4,16 @@ import static org.example.backend.room.exception.RoomExceptionType.NOT_FOUND_SEMINAR_ROOM; import java.time.LocalDate; -import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; import lombok.RequiredArgsConstructor; -import org.example.backend.common.utils.TimeParsingUtils; -import org.example.backend.reservation.domain.ReservationPurpose; import org.example.backend.reservation.exception.ReservationException; import org.example.backend.room.domain.Room; import org.example.backend.room.exception.RoomException; import org.example.backend.room.repository.RoomRepository; import org.example.backend.reservation.domain.Reservation; -import org.example.backend.reservation.domain.dto.ReservationReqDto; +import org.example.backend.reservation.domain.dto.ReservationCreateDto; import org.example.backend.reservation.domain.dto.ReservationResDto; import org.example.backend.reservation.repository.ReservationRepository; -import org.example.backend.user.domain.entity.User; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -32,12 +27,12 @@ public class ReservationService { private final RoomRepository roomRepository; @Transactional - public List createReservation(Long seminarRoomId, ReservationReqDto reqDto, User user) { + public List createReservation(Long seminarRoomId, ReservationCreateDto reqDto) { Room room = getSeminarRoomById(seminarRoomId); validateReservationOverlap(reqDto, seminarRoomId); - Reservation reservation = Reservation.of(reqDto, room, user); + Reservation reservation = Reservation.of(reqDto, room); reservationRepository.save(reservation); return List.of(ReservationResDto.of(reservation)); @@ -77,16 +72,21 @@ public List getReservationsByRoomAndDate(Long seminarRoomId, @Transactional - public void deleteReservation(Long id) { + public void deleteReservation(Long id, String password) { Reservation reservation = getReservationById(id); + System.out.println(reservation.getPassword()); + if (!reservation.isPasswordMatch(password)) { + throw new ReservationException(NOT_VALID_RESERVATION_PASSWORD); + } + reservationRepository.delete(reservation); } - private void validateReservationOverlap(ReservationReqDto reqDto, Long seminarRoomId) { + private void validateReservationOverlap(ReservationCreateDto reqDto, Long seminarRoomId) { boolean hasReservationConflict = reservationRepository.existsByTimePeriod( seminarRoomId, - TimeParsingUtils.formatterLocalDateTime(reqDto.getStartTime()), - TimeParsingUtils.formatterLocalDateTime(reqDto.getEndTime()) + reqDto.getStartTime(), + reqDto.getEndTime() ); if (hasReservationConflict) { diff --git a/backend/src/main/java/org/example/backend/user/controller/UserController.java b/backend/src/main/java/org/example/backend/user/controller/UserController.java deleted file mode 100644 index b1cb64e7..00000000 --- a/backend/src/main/java/org/example/backend/user/controller/UserController.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.example.backend.user.controller; - -import java.util.List; -import lombok.RequiredArgsConstructor; -import org.example.backend.admin.domain.dto.SignInReqDto; -import org.example.backend.common.dto.ResponseDto; -import org.example.backend.user.domain.dto.UserReqDto; -import org.example.backend.user.domain.dto.UserResDto; -import org.example.backend.user.service.UserService; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -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.RestController; - -@RestController -@RequiredArgsConstructor -@RequestMapping("/api/user") -public class UserController { - - private final UserService userService; - - @PostMapping("/login") - public ResponseEntity loginProcess(@RequestBody SignInReqDto joinDTO) { - String token = userService.loginProcess(joinDTO); - - return ResponseEntity.ok() - .header("Authorization", "Bearer " + token) - .body("ok"); - } - - @PostMapping - public ResponseEntity createUser(@RequestBody UserReqDto userReqDto) { - Long userId = userService.saveUser(userReqDto); - return new ResponseEntity<>(userId, HttpStatus.OK); - } - - @GetMapping - public ResponseDto> getAllUsers(Pageable pageable) { - Page userList = userService.getAllUsers(pageable); - return ResponseDto.ok(userList.getNumber(), userList.getTotalPages(), userList.getContent()); - } - - @PostMapping("/{userId}") - public ResponseEntity updateUser(@PathVariable(name = "userId") Long userId, - @RequestBody UserReqDto userReqDto) { - UserResDto userResDto = userService.updateUser(userId, userReqDto); - return new ResponseEntity<>(userResDto, HttpStatus.OK); - } - - @DeleteMapping("/{userId}") - public ResponseEntity deleteUser(@PathVariable(name = "userId") Long userId) { - userService.deleteUser(userId); - return ResponseEntity.noContent().build(); - } -} diff --git a/backend/src/main/java/org/example/backend/user/domain/dto/UserReqDto.java b/backend/src/main/java/org/example/backend/user/domain/dto/UserReqDto.java deleted file mode 100644 index 22d0b754..00000000 --- a/backend/src/main/java/org/example/backend/user/domain/dto/UserReqDto.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.example.backend.user.domain.dto; - -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class UserReqDto { - - private String studentId; - - private String name; - - private String major; - - private String phoneN; - - @Builder - private UserReqDto(String studentId, String name, String major, String phoneN) { - this.studentId = studentId; - this.name = name; - this.major = major; - this.phoneN = phoneN; - } - - public static UserReqDto of(String studentId, String name, String major, String phoneN) { - return UserReqDto.builder() - .studentId(studentId) - .name(name) - .major(major) - .phoneN(phoneN) - .build(); - } -} diff --git a/backend/src/main/java/org/example/backend/user/domain/dto/UserResDto.java b/backend/src/main/java/org/example/backend/user/domain/dto/UserResDto.java deleted file mode 100644 index 29eb5fd8..00000000 --- a/backend/src/main/java/org/example/backend/user/domain/dto/UserResDto.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.example.backend.user.domain.dto; - -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import org.example.backend.user.domain.entity.User; - -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class UserResDto { - - private Long id; - - private String studentId; - - private String name; - - private String major; - - private String phoneN; - - @Builder - private UserResDto(Long id, String studentId, String name, String major, String phoneN) { - this.id = id; - this.studentId = studentId; - this.name = name; - this.major = major; - this.phoneN = phoneN; - } - - public static UserResDto of(User user) { - return UserResDto.builder() - .id(user.getId()) - .studentId(user.getStudentId()) - .name(user.getName()) - .major(user.getMajor()) - .phoneN(user.getPhoneN()) - .build(); - } -} \ No newline at end of file diff --git a/backend/src/main/java/org/example/backend/user/domain/entity/User.java b/backend/src/main/java/org/example/backend/user/domain/entity/User.java deleted file mode 100644 index 3cdad51f..00000000 --- a/backend/src/main/java/org/example/backend/user/domain/entity/User.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.example.backend.user.domain.entity; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.Table; -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import org.example.backend.user.domain.dto.UserReqDto; - -@Entity -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Table(name = "users") -public class User { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "user_id", nullable = false) - private Long id; - - @Column(name = "student_id", unique = true) - private String studentId; - - @Column(name = "name") - private String name; - - @Column(name = "major") - private String major; - - @Column(name = "phone", unique = true) - private String phoneN; - - @Column(name = "role", nullable = false) - private String role = "USER"; - - @Builder - private User(String studentId, String name, String major, String phoneN) { - this.studentId = studentId; - this.name = name; - this.major = major; - this.phoneN = phoneN; - } - - public static User of(UserReqDto dto) { - return User.builder() - .studentId(dto.getStudentId()) - .name(dto.getName()) - .major(dto.getMajor()) - .phoneN(dto.getPhoneN()) - .build(); - } - - public void update(UserReqDto dto) { - this.name = dto.getName(); - this.major = dto.getMajor(); - this.phoneN = dto.getPhoneN(); - } -} diff --git a/backend/src/main/java/org/example/backend/user/exception/UserException.java b/backend/src/main/java/org/example/backend/user/exception/UserException.java deleted file mode 100644 index 21b76099..00000000 --- a/backend/src/main/java/org/example/backend/user/exception/UserException.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.example.backend.user.exception; - -import lombok.RequiredArgsConstructor; -import org.example.backend.common.exception.BaseException; -import org.example.backend.common.exception.BaseExceptionType; - -@RequiredArgsConstructor -public class UserException extends BaseException { - - private final UserExceptionType exceptionType; - - @Override - public BaseExceptionType exceptionType() { - return exceptionType; - } -} \ No newline at end of file diff --git a/backend/src/main/java/org/example/backend/user/exception/UserExceptionType.java b/backend/src/main/java/org/example/backend/user/exception/UserExceptionType.java deleted file mode 100644 index 1bdf1808..00000000 --- a/backend/src/main/java/org/example/backend/user/exception/UserExceptionType.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.example.backend.user.exception; - -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.HttpStatus.NOT_FOUND; - -import lombok.RequiredArgsConstructor; -import org.example.backend.common.exception.BaseExceptionType; -import org.springframework.http.HttpStatus; - -@RequiredArgsConstructor -public enum UserExceptionType implements BaseExceptionType { - - NOT_FOUND_USER(NOT_FOUND, "사용자를 찾을 수 없습니다"), - DUPLICATE_PHONE(BAD_REQUEST, "전화번호가 이미 존재합니다."), - REQUIRED_NAME(BAD_REQUEST, "이름은 필수 입력값입니다."), - REQUIRED_STUDENT_ID(BAD_REQUEST, "학번은 필수 입력값입니다.") - ; - - private final HttpStatus httpStatus; - private final String errorMessage; - - @Override - public HttpStatus httpStatus() { - return httpStatus; - } - - @Override - public String errorMessage() { - return errorMessage; - } -} diff --git a/backend/src/main/java/org/example/backend/user/repository/UserRepository.java b/backend/src/main/java/org/example/backend/user/repository/UserRepository.java deleted file mode 100644 index ff806855..00000000 --- a/backend/src/main/java/org/example/backend/user/repository/UserRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.example.backend.user.repository; - -import java.util.Optional; -import org.example.backend.user.domain.entity.User; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface UserRepository extends JpaRepository { - Optional findByStudentId(String studentId); -} diff --git a/backend/src/main/java/org/example/backend/user/service/UserService.java b/backend/src/main/java/org/example/backend/user/service/UserService.java deleted file mode 100644 index b88fab4e..00000000 --- a/backend/src/main/java/org/example/backend/user/service/UserService.java +++ /dev/null @@ -1,105 +0,0 @@ -package org.example.backend.user.service; - -import static org.example.backend.user.exception.UserExceptionType.NOT_FOUND_USER; - -import java.util.Optional; -import org.example.backend.user.domain.entity.User; -import org.springframework.data.domain.Pageable; -import lombok.RequiredArgsConstructor; -import org.example.backend.admin.domain.dto.SignInReqDto; -import org.example.backend.jwt.JWTUtil; -import org.example.backend.user.domain.dto.UserReqDto; -import org.example.backend.user.domain.dto.UserResDto; -import org.example.backend.user.exception.UserException; -import org.example.backend.user.exception.UserExceptionType; -import org.example.backend.user.repository.UserRepository; -import org.springframework.data.domain.Page; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.yj.sejongauth.controller.Sj; -import org.yj.sejongauth.domain.SjProfile; - -@Service -@RequiredArgsConstructor -@Transactional(readOnly = true) -public class UserService { - - private final UserRepository userRepository; - - private final Sj sj; - private final JWTUtil jwtUtil; - - @Transactional - public String loginProcess(SignInReqDto joinDTO) { - try { - SjProfile sjProfile = sj.login(joinDTO.getLoginId(), joinDTO.getPassword()); - UserReqDto userReqDto = UserReqDto.builder() - .name(sjProfile.getName()) - .studentId(sjProfile.getStudentCode()) - .major(sjProfile.getMajor()) - .build(); - - Long userId; - Optional optionalUser = findUser(userReqDto); - if(optionalUser.isEmpty()){ - userId = saveUser(userReqDto); - }else { - userId = Long.valueOf(optionalUser.get().getStudentId()); - } - - - return jwtUtil.createJwt(String.valueOf(userId), "USER", 60 * 60 * 10L); - } catch (RuntimeException e) { - throw new UserException(NOT_FOUND_USER); - } - } - - @Transactional - public Long saveUser(UserReqDto userReqDto) { - validateRequiredFields(userReqDto); - User user = User.of(userReqDto); - User savedUser = userRepository.save(user); - return savedUser.getId(); - } - - private Optional findUser(UserReqDto userReqDto){ - validateRequiredFields(userReqDto); - return userRepository.findByStudentId(userReqDto.getStudentId()); - } - - private void validateRequiredFields(UserReqDto userReqDto) { - if (userReqDto.getName() == null || userReqDto.getName().isEmpty()) { - throw new UserException(UserExceptionType.REQUIRED_NAME); - } - if (userReqDto.getStudentId() == null || userReqDto.getStudentId().isEmpty()) { - throw new UserException(UserExceptionType.REQUIRED_STUDENT_ID); - } - } - - public Page getAllUsers(Pageable pageable) { - return userRepository.findAll(pageable) - .map(UserResDto::of); - } - - @Transactional - public UserResDto updateUser(Long userId, UserReqDto userReqDto) { - User user = findUserById(userId); - user.update(userReqDto); - return UserResDto.of(user); - } - - @Transactional - public void deleteUser(Long userId) { - User user = findUserById(userId); - userRepository.delete(user); - } - - public User getUserById(Long userId) { - return findUserById(userId); - } - - private User findUserById(Long userId) { - return userRepository.findById(userId) - .orElseThrow(() -> new UserException(NOT_FOUND_USER)); - } -}