Skip to content

Commit c104735

Browse files
committed
[Refactoring] entity 수정
1 parent 23f8c11 commit c104735

File tree

11 files changed

+276
-137
lines changed

11 files changed

+276
-137
lines changed

.idea/workspace.xml

+115-109
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/sprint/mission/entity/BinaryContent.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package com.sprint.mission.entity;
22

3+
import static lombok.AccessLevel.*;
4+
35
import io.swagger.v3.oas.annotations.media.Schema;
46
import jakarta.persistence.*;
57
import lombok.*;
68

79
@Entity
810
@EqualsAndHashCode(of = {"fileName", "contentType", "size"}, callSuper = true)
9-
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
11+
@NoArgsConstructor(access = PROTECTED)
1012
@ToString(of = {"fileName", "size", "contentType"})
1113
@Getter
1214
@Schema(description = "바이너리 컨텐츠")
1315
@Table(name = "binary_contents")
1416
public class BinaryContent extends BaseEntity {
1517

18+
@Column(nullable = false)
1619
private String fileName;
20+
@Column(nullable = false)
1721
private Long size;
22+
@Column(length = 100, nullable = false)
1823
private String contentType;
1924

20-
@OneToOne(mappedBy = "profile")
21-
private User user;
22-
2325
public BinaryContent(String fileName, Long size, String contentType) {
2426
this.fileName = fileName;
2527
this.size = size;

src/main/java/com/sprint/mission/entity/Channel.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.sprint.mission.entity;
22

33
import io.swagger.v3.oas.annotations.media.Schema;
4+
import jakarta.persistence.Column;
45
import jakarta.persistence.Entity;
6+
import jakarta.persistence.EnumType;
7+
import jakarta.persistence.Enumerated;
58
import jakarta.persistence.OneToMany;
69
import jakarta.persistence.Table;
710
import lombok.*;
@@ -10,6 +13,7 @@
1013
import java.util.List;
1114

1215
import static jakarta.persistence.CascadeType.*;
16+
import static jakarta.persistence.EnumType.*;
1317

1418
@Entity
1519
@EqualsAndHashCode(of = {"channelType", "name"}, callSuper = true)
@@ -20,6 +24,8 @@
2024
@Table(name = "channels")
2125
public class Channel extends BaseUpdatableEntity {
2226

27+
@Enumerated(STRING)
28+
@Column(nullable = false)
2329
private ChannelType channelType;
2430
private String name;
2531
private String description;

src/main/java/com/sprint/mission/entity/Message.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@
2121
@Table(name = "messages")
2222
public class Message extends BaseUpdatableEntity {
2323

24+
@Column(columnDefinition = "text", nullable = false)
2425
private String content;
2526

26-
@ManyToOne(fetch = LAZY, cascade = REMOVE)
27-
@JoinColumn(name = "channel_id", nullable = false)
27+
@ManyToOne(fetch = LAZY, optional = false)
28+
@JoinColumn(name = "channel_id", columnDefinition = "uuid")
2829
private Channel channel;
2930

30-
@ManyToOne(fetch = LAZY)
31+
@ManyToOne(fetch = LAZY, optional = false)
32+
@JoinColumn(name = "author_id", columnDefinition = "uuid")
3133
@OnDelete(action = OnDeleteAction.SET_NULL)
3234
private User author;
3335

34-
// 설계도에서 OneToMany 관계를 JoinTable로 설계하도록 되어있어서...
35-
// 이런 구조에서 Message삭제 시 binaryContent자동 삭제는 구현 못했습니다(수동으로 메서드 만들어서 해야될까요)
36-
// MessageCascadeTest 파일에서 테스트 실패
3736
@OneToMany(fetch = LAZY, cascade = REMOVE, orphanRemoval = true)
3837
@JoinTable(
3938
name = "message_attachments",
@@ -49,13 +48,11 @@ public Message update(String newContent) {
4948
return this;
5049
}
5150

52-
public Message(String content, Channel channel, User author) {
51+
public Message(String content, Channel channel, User author, List<BinaryContent> messageAttachments) {
5352
this.content = content;
5453
this.channel = channel;
5554
this.author = author;
55+
this.messageAttachments = messageAttachments;
5656
}
5757

58-
public void addAttachment(BinaryContent attachment) {
59-
messageAttachments.add(attachment);
60-
}
6158
}

src/main/java/com/sprint/mission/entity/ReadStatus.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
@Table(name = "read_statuses")
1919
public class ReadStatus extends BaseUpdatableEntity {
2020

21-
@ManyToOne(fetch = LAZY)
22-
@JoinColumn(name = "user_id", unique = true)
21+
@ManyToOne(fetch = LAZY, optional = false)
22+
@JoinColumn(name = "user_id", columnDefinition = "uuid", unique = true)
2323
private User user;
2424

25-
@ManyToOne(fetch = LAZY)
26-
@JoinColumn(name = "channel_id", unique = true)
25+
@ManyToOne(fetch = LAZY, optional = false)
26+
@JoinColumn(name = "channel_id", columnDefinition = "uuid", unique = true)
2727
private Channel channel;
2828

29-
@NotNull
29+
@Column(nullable = false, columnDefinition = "timestamp with time zone")
3030
private Instant lastReadAt;
3131

3232
public void update(Instant newLastReadAt) {

src/main/java/com/sprint/mission/entity/User.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@
1919
@Getter //@Builder
2020
@NoArgsConstructor(access = PROTECTED)
2121
@Schema(description = "유저")
22-
@Table(name = "users")
22+
@Table(
23+
name = "users",
24+
uniqueConstraints = {
25+
@UniqueConstraint(columnNames = {"username", "email"})
26+
}
27+
)
2328
public class User extends BaseUpdatableEntity {
2429

30+
@Column(length = 50, nullable = false, unique = true)
2531
private String username;
32+
@Column(length = 100, nullable = false, unique = true)
2633
private String email;
34+
@Column(length = 60, nullable = false)
2735
private String password;
2836

29-
@OneToOne(fetch = LAZY)
30-
@JoinColumn(name = "profile_id")
31-
@OnDelete(action = OnDeleteAction.SET_NULL) // profile이 삭제되면 user의 profile은 null로 변경
37+
@OneToOne(fetch = LAZY, cascade = ALL, orphanRemoval = true)
38+
@JoinColumn(name = "profile_id", columnDefinition = "uuid")
3239
private BinaryContent profile;
3340

3441
@OneToOne(mappedBy = "user", cascade = REMOVE)

src/main/java/com/sprint/mission/entity/UserStatus.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
@Table(name = "user_statuses")
2121
public class UserStatus extends BaseUpdatableEntity {
2222

23-
@OneToOne(fetch = LAZY)
23+
@OneToOne(fetch = LAZY, optional = false)
2424
@JoinColumn(name = "user_id", nullable = false, unique = true)
2525
private User user;
2626

27+
@Column(columnDefinition = "timestamp with the zone", nullable = false)
2728
private Instant lastActiveAt;
2829

2930
public UserStatus(User user) {
@@ -38,8 +39,8 @@ public void update() {
3839
public boolean isOnline() {
3940
if (lastActiveAt == null) {
4041
return false;
41-
} else {
42-
return Duration.between(lastActiveAt, Instant.now()).toMinutes() < 5;
4342
}
43+
44+
return Duration.between(lastActiveAt, Instant.now()).toMinutes() < 5;
4445
}
4546
}

src/main/resources/application-dev.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ spring:
1616
---
1717
logging:
1818
level:
19-
org.hibernate.sql: warn
19+
com.sprint.mission: debug
20+
org.hibernate.sql: debug
2021
org.hibernate.orm.jdbc.bind: trace
2122

src/main/resources/application-prod.yml

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ spring:
1212
---
1313
logging:
1414
level:
15+
com.sprint.mission: info
1516
org.hibernate.sql: info

src/main/resources/application.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ springdoc:
5858
enabled: false
5959
swagger-ui:
6060
enabled: false
61-
61+
logging:
62+
level:
63+
root: info

src/main/resources/new-schema.sql

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
CREATE TABLE users
2+
(
3+
id uuid PRIMARY KEY,
4+
created_at timestamptz NOT NULL,
5+
updated_at timestamptz,
6+
username varchar(50) NOT NULL,
7+
email varchar(100) NOT NULL,
8+
password varchar(60) NOT NULL,
9+
profile_id uuid,
10+
UNIQUE (username, email)
11+
);
12+
13+
CREATE TABLE binary_contents
14+
(
15+
id uuid PRIMARY KEY,
16+
created_at timestamptz NOT NULL,
17+
file_name varchar(255) NOT NULL,
18+
size bigint NOT NULL,
19+
content_type varchar(100) NOT NULL,
20+
bytes bytea NOT NULL
21+
);
22+
23+
24+
CREATE TABLE channels
25+
(
26+
id uuid PRIMARY KEY,
27+
created_at timestamptz NOT NULL,
28+
updated_at timestamptz,
29+
name varchar(100),
30+
description varchar(500),
31+
type varchar(10) NOT NULL
32+
-- CONSTRAINT channels_pkey PRIMARY KEY (id),
33+
-- CONSTRAINT channels_type_check
34+
-- CHECK (type::text = ANY
35+
-- (ARRAY ['PUBLIC'::character varying, 'PRIVATE'::character varying]::text[]))
36+
);
37+
38+
CREATE TABLE messages
39+
(
40+
id uuid PRIMARY KEY,
41+
created_at timestamptz NOT NULL,
42+
updated_at timestamptz NOT NULL,
43+
content text,
44+
channel_id uuid NOT NULL,
45+
author_id uuid
46+
-- CONSTRAINT messages_pkey PRIMARY KEY (id),
47+
-- CONSTRAINT fk_author FOREIGN KEY (author_id)
48+
-- REFERENCES public.users (id) MATCH SIMPLE
49+
-- ON UPDATE NO ACTION
50+
-- ON DELETE SET NULL,
51+
-- CONSTRAINT fk_channel FOREIGN KEY (channel_id)
52+
-- REFERENCES public.channels (id) MATCH SIMPLE
53+
-- ON UPDATE NO ACTION
54+
-- ON DELETE CASCADE
55+
);
56+
57+
CREATE TABLE read_statuses
58+
(
59+
id uuid PRIMARY KEY,
60+
created_at timestamptz NOT NULL,
61+
updated_at timestamptz,
62+
user_id uuid NOT NULL,
63+
last_read_at timestamptz NOT NULL,
64+
channel_id uuid NOT NULL,
65+
UNIQUE (user_id, channel_id)
66+
-- CONSTRAINT read_statuses_pkey PRIMARY KEY (id),
67+
-- CONSTRAINT read_statuses_channel_id_key UNIQUE (channel_id),
68+
-- CONSTRAINT unique_user_id UNIQUE (user_id),
69+
-- CONSTRAINT fk_channel_id FOREIGN KEY (channel_id)
70+
-- REFERENCES public.channels (id) MATCH SIMPLE
71+
-- ON UPDATE NO ACTION
72+
-- ON DELETE CASCADE,
73+
-- CONSTRAINT fk_user_id FOREIGN KEY (user_id)
74+
-- REFERENCES public.users (id) MATCH SIMPLE
75+
-- ON UPDATE NO ACTION
76+
-- ON DELETE CASCADE
77+
);
78+
79+
80+
CREATE TABLE user_statuses
81+
(
82+
id uuid PRIMARY KEY,
83+
created_at timestamptz NOT NULL,
84+
updated_at timestamptz,
85+
user_id uuid UNIQUE NOT NULL,
86+
last_active_at timestamptz NOT NULL
87+
-- CONSTRAINT user_statuses_pkey PRIMARY KEY (id),
88+
-- CONSTRAINT user_statuses_user_id_key UNIQUE (user_id),
89+
-- CONSTRAINT user_statuses_user_id_fkey FOREIGN KEY (user_id)
90+
-- REFERENCES public.users (id) MATCH SIMPLE
91+
-- ON UPDATE NO ACTION
92+
-- ON DELETE CASCADE
93+
);
94+
95+
96+
97+
CREATE TABLE message_attachments
98+
(
99+
message_id uuid,
100+
attachment_id uuid,
101+
PRIMARY KEY (message_id, attachment_id)
102+
-- CONSTRAINT fk_attachment FOREIGN KEY (attachment_id)
103+
-- REFERENCES public.binary_contents (id) MATCH SIMPLE
104+
-- ON UPDATE NO ACTION
105+
-- ON DELETE CASCADE,
106+
-- CONSTRAINT fk_message FOREIGN KEY (message_id)
107+
-- REFERENCES public.messages (id) MATCH SIMPLE
108+
-- ON UPDATE NO ACTION
109+
-- ON DELETE CASCADE
110+
);
111+
112+
ALTER TABLE users
113+
ADD CONSTRAINT fk_user_binary_content
114+
FOREIGN KEY (profile_id)
115+
REFERENCES binary_content (id)
116+
ON DELETE SET NULL

0 commit comments

Comments
 (0)