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

[토닉] 1단계: 엔티티 매핑 #3

Merged
merged 3 commits into from Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 53 additions & 42 deletions src/main/java/qna/domain/Answer.java
@@ -1,19 +1,51 @@
package qna.domain;

import java.time.LocalDateTime;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import qna.NotFoundException;
import qna.UnAuthorizedException;

import java.util.Objects;

@Entity
public class Answer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long writerId;

@Column(nullable = false)
private LocalDateTime createdAt;

Choose a reason for hiding this comment

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

여러 엔티티에 동일하게 추가되는 필드가 있는데, MappedSuperclass 또는 Embeddable 을 학습해보시면 중복 코드 제거가 가능하겠네요.
둘 중 무엇을 사용하면 좋을지, 각각 어떤 장단점이 있는지 고민해서 적용해보시면 좋겠습니다.


@Column(nullable = false)
private boolean deleted = false;

private Long questionId;
private LocalDateTime updatedAt;

Choose a reason for hiding this comment

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

생성, 수정 시각을 자동으로 관리하기 위해 Auditing 기능을 학습해서 적용해보시면 좋겠습니다.

private Long writerId;

@Lob
private String contents;
private boolean deleted = false;

public Answer(User writer, Question question, String contents) {
this(null, writer, question, contents);

public Answer(Long id, LocalDateTime createdAt, boolean deleted, Long questionId, LocalDateTime updatedAt,
Long writerId, String contents) {
this.id = id;
this.createdAt = createdAt;
this.deleted = deleted;
this.questionId = questionId;
this.updatedAt = updatedAt;
this.writerId = writerId;
this.contents = contents;
}

public Answer(LocalDateTime createdAt, boolean deleted, Long questionId, LocalDateTime updatedAt,
Long writerId, String contents) {
this(null, createdAt, deleted, questionId, updatedAt, writerId, contents);
}

public Answer(Long id, User writer, Question question, String contents) {
Expand All @@ -32,6 +64,9 @@ public Answer(Long id, User writer, Question question, String contents) {
this.contents = contents;
}

public Answer() {
}

public boolean isOwner(User writer) {
return this.writerId.equals(writer.getId());
}
Expand All @@ -40,46 +75,10 @@ public void toQuestion(Question question) {
this.questionId = question.getId();
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getWriterId() {
return writerId;
}

public void setWriterId(Long writerId) {
this.writerId = writerId;
}

public Long getQuestionId() {
return questionId;
}

public void setQuestionId(Long questionId) {
this.questionId = questionId;
}

public String getContents() {
return contents;
}

public void setContents(String contents) {
this.contents = contents;
}

public boolean isDeleted() {
return deleted;
}

public void setDeleted(boolean deleted) {
this.deleted = deleted;
}

@Override
public String toString() {
return "Answer{" +
Expand All @@ -90,4 +89,16 @@ public String toString() {
", deleted=" + deleted +
'}';
}

public void setDeleted(boolean deleted) {
this.deleted = deleted;
}

public Long getId() {
return id;
}

public Long getWriterId() {
return writerId;
}
}
13 changes: 13 additions & 0 deletions src/main/java/qna/domain/DeleteHistory.java
Expand Up @@ -2,9 +2,20 @@

import java.time.LocalDateTime;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class DeleteHistory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Enumerated(EnumType.STRING)
private ContentType contentType;
private Long contentId;
private Long deletedById;
Expand All @@ -17,6 +28,8 @@ public DeleteHistory(ContentType contentType, Long contentId, Long deletedById,
this.createDate = createDate;
}

public DeleteHistory() {}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/qna/domain/DeleteHistoryRepository.java
@@ -1,6 +1,8 @@
package qna.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DeleteHistoryRepository extends JpaRepository<DeleteHistory, Long> {
}
50 changes: 24 additions & 26 deletions src/main/java/qna/domain/Question.java
@@ -1,11 +1,29 @@
package qna.domain;

import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;

@Entity
public class Question {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@Lob
private String contents;
private Long writerId;
@Column(nullable = false)
private LocalDateTime createdAt;
@Column(nullable = false)
private boolean deleted = false;
@Column(nullable = false, length = 100)
private String title;
private LocalDateTime updatedAt;
private Long writerId;

public Question(String title, String contents) {
this(null, title, contents);
Expand All @@ -17,6 +35,10 @@ public Question(Long id, String title, String contents) {
this.contents = contents;
}

public Question() {

}

public Question writeBy(User writer) {
this.writerId = writer.getId();
return this;
Expand All @@ -34,34 +56,10 @@ public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContents() {
return contents;
}

public void setContents(String contents) {
this.contents = contents;
}

public Long getWriterId() {
return writerId;
}

public void setWriterId(Long writerId) {
this.writerId = writerId;
}

public boolean isDeleted() {
return deleted;
}
Expand Down
64 changes: 20 additions & 44 deletions src/main/java/qna/domain/User.java
@@ -1,23 +1,35 @@
package qna.domain;

import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import qna.UnAuthorizedException;

import java.util.Objects;

@Entity
public class User {
public static final GuestUser GUEST_USER = new GuestUser();

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

Choose a reason for hiding this comment

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

아래 제약 조건 추가를 위해서는 어떤 설정을 해줄 수 있을까요?

alter table user
    add constraint UK_a3imlf41l37utmxiquukk8ajc unique (user_id)

private String userId;
private String password;
private String name;
@Column(nullable = false)
private LocalDateTime createdAt;
@Column(length = 50)
private String email;
@Column(nullable = false, length = 20)
private String name;
@Column(nullable = false, length = 20)
private String password;
private LocalDateTime updatedAt;
@Column(nullable = false, length = 20)
private String userId;

private User() {
}

public User(String userId, String password, String name, String email) {
this(null, userId, password, name, email);
public User() {
}

public User(Long id, String userId, String password, String name, String email) {
Expand Down Expand Up @@ -66,42 +78,6 @@ public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "User{" +
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/qna/service/DeleteHistoryService.java
@@ -1,13 +1,12 @@
package qna.service;

import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import qna.domain.DeleteHistory;
import qna.domain.DeleteHistoryRepository;

import java.util.List;

@Service
public class DeleteHistoryService {
private DeleteHistoryRepository deleteHistoryRepository;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/subway/Application.java
@@ -0,0 +1,12 @@
package subway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}