11package DC_square .spring .domain .entity .notification ;
22
3- import DC_square .spring .domain .entity .Dday ;
43import DC_square .spring .domain .entity .User ;
54import DC_square .spring .domain .enums .NotificationType ;
65import jakarta .persistence .*;
76import lombok .*;
8- import org .hibernate .annotations .OnDelete ;
9- import org .hibernate .annotations .OnDeleteAction ;
107
11- import java .time .LocalDate ;
128import java .time .LocalDateTime ;
139
14- @ Builder
10+ @ Entity
1511@ Getter
1612@ Setter
17- @ Entity
1813@ NoArgsConstructor
1914@ AllArgsConstructor
20- @ Table ( name = "notification" )
15+ @ Builder
2116public class Notification {
17+
2218 @ Id
2319 @ GeneratedValue (strategy = GenerationType .IDENTITY )
2420 private Long id ;
2521
26- @ Column (nullable = true )
27- private String boardName ;
28-
29- @ Column (nullable = true )
30- private String postTitle ;
31-
32- @ Column
33- private String commenterName ;
34-
35- @ Column
36- private String commentContent ;
37-
38- @ Column (nullable = false )
39- private String ddayName ;
40-
41- @ Column (nullable = false )
42- private Integer daysRemaining ;
43-
44- @ Embedded
45- private NotificationContent content ;
46-
47- @ Embedded
48- private RelatedUrl url ;
49-
50- @ Builder .Default
51- @ Column (name = "is_read" , nullable = false )
52- private Boolean read = false ;
22+ @ ManyToOne
23+ @ JoinColumn (name = "user_id" , nullable = false )
24+ private User user ;
5325
5426 @ Enumerated (EnumType .STRING )
55- @ Column (nullable = false )
5627 private NotificationType notificationType ;
5728
58- @ OnDelete (action = OnDeleteAction .CASCADE )
59- @ ManyToOne (fetch = FetchType .LAZY )
60- @ JoinColumn (name = "user_id" )
61- private User user ;
62-
63- @ Column (name = "created_at" )
64- private LocalDateTime createdAt ;
29+ private String title ;
6530
66- private LocalDateTime updatedAt ;
31+ private String content ;
6732
68- @ Builder
69- public Notification (User user , NotificationType notificationType , String content , String url ,
70- String boardName , String postTitle , String commenterName , String commentContent ,
71- String ddayName , Integer daysRemaining ) {
72- this .user = user ;
73- this .notificationType = notificationType ;
74- this .content = new NotificationContent (content );
75- this .url = new RelatedUrl (url );
76- this .read = false ;
77- this .boardName = boardName ;
78- this .postTitle = postTitle ;
79- this .commenterName = commenterName ;
80- this .commentContent = commentContent ;
81- this .ddayName = ddayName ;
82- this .daysRemaining = daysRemaining ;
83- this .createdAt = LocalDateTime .now ();
84- this .updatedAt = LocalDateTime .now ();
85- }
86-
87- // 댓글 관련 알림
88- public static Notification createCommentNotification (User user , String content , String url ,
89- String boardName , String postTitle ,
90- String commenterName , String commentContent ) {
91- return Notification .builder ()
92- .user (user )
93- .notificationType (NotificationType .COMMENT )
94- .content (new NotificationContent (content ))
95- .url (new RelatedUrl (url ))
96- .boardName (boardName )
97- .postTitle (postTitle )
98- .commenterName (commenterName )
99- .commentContent (commentContent )
100- .build ();
101- }
102-
103- // 디데이 관련 알림
104- public static Notification createDdayNotification (User user , String content , String url ,
105- String ddayName , Integer daysRemaining ) {
106- return Notification .builder ()
107- .user (user )
108- .notificationType (NotificationType .DDAY )
109- .content (new NotificationContent (content ))
110- .url (new RelatedUrl (url ))
111- .ddayName (ddayName )
112- .daysRemaining (daysRemaining )
113- .build ();
114- }
115-
116- public static int calculateDaysRemaining (Dday dday ) {
117- if (dday == null || dday .getDay () == null ) {
118- return 0 ;
119- }
120- return (int ) LocalDate .now ().until (dday .getDay ()).getDays ();
121- }
122-
123- public String getContent () {
124- return content .getContent ();
125- }
126-
127- public String getUrl () {
128- return url .getUrl ();
129- }
130-
131- public void read (){
132- read = true ;
133- }
134-
135- public void setCreatedAt (LocalDateTime createdAt ) {
136- this .createdAt = createdAt ;
137- }
33+ private LocalDateTime createdAt ;
13834
13935 @ PrePersist
14036 public void prePersist () {
141- this .createdAt = LocalDateTime .now ();
142- this .updatedAt = LocalDateTime .now ();
143- }
144-
145- @ PreUpdate
146- public void preUpdate () {
147- this .updatedAt = LocalDateTime .now ();
37+ createdAt = LocalDateTime .now ();
14838 }
149- }
39+ }
0 commit comments