diff --git a/.gitignore b/.gitignore index b63da45..835efad 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ build/ !gradle/wrapper/gradle-wrapper.jar !**/src/main/**/build/ !**/src/test/**/build/ - +application.yaml ### IntelliJ IDEA ### .idea/modules.xml .idea/jarRepositories.xml diff --git a/src/main/java/org/sopt/diary/api/DiaryListResponse.java b/src/main/java/org/sopt/diary/api/DiaryListResponse.java index d9684f4..e9a288a 100644 --- a/src/main/java/org/sopt/diary/api/DiaryListResponse.java +++ b/src/main/java/org/sopt/diary/api/DiaryListResponse.java @@ -16,4 +16,4 @@ public List getDiaryList(){ } -} +} \ No newline at end of file diff --git a/src/main/java/org/sopt/diary/api/SimpleDiaryResponse.java b/src/main/java/org/sopt/diary/api/SimpleDiaryResponse.java index 090e92e..63ee202 100644 --- a/src/main/java/org/sopt/diary/api/SimpleDiaryResponse.java +++ b/src/main/java/org/sopt/diary/api/SimpleDiaryResponse.java @@ -16,4 +16,4 @@ public Long getDiaryId() { public String getMessage() { return content; } -} +} \ No newline at end of file diff --git a/src/main/java/org/sopt/diary/repository/DiaryEntity.java b/src/main/java/org/sopt/diary/repository/DiaryEntity.java index a50b983..f0c0b16 100644 --- a/src/main/java/org/sopt/diary/repository/DiaryEntity.java +++ b/src/main/java/org/sopt/diary/repository/DiaryEntity.java @@ -1,6 +1,7 @@ package org.sopt.diary.repository; import jakarta.persistence.*; +import org.sopt.diary.user.repository.UserEntity; import java.time.LocalDateTime; @Entity @@ -18,26 +19,43 @@ public class DiaryEntity { @Column private LocalDateTime createdAt; + @Column // 카테고리 필드 추가 private String category; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = true) + private UserEntity user; // 기본 생성자 (JPA에서 사용) - public DiaryEntity() {} + public DiaryEntity() { + this.createdAt = LocalDateTime.now(); // 기본 생성 시점 설정 + } - // 필수값인 title과 content를 받는 생성자 - public DiaryEntity(String title, String content,String category) { - if (title == null || content == null ) { + // UserEntity가 포함된 생성자 + public DiaryEntity(String title, String content, String category, UserEntity user) { + if (title == null || content == null) { throw new IllegalArgumentException("Title과 Content는 반드시 필요합니다."); } this.title = title; this.content = content; this.category = category; - this.createdAt = LocalDateTime.now(); // 생성 시점의 현재 시간 + this.user = user; + this.createdAt = LocalDateTime.now(); + } + // UserEntity 없이 사용할 수 있는 생성자 추가 + public DiaryEntity(String title, String content, String category) { + if (title == null || content == null) { + throw new IllegalArgumentException("Title과 Content는 반드시 필요합니다."); + } + this.title = title; + this.content = content; + this.category = category; + this.createdAt = LocalDateTime.now(); } - // Getter + // Getter 메서드들 public Long getId() { return id; } @@ -54,8 +72,15 @@ public LocalDateTime getCreatedAt() { return createdAt; } - + public String getCategory() { + return category; + } + + public UserEntity getUser() { + return user; + } + // Setter 메서드들 public void setTitle(String title) { this.title = title; } @@ -68,11 +93,11 @@ public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; } - public String getCategory() { - return category; - } - public void setCategory(String category) { this.category = category; } + + public void setUser(UserEntity user) { + this.user = user; + } } diff --git a/src/main/java/org/sopt/diary/service/Diary.java b/src/main/java/org/sopt/diary/service/Diary.java index 5dc5f1e..1793d6a 100644 --- a/src/main/java/org/sopt/diary/service/Diary.java +++ b/src/main/java/org/sopt/diary/service/Diary.java @@ -17,4 +17,4 @@ public long getId() { public String getTitle() { // name 대신 title 사용 return title; } -} +} \ No newline at end of file diff --git a/src/main/java/org/sopt/diary/user/api/UserController.java b/src/main/java/org/sopt/diary/user/api/UserController.java new file mode 100644 index 0000000..61d320a --- /dev/null +++ b/src/main/java/org/sopt/diary/user/api/UserController.java @@ -0,0 +1,38 @@ +package org.sopt.diary.user.api; + + +import org.sopt.diary.user.repository.UserEntity; +import org.sopt.diary.user.service.UserService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +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 +@RequestMapping("/users") +public class UserController { + + + private UserService userService; + + public UserController(UserService userService){ + this.userService = userService; + } + + //회원가입 API + + @PostMapping("/signup") + public ResponseEntity registerUser(@RequestBody UserSignupRequest signupRequest){ + UserEntity registeredUser = userService.registerUser(signupRequest); + return ResponseEntity.status(HttpStatus.OK).body(registeredUser); + } + + @PostMapping("/login") + public ResponseEntity loginUser(@RequestBody UserLoginRequst loginRequst){ + UserEntity user = userService.loginUser(loginRequst); + return ResponseEntity.status(HttpStatus.OK).body(user); + } + +} diff --git a/src/main/java/org/sopt/diary/user/api/UserLoginRequst.java b/src/main/java/org/sopt/diary/user/api/UserLoginRequst.java new file mode 100644 index 0000000..43dd491 --- /dev/null +++ b/src/main/java/org/sopt/diary/user/api/UserLoginRequst.java @@ -0,0 +1,33 @@ +package org.sopt.diary.user.api; + +public class UserLoginRequst { + private String username; + private String password; + + //기본생성자 + public UserLoginRequst(){} + + + //매개변수 받는 생성자 + + public UserLoginRequst(String username,String password){ + this.username = username; + this.password = password; + } + + // Getter 메서드들 + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + // Setter 메서드들 + public void setUsername(String username) { + this.username = username; + } + + +} diff --git a/src/main/java/org/sopt/diary/user/api/UserResponse.java b/src/main/java/org/sopt/diary/user/api/UserResponse.java new file mode 100644 index 0000000..292870a --- /dev/null +++ b/src/main/java/org/sopt/diary/user/api/UserResponse.java @@ -0,0 +1,40 @@ +package org.sopt.diary.user.api; + +public class UserResponse { + private Long id; + private String username; + private String nickname; + + + public UserResponse(){} + + // 매개변수를 받는 생성자 + public UserResponse(Long id, String username, String nickname) { + this.id = id; + this.username = username; + this.nickname = nickname; + } + + // Getter 메서드들 + public Long getId() { + return id; + } + + public String getUsername() { + return username; + } + + public String getNickname() { + return nickname; + } + + + public void setUsername(String username) { + this.username = username; + } + + public void setNickname(String nickname) { + this.nickname = nickname; + } + +} diff --git a/src/main/java/org/sopt/diary/user/api/UserSignupRequest.java b/src/main/java/org/sopt/diary/user/api/UserSignupRequest.java new file mode 100644 index 0000000..db94b3a --- /dev/null +++ b/src/main/java/org/sopt/diary/user/api/UserSignupRequest.java @@ -0,0 +1,40 @@ +package org.sopt.diary.user.api; + +public class UserSignupRequest { + private String username; + private String password; + private String nickname; + + // 기본 생성자 + public UserSignupRequest() {} + + // 매개변수를 받는 생성자 + public UserSignupRequest(String username, String password, String nickname) { + this.username = username; + this.password = password; + this.nickname = nickname; + } + + // Getter 메서드들 + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + public String getNickname() { + return nickname; + } + + // Setter 메서드들 + public void setUsername(String username) { + this.username = username; + } + + + public void setNickname(String nickname) { + this.nickname = nickname; + } +} diff --git a/src/main/java/org/sopt/diary/user/repository/UserEntity.java b/src/main/java/org/sopt/diary/user/repository/UserEntity.java new file mode 100644 index 0000000..f10249e --- /dev/null +++ b/src/main/java/org/sopt/diary/user/repository/UserEntity.java @@ -0,0 +1,58 @@ +package org.sopt.diary.user.repository; + +import jakarta.persistence.*; +import org.sopt.diary.repository.DiaryEntity; +import java.util.List; + +@Entity +@Table(name="user_table") +public class UserEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "username", nullable = false, unique = true) + private String username; + + @Column(name = "password", nullable = false) + private String password; + + @Column(name = "nickname", nullable = false) + private String nickname; + + @OneToMany(mappedBy = "user") + private List diaries; + + // 기본 생성자 + public UserEntity() { + } + // 모든 필드를 받는 생성자 추가 + public UserEntity(String username, String password, String nickname) { + this.username = username; + this.password = password; + this.nickname = nickname; + } + // Getter 메서드들 + public Long getId() { + return id; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + public String getNickname() { + return nickname; + } + + public List getDiaries() { + return diaries; + } +} + + diff --git a/src/main/java/org/sopt/diary/user/repository/UserRepository.java b/src/main/java/org/sopt/diary/user/repository/UserRepository.java new file mode 100644 index 0000000..db3e120 --- /dev/null +++ b/src/main/java/org/sopt/diary/user/repository/UserRepository.java @@ -0,0 +1,11 @@ +package org.sopt.diary.user.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface UserRepository extends JpaRepository { + Optional findByUsername(String name); + + boolean existsByNickname(String nickname); +} diff --git a/src/main/java/org/sopt/diary/user/service/User.java b/src/main/java/org/sopt/diary/user/service/User.java new file mode 100644 index 0000000..95b6416 --- /dev/null +++ b/src/main/java/org/sopt/diary/user/service/User.java @@ -0,0 +1,4 @@ +package org.sopt.diary.user.service; + +public class User { +} diff --git a/src/main/java/org/sopt/diary/user/service/UserService.java b/src/main/java/org/sopt/diary/user/service/UserService.java new file mode 100644 index 0000000..6d77ada --- /dev/null +++ b/src/main/java/org/sopt/diary/user/service/UserService.java @@ -0,0 +1,54 @@ +package org.sopt.diary.user.service; + + +import org.sopt.diary.user.api.UserLoginRequst; +import org.sopt.diary.user.api.UserSignupRequest; +import org.sopt.diary.user.repository.UserEntity; +import org.sopt.diary.user.repository.UserRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class UserService { + private final UserRepository userRepository; + public UserService(UserRepository userRepository) { + this.userRepository = userRepository; + } + + + + @Transactional + public UserEntity registerUser(UserSignupRequest signupRequest){ //회원가입 + checkDuplicateNickname(signupRequest.getNickname()); + UserEntity newUser = new UserEntity( + signupRequest.getUsername(),signupRequest.getPassword(),signupRequest.getNickname() + ); + return userRepository.save(newUser); + } + + public UserEntity loginUser(UserLoginRequst userLoginRequest){ + // 사용자 조회 + return validateUserCredentials(userLoginRequest.getUsername(), userLoginRequest.getPassword()); + + } + + + + //회원 가입 시, nickname의 중복성을 체크하는 매서드 + public void checkDuplicateNickname(String nickname){ + if (userRepository.existsByNickname(nickname)) { + throw new IllegalArgumentException("닉네임이 이미 존재합니다"); + } + } + + //로그인 검증 매서드 + private UserEntity validateUserCredentials(String username,String password){ + // 사용자 조회 + UserEntity user = userRepository.findByUsername(username) + .orElseThrow(() -> new IllegalArgumentException("유저가 존재하지 않습니다.")); + if(!user.getPassword().equals(password)){ + throw new IllegalArgumentException("비밀번호가 틀렸어요"); + } + return user; + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml new file mode 100644 index 0000000..d3d10d3 --- /dev/null +++ b/src/test/resources/application.yml @@ -0,0 +1,14 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://db-2vfrih-kr.vpc-pub-cdb.ntruss.com:3306/team4 + username: sopt-35-team4 + password: 1q2w3e4r12!@ + jpa: + show-sql: true + hibernate: + ddl-auto: none + dialect: org.hibernate.dialect.MySQL8Dialect + properties: + format_sql: true + show_sql: true \ No newline at end of file