-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathPosts.java
112 lines (91 loc) · 3.36 KB
/
Posts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Twitter Backend - Moo: Twitter Clone Application Backend by Scaler
* Copyright © 2021-2023 Subhrodip Mohanta ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package xyz.subho.clone.twitter.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.annotation.CreatedBy;
@Entity
@Table(name = "posts")
@Data
public class Posts {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(columnDefinition = "BINARY(16)")
private UUID id;
@Column(length = 240)
private String text;
@ManyToOne
@JoinColumn(
name = "users_id",
columnDefinition = "BINARY(16)",
updatable = false,
nullable = false)
@CreatedBy
private Users users;
@ElementCollection private Map<String, Date> images = new HashMap<>(4); // maximum of 4 images
@Column(name = "like_count", columnDefinition = "BIGINT(20) default '0'", nullable = false)
private Long likeCount = 0L;
@Column(name = "repost_count", columnDefinition = "BIGINT(20) default '0'", nullable = false)
private Long repostCount = 0L;
@Column(name = "orig_post_id")
private UUID originalPostId;
@Column(name = "reply_to_id")
private UUID replyToId;
@CreationTimestamp private Date timestamp;
@UpdateTimestamp private Date updatedAt;
@ElementCollection private Map<String, Date> hashtags = new HashMap<>();
@ElementCollection private Map<String, Date> mentions = new HashMap<>();
@OneToMany(mappedBy = "posts", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private List<HashtagPosts> postHashtags = new ArrayList<>();
@OneToMany(mappedBy = "posts", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private List<Likes> postLikes = new ArrayList<>();
public long incrementLikeCount() {
return ++likeCount;
}
public long decrementLikeCount() {
return (likeCount < 1) ? 0 : --likeCount;
}
public long incrementRepostCount() {
return ++repostCount;
}
public long decrementRepostCount() {
return (repostCount < 1) ? 0 : --repostCount;
}
}