diff options
| author | realtradam <[email protected]> | 2024-07-16 16:19:21 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2024-07-16 16:19:21 -0400 |
| commit | 2a43a36f448980b5f15f73dbcfab850441e69fdf (patch) | |
| tree | 8df817fe5137acd0b2e493d1d62d4b20939e73f1 | |
| parent | 55a6616445b5697547792176e6a560bf19e69ba8 (diff) | |
| download | spring-blog-2a43a36f448980b5f15f73dbcfab850441e69fdf.tar.gz spring-blog-2a43a36f448980b5f15f73dbcfab850441e69fdf.zip | |
code cleanup
7 files changed, 41 insertions, 27 deletions
diff --git a/src/main/java/com/blog/web/dto/ArticleDto.java b/src/main/java/com/blog/web/dto/ArticleDto.java index 7e8b51a..372b318 100644 --- a/src/main/java/com/blog/web/dto/ArticleDto.java +++ b/src/main/java/com/blog/web/dto/ArticleDto.java @@ -1,5 +1,6 @@ package com.blog.web.dto; +import com.blog.web.models.Article; import com.blog.web.models.UserEntity; import jakarta.persistence.*; import jakarta.validation.constraints.NotEmpty; @@ -48,6 +49,16 @@ public class ArticleDto { public ArticleDto() {}; + public ArticleDto(Article article) { + this.id = article.getId(); + this.title = article.getTitle(); + this.photoUrl = article.getPhotoUrl(); + this.content = article.getContent(); + this.createdBy = article.getCreatedBy(); + this.createdOn = article.getCreatedOn(); + this.updatedOn = article.getUpdatedOn(); + } + public Long getId() { return id; } diff --git a/src/main/java/com/blog/web/mappers/ArticleMapper.java b/src/main/java/com/blog/web/mappers/ArticleMapper.java index cdeb238..680f67e 100644 --- a/src/main/java/com/blog/web/mappers/ArticleMapper.java +++ b/src/main/java/com/blog/web/mappers/ArticleMapper.java @@ -5,27 +5,10 @@ import com.blog.web.models.Article; public class ArticleMapper { public static Article mapToArticle(ArticleDto articleDto) { - Article article = new Article( - articleDto.getId(), - articleDto.getTitle(), - articleDto.getPhotoUrl(), - articleDto.getContent(), - articleDto.getCreatedBy(), - articleDto.getCreatedOn(), - articleDto.getUpdatedOn() - ); - return article; + return new Article(articleDto); } public static ArticleDto mapToArticleDto(Article article) { - return new ArticleDto( - article.getId(), - article.getTitle(), - article.getPhotoUrl(), - article.getContent(), - article.getCreatedBy(), - article.getCreatedOn(), - article.getUpdatedOn() - ); + return new ArticleDto(article); } } diff --git a/src/main/java/com/blog/web/models/Article.java b/src/main/java/com/blog/web/models/Article.java index 2682994..42d27a1 100644 --- a/src/main/java/com/blog/web/models/Article.java +++ b/src/main/java/com/blog/web/models/Article.java @@ -1,5 +1,6 @@ package com.blog.web.models; +import com.blog.web.dto.ArticleDto; import jakarta.persistence.*; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; @@ -42,6 +43,16 @@ public class Article { public Article() {}; + public Article(ArticleDto articleDto) { + this.id = articleDto.getId(); + this.title = articleDto.getTitle(); + this.photoUrl = articleDto.getPhotoUrl(); + this.content = articleDto.getContent(); + this.createdBy = articleDto.getCreatedBy(); + this.createdOn = articleDto.getCreatedOn(); + this.updatedOn = articleDto.getUpdatedOn(); + } + public Long getId() { return id; } diff --git a/src/main/java/com/blog/web/models/UserEntity.java b/src/main/java/com/blog/web/models/UserEntity.java index ac7b288..d121580 100644 --- a/src/main/java/com/blog/web/models/UserEntity.java +++ b/src/main/java/com/blog/web/models/UserEntity.java @@ -1,9 +1,12 @@ package com.blog.web.models; import jakarta.persistence.*; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.User; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; @Entity(name = "users") // Named UserEntity to prevent conflicts with Java User object @@ -27,6 +30,15 @@ public class UserEntity { return this.id == user.getId(); } + public User toSecurityUser() { + return new User( + this.getEmail(), + this.getPassword(), + this.getRoles().stream().map((role) -> new SimpleGrantedAuthority(role.getName())) + .collect(Collectors.toList()) + ); + } + public Long getId() { return id; } diff --git a/src/main/java/com/blog/web/security/CustomUserDetailsService.java b/src/main/java/com/blog/web/security/CustomUserDetailsService.java index 76c0ed8..f221c9e 100644 --- a/src/main/java/com/blog/web/security/CustomUserDetailsService.java +++ b/src/main/java/com/blog/web/security/CustomUserDetailsService.java @@ -23,13 +23,7 @@ public class CustomUserDetailsService implements UserDetailsService { public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserEntity userEntity = userRepository.findFirstByUsername(username); if(userEntity != null) { - final User authUser = new User( - userEntity.getEmail(), - userEntity.getPassword(), - userEntity.getRoles().stream().map((role) -> new SimpleGrantedAuthority(role.getName())) - .collect(Collectors.toList()) - ); - return authUser; + return userEntity.toSecurityUser(); } else { throw new UsernameNotFoundException("Invalid username or password"); diff --git a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java index 5c45485..acca7e5 100644 --- a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java +++ b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Service; import java.util.List; import java.util.stream.Collectors; +import com.blog.web.mappers.ArticleMapper; import static com.blog.web.mappers.ArticleMapper.mapToArticle; import static com.blog.web.mappers.ArticleMapper.mapToArticleDto; @@ -29,7 +30,8 @@ public class ArticleServiceImpl implements ArticleService { @Override public List<ArticleDto> findAllArticles() { List<Article> articles = articleRepository.findAll(); - return articles.stream().map((article) -> mapToArticleDto(article)).collect(Collectors.toList()); + //return articles.stream().map((article) -> mapToArticleDto(article)).collect(Collectors.toList()); + return articles.stream().map(ArticleMapper::mapToArticleDto).collect(Collectors.toList()); } @Override diff --git a/src/main/java/com/blog/web/services/impl/UserServiceImpl.java b/src/main/java/com/blog/web/services/impl/UserServiceImpl.java index 99b3bc8..f77eb9e 100644 --- a/src/main/java/com/blog/web/services/impl/UserServiceImpl.java +++ b/src/main/java/com/blog/web/services/impl/UserServiceImpl.java @@ -9,6 +9,7 @@ import com.blog.web.security.SecurityUtil; import com.blog.web.services.UserService; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; + import java.util.Arrays; @Service |
