Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semina3 #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/sopt/diary/api/DiaryListResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ public List<DiaryResponse> getDiaryList(){
}


}
}
2 changes: 1 addition & 1 deletion src/main/java/org/sopt/diary/api/SimpleDiaryResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ public Long getDiaryId() {
public String getMessage() {
return content;
}
}
}
47 changes: 36 additions & 11 deletions src/main/java/org/sopt/diary/repository/DiaryEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sopt.diary.repository;

import jakarta.persistence.*;
import org.sopt.diary.user.repository.UserEntity;
import java.time.LocalDateTime;

@Entity
Expand All @@ -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는 반드시 필요합니다.");
Comment on lines +37 to 38

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 위에서 @Column(nullable = false) 처리를 했는데, 다시 한번 제목과 내용의 null 여부를 검사하는 이유가 뭔가용?
중복되는 거라면 빼도 되는 거 아닐까 싶은데 혹시 따로 처리를 하신 이유가 있는지 궁금합니다~!

엔티티는 데이터 저장 객체로서 데이터베이스와의 매핑만 책임져야 한다고 생각해서
만약 검증과 유효성 검사를 따로 진행하고 싶다면 엔티티 자체가 아닌 DTO나 서비스에서 하는게 맞지 않나 싶기도 해용
어떻게 생각하시나요?!

}
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는 반드시 필요합니다.");
}
Comment on lines +48 to +51

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 위의 리뷰와 같이 null 처리를 하신 이유가 궁금합니다!

this.title = title;
this.content = content;
this.category = category;
this.createdAt = LocalDateTime.now();
}

// Getter
// Getter 메서드들
public Long getId() {
return id;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/sopt/diary/service/Diary.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public long getId() {
public String getTitle() { // name 대신 title 사용
return title;
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/org/sopt/diary/user/api/UserController.java
Original file line number Diff line number Diff line change
@@ -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<UserEntity> registerUser(@RequestBody UserSignupRequest signupRequest){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserEntity 를 직접 반환하는 방법 말고 다른 방법은 없을까요?

제가 알기로는 Entity의 경우 민감한 정보가 포함되어 있으면 그대로 노출될 수 있기 때문에 직접 반환하는 건 위험하다고 알고 있어서요!
또한 엔티티는 데이터베이스와 매핑되는 객체로서, 비즈니스 로직이나 데이터 저장, 조회만을 담당해야 한다고 생각해서 별도로 DTO를 생성해서 하는게 더 좋지 않을까 싶습니당

틀렸으면 말해주삼 킥킥 ><

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

밑에 보니까 UserResponse가 잇는데 이거 사용해도 되지 않을까용

UserEntity registeredUser = userService.registerUser(signupRequest);
return ResponseEntity.status(HttpStatus.OK).body(registeredUser);
}

@PostMapping("/login")
public ResponseEntity<UserEntity> loginUser(@RequestBody UserLoginRequst loginRequst){
UserEntity user = userService.loginUser(loginRequst);
return ResponseEntity.status(HttpStatus.OK).body(user);
}

}
33 changes: 33 additions & 0 deletions src/main/java/org/sopt/diary/user/api/UserLoginRequst.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
40 changes: 40 additions & 0 deletions src/main/java/org/sopt/diary/user/api/UserResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.sopt.diary.user.api;

public class UserResponse {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 record로 하는게 더 편하지 않나용

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;
}

}
40 changes: 40 additions & 0 deletions src/main/java/org/sopt/diary/user/api/UserSignupRequest.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
58 changes: 58 additions & 0 deletions src/main/java/org/sopt/diary/user/repository/UserEntity.java
Original file line number Diff line number Diff line change
@@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

히히 username 아니고 nickname이 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<DiaryEntity> 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<DiaryEntity> getDiaries() {
return diaries;
}
}


11 changes: 11 additions & 0 deletions src/main/java/org/sopt/diary/user/repository/UserRepository.java
Original file line number Diff line number Diff line change
@@ -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 <UserEntity,Long> {
Optional<UserEntity> findByUsername(String name);

boolean existsByNickname(String nickname);
}
4 changes: 4 additions & 0 deletions src/main/java/org/sopt/diary/user/service/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.sopt.diary.user.service;

public class User {
}
Loading