diff options
Diffstat (limited to 'backend/src/main/java/com/blog/web/models')
| -rw-r--r-- | backend/src/main/java/com/blog/web/models/Article.java | 18 | ||||
| -rw-r--r-- | backend/src/main/java/com/blog/web/models/UserEntity.java | 22 |
2 files changed, 17 insertions, 23 deletions
diff --git a/backend/src/main/java/com/blog/web/models/Article.java b/backend/src/main/java/com/blog/web/models/Article.java index ed4ac1c..58fcca3 100644 --- a/backend/src/main/java/com/blog/web/models/Article.java +++ b/backend/src/main/java/com/blog/web/models/Article.java @@ -6,12 +6,13 @@ import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; import java.time.LocalDateTime; +import java.util.Optional; @Entity public class Article { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private final long id; private String title; private String photoUrl; private String content; @@ -23,7 +24,7 @@ public class Article { @JoinColumn(name = "created_by", nullable = false) private UserEntity createdBy; - public Article(Long id, String title, String photoUrl, String content, UserEntity createdBy, LocalDateTime createdOn, LocalDateTime updatedOn) { + public Article(long id, String title, String photoUrl, String content, UserEntity createdBy, LocalDateTime createdOn, LocalDateTime updatedOn) { this.id = id; this.title = title; this.photoUrl = photoUrl; @@ -33,11 +34,8 @@ public class Article { this.updatedOn = updatedOn; } - public Article() { - } - public Article(ArticleDto articleDto) { - this.id = articleDto.getId(); + this.id = Optional.ofNullable(articleDto.getId()).orElse(0L); this.title = articleDto.getTitle(); this.photoUrl = articleDto.getPhotoUrl(); this.content = articleDto.getContent(); @@ -46,12 +44,12 @@ public class Article { this.updatedOn = articleDto.getUpdatedOn(); } - public Long getId() { - return id; + public Article() { + this.id = 0; } - public void setId(Long id) { - this.id = id; + public long getId() { + return id; } public String getTitle() { diff --git a/backend/src/main/java/com/blog/web/models/UserEntity.java b/backend/src/main/java/com/blog/web/models/UserEntity.java index 089b8bc..f74308b 100644 --- a/backend/src/main/java/com/blog/web/models/UserEntity.java +++ b/backend/src/main/java/com/blog/web/models/UserEntity.java @@ -1,12 +1,11 @@ package com.blog.web.models; import jakarta.persistence.*; +import jakarta.validation.constraints.NotEmpty; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; -import java.util.ArrayList; import java.util.HashSet; -import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -15,13 +14,18 @@ import java.util.stream.Collectors; public class UserEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; + private final long id = 0; + @NotEmpty + @Column(unique = true) private String username; + @NotEmpty + @Column(unique = true) private String email; + @NotEmpty private String password; @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinTable(name = "user_roles", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}) - private Set<Role> roles = new HashSet<>(); + private final Set<Role> roles = new HashSet<>(); public UserEntity(String username, String email, String password, HashSet<Role> roles) { this.username = username; @@ -39,17 +43,13 @@ public class UserEntity { } public User toSecurityUser() { - return new User(this.getEmail(), this.getPassword(), this.getRoles().stream().map((role) -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList())); + return new User(this.getEmail(), this.password, this.getRoles().stream().map((role) -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList())); } public long getId() { return id; } - public void setId(long id) { - this.id = id; - } - public String getUsername() { return username; } @@ -66,10 +66,6 @@ public class UserEntity { this.email = email; } - public String getPassword() { - return password; - } - public void setPassword(String password) { this.password = password; } |
