From fc34d9853386b6dbf270a10862a47a052f1fb151 Mon Sep 17 00:00:00 2001 From: realtradam Date: Sun, 28 Jul 2024 20:00:49 -0400 Subject: code cleanup --- .../blog/web/controllers/ArticleController.java | 6 +- .../src/main/java/com/blog/web/models/Article.java | 18 ++-- .../main/java/com/blog/web/models/UserEntity.java | 22 ++-- .../com/blog/web/repository/ArticleRepository.java | 5 +- .../java/com/blog/web/services/ArticleService.java | 8 +- .../blog/web/services/impl/ArticleServiceImpl.java | 44 +++++--- .../blog/web/services/impl/UserServiceImpl.java | 9 +- backend/src/main/resources/application.properties | 2 - .../main/resources/templates/articles-list.html | 14 +-- .../main/resources/templates/articles/edit.html | 15 ++- .../src/main/resources/templates/articles/new.html | 94 +++++++++-------- .../main/resources/templates/articles/show.html | 32 ++++-- .../src/main/resources/templates/auth/login.html | 4 +- .../main/resources/templates/auth/register.html | 13 ++- backend/src/main/resources/templates/index.html | 46 +++++--- backend/src/main/resources/templates/layout.html | 59 +++++++---- backend/src/main/resources/templates/post.html | 117 +++++++++++++++------ 17 files changed, 314 insertions(+), 194 deletions(-) diff --git a/backend/src/main/java/com/blog/web/controllers/ArticleController.java b/backend/src/main/java/com/blog/web/controllers/ArticleController.java index e7890b0..fef8c63 100644 --- a/backend/src/main/java/com/blog/web/controllers/ArticleController.java +++ b/backend/src/main/java/com/blog/web/controllers/ArticleController.java @@ -12,8 +12,8 @@ import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; -import java.util.List; import java.util.Optional; +import java.util.Set; @CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true", allowedHeaders = "*") @RestController @@ -41,8 +41,8 @@ public class ArticleController { } @GetMapping("/articles") - public List listArticles(@RequestParam(value = "search", required = false) Optional search) { - final List articles; + public Set listArticles(@RequestParam(value = "search", required = false) Optional search) { + final Set articles; if (search.isPresent()) { articles = articleService.searchPublicArticles(search.get()); } else { 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 roles = new HashSet<>(); + private final Set roles = new HashSet<>(); public UserEntity(String username, String email, String password, HashSet 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; } diff --git a/backend/src/main/java/com/blog/web/repository/ArticleRepository.java b/backend/src/main/java/com/blog/web/repository/ArticleRepository.java index e48f156..1ddb394 100644 --- a/backend/src/main/java/com/blog/web/repository/ArticleRepository.java +++ b/backend/src/main/java/com/blog/web/repository/ArticleRepository.java @@ -4,10 +4,9 @@ import com.blog.web.models.Article; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import java.util.HashSet; -import java.util.List; +import java.util.Set; public interface ArticleRepository extends JpaRepository { @Query("SELECT a from Article a WHERE a.title LIKE CONCAT('%', :search, '%')") - List
searchArticles(String search); + Set
searchArticles(String search); } diff --git a/backend/src/main/java/com/blog/web/services/ArticleService.java b/backend/src/main/java/com/blog/web/services/ArticleService.java index 4bf3028..5ec0175 100644 --- a/backend/src/main/java/com/blog/web/services/ArticleService.java +++ b/backend/src/main/java/com/blog/web/services/ArticleService.java @@ -4,11 +4,11 @@ import com.blog.web.dto.ArticleDto; import com.blog.web.dto.ArticlePublicDto; import com.blog.web.models.Article; -import java.util.List; import java.util.Optional; +import java.util.Set; public interface ArticleService { - List findAllArticles(); + Set findAllArticles(); Optional
saveArticle(ArticleDto article); @@ -18,9 +18,7 @@ public interface ArticleService { boolean delete(long articleId); - //List searchArticles(String search); - ArticlePublicDto findArticlePublicById(long articleId); - List searchPublicArticles(String search); + Set searchPublicArticles(String search); } diff --git a/backend/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java b/backend/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java index 04cc8be..871a2bf 100644 --- a/backend/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java +++ b/backend/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java @@ -14,6 +14,7 @@ import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import static com.blog.web.mappers.ArticleMapper.*; @@ -32,9 +33,9 @@ public class ArticleServiceImpl implements ArticleService { @Override - public List findAllArticles() { + public Set findAllArticles() { List
articles = articleRepository.findAll(); - return articles.stream().map(ArticleMapper::mapToArticlePublicDto).collect(Collectors.toList()); + return articles.stream().map(ArticleMapper::mapToArticlePublicDto).collect(Collectors.toSet()); } @Override @@ -52,28 +53,35 @@ public class ArticleServiceImpl implements ArticleService { @Override public Optional findArticleById(long articleId) { final Optional
otpArticle = articleRepository.findById(articleId); - if(otpArticle.isEmpty()) { + if (otpArticle.isEmpty()) { return Optional.empty(); - } - else { + } else { return Optional.of(mapToArticleDto(otpArticle.get())); } } @Override public void updateArticle(ArticleDto newArticle) { - if(newArticle == null) { return; } + if (newArticle == null) { + return; + } final Optional optExistingArticle = this.findArticleById(newArticle.getId()); - if(optExistingArticle.isEmpty()) { return; } // cant find article, give up + if (optExistingArticle.isEmpty()) { + return; + } // cant find article, give up final ArticleDto existingArticle = optExistingArticle.get(); Long ownerId = existingArticle.getUserId(); final Optional optUser = userService.getLoggedInUser(); - if (optUser.isEmpty()) { return; } // not logged in, not allowed to edit + if (optUser.isEmpty()) { + return; + } // not logged in, not allowed to edit final UserEntity user = optUser.get(); Long userId = user.getId(); - if (!ownerId.equals(userId)) { return; } // logged in a different user, not allowed to edit + if (!ownerId.equals(userId)) { + return; + } // logged in a different user, not allowed to edit final Article article = mapToArticle(newArticle); article.setCreatedBy(user); @@ -83,16 +91,22 @@ public class ArticleServiceImpl implements ArticleService { @Override public boolean delete(long articleId) { final Optional optArticle = this.findArticleById(articleId); - if(optArticle.isEmpty()) { return false; } // cant find article, give up + if (optArticle.isEmpty()) { + return false; + } // cant find article, give up final ArticleDto article = optArticle.get(); String ownerId = article.getUsername(); final Optional optUser = userService.getLoggedInUser(); - if (optUser.isEmpty()) { return false; } // not logged in, not allowed to delete + if (optUser.isEmpty()) { + return false; + } // not logged in, not allowed to delete final UserEntity user = optUser.get(); String userId = user.getUsername(); - if (!ownerId.equals(userId)) { return false; } // logged in a different user, not allowed to delete + if (!ownerId.equals(userId)) { + return false; + } // logged in a different user, not allowed to delete else { articleRepository.deleteById(articleId); return true; @@ -105,8 +119,8 @@ public class ArticleServiceImpl implements ArticleService { } @Override - public List searchPublicArticles(String search) { - List
articles = articleRepository.searchArticles(search); - return articles.stream().map(article -> mapToArticlePublicDto(article)).collect(Collectors.toList()); + public Set searchPublicArticles(String search) { + Set
articles = articleRepository.searchArticles(search); + return articles.stream().map(article -> mapToArticlePublicDto(article)).collect(Collectors.toSet()); } } diff --git a/backend/src/main/java/com/blog/web/services/impl/UserServiceImpl.java b/backend/src/main/java/com/blog/web/services/impl/UserServiceImpl.java index b39d15e..9ca8b0b 100644 --- a/backend/src/main/java/com/blog/web/services/impl/UserServiceImpl.java +++ b/backend/src/main/java/com/blog/web/services/impl/UserServiceImpl.java @@ -10,7 +10,8 @@ import com.blog.web.services.UserService; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; -import java.util.*; +import java.util.HashSet; +import java.util.Optional; @Service public class UserServiceImpl implements UserService { @@ -26,7 +27,9 @@ public class UserServiceImpl implements UserService { @Override public void saveUser(RegistrationDto registrationDto) { - final HashSet roles = (HashSet) Set.of(roleRepository.findByName("User").orElse(new Role())); + final HashSet roles = new HashSet() {{ + add(roleRepository.findByName("User").get()); + }}; userRepository.save(new UserEntity(registrationDto.getUsername(), registrationDto.getEmail(), passwordEncoder.encode(registrationDto.getPassword()), roles)); } @@ -43,7 +46,7 @@ public class UserServiceImpl implements UserService { public Optional getLoggedInUser() { final Optional user; Optional optUsername = Optional.ofNullable(SecurityUtil.getSessionUser()); - if(optUsername.isPresent()) { + if (optUsername.isPresent()) { user = this.findByUsername(optUsername.get()); } else { user = Optional.empty(); diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 102e2c2..3f985f6 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -2,9 +2,7 @@ spring.application.name=web spring.datasource.url=jdbc:postgresql://localhost:5432/spring spring.datasource.username=tradam spring.datasource.driver-class-name=org.postgresql.Driver - spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true - spring.security.user.name=zxcv spring.security.user.password=zxcv diff --git a/backend/src/main/resources/templates/articles-list.html b/backend/src/main/resources/templates/articles-list.html index 799c751..3e133df 100644 --- a/backend/src/main/resources/templates/articles-list.html +++ b/backend/src/main/resources/templates/articles-list.html @@ -5,12 +5,12 @@ Title -

blah blah

-
-
my aga article
-

- ... -

-
+

blah blah

+
+
my aga article
+

+ ... +

+
\ No newline at end of file diff --git a/backend/src/main/resources/templates/articles/edit.html b/backend/src/main/resources/templates/articles/edit.html index f4faee9..d48bd14 100644 --- a/backend/src/main/resources/templates/articles/edit.html +++ b/backend/src/main/resources/templates/articles/edit.html @@ -7,7 +7,8 @@
-
+
@@ -21,7 +22,8 @@ name="title" th:field="*{title}" placeholder="Yep"> -

Please fill out this field.

+

Please + fill out this field.

@@ -49,12 +52,14 @@ name="content" th:field="*{content}" placeholder="Doe"> -

Please fill out this field.

+

+ Please fill out this field.

- + diff --git a/backend/src/main/resources/templates/articles/new.html b/backend/src/main/resources/templates/articles/new.html index f5924d6..fec6d6f 100644 --- a/backend/src/main/resources/templates/articles/new.html +++ b/backend/src/main/resources/templates/articles/new.html @@ -7,55 +7,59 @@
-
-
-
- - -

Please fill out this field.

+ +
+
+ + +

Please + fill out this field.

+
+
+ + +

+ Please fill out this field.

+
-
- - -

Please fill out this field.

+
+
+ + +

+ Please fill out this field.

+
-
-
-
- - -

Please fill out this field.

+
-
-
-
- + - +
diff --git a/backend/src/main/resources/templates/articles/show.html b/backend/src/main/resources/templates/articles/show.html index 206aaae..0912e8e 100644 --- a/backend/src/main/resources/templates/articles/show.html +++ b/backend/src/main/resources/templates/articles/show.html @@ -7,7 +7,6 @@ -

@@ -15,14 +14,16 @@
-
+
-
+
@@ -36,12 +37,17 @@

Subscribe to Ghostwind CSS

-

Get the latest posts delivered right to your inbox

+

Get the latest posts + delivered right to your inbox

- - + +
@@ -54,7 +60,9 @@ Avatar of Author

Ghostwind CSS

-

Tailwind CSS version of Ghost's Casper theme by TailwindToolbox.com

+

Tailwind CSS version of Ghost's Casper theme by TailwindToolbox.com

@@ -74,11 +82,13 @@ - + - - - + + + diff --git a/backend/src/main/resources/templates/auth/login.html b/backend/src/main/resources/templates/auth/login.html index 47bc63e..1eaa5f9 100644 --- a/backend/src/main/resources/templates/auth/login.html +++ b/backend/src/main/resources/templates/auth/login.html @@ -39,7 +39,9 @@
- +
diff --git a/backend/src/main/resources/templates/auth/register.html b/backend/src/main/resources/templates/auth/register.html index dc57ca4..89153e2 100644 --- a/backend/src/main/resources/templates/auth/register.html +++ b/backend/src/main/resources/templates/auth/register.html @@ -21,7 +21,8 @@ name="username" th:field="*{username}" placeholder="Ted"> -

Please fill out this field.

+

+ Please fill out this field.

@@ -49,12 +51,15 @@ name="password" th:field="*{password}" placeholder="Doe"> -

Please fill out this field.

+

+ Please fill out this field.

- +
diff --git a/backend/src/main/resources/templates/index.html b/backend/src/main/resources/templates/index.html index 0c42c21..e3c4a8c 100644 --- a/backend/src/main/resources/templates/index.html +++ b/backend/src/main/resources/templates/index.html @@ -1,4 +1,3 @@ - POST
  • - MULTIMENU POST + MULTIMENU POST
  • - LINK + LINK
  • - POST_VUE + POST_VUE
  • @@ -49,7 +57,8 @@
    -
    +
    @@ -57,9 +66,11 @@

    Spring Blog

    -
    👋 Welcome to my Java Spring Blog!
    +
    👋 Welcome to my Java Spring Blog! +

    - This is a blog project I have created with the goal of learning and understand the ins and outs of the Java Spring framework. + This is a blog project I have created with the goal of learning and understand the ins + and outs of the Java Spring framework.

    @@ -81,14 +92,19 @@
    diff --git a/backend/src/main/resources/templates/layout.html b/backend/src/main/resources/templates/layout.html index f3e0278..f2941f6 100644 --- a/backend/src/main/resources/templates/layout.html +++ b/backend/src/main/resources/templates/layout.html @@ -12,10 +12,12 @@ - + + +::-webkit-scrollbar{height:10px;width:10px}::-webkit-scrollbar-track{background:#efefef;border-radius:6px}::-webkit-scrollbar-thumb{background:#d5d5d5;border-radius:6px}::-webkit-scrollbar-thumb:hover{background:#c4c4c4} + @@ -29,10 +31,12 @@
    -
    +
    -
    +

    @@ -104,8 +122,7 @@

    - -
    +
    @@ -122,20 +139,22 @@
  • - +
  • - +
  • - +
  • -
    diff --git a/backend/src/main/resources/templates/post.html b/backend/src/main/resources/templates/post.html index a775ff2..75d39cc 100644 --- a/backend/src/main/resources/templates/post.html +++ b/backend/src/main/resources/templates/post.html @@ -1,5 +1,5 @@ - + @@ -8,7 +8,8 @@ - + +