Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a0fc386
Refactor User feature to DDD-style package structure
YukiJanse Feb 6, 2026
1dc341c
Refactor class name User to UserJpaEntity.
YukiJanse Feb 6, 2026
4fd77eb
fixed integrationtests
YukiJanse Feb 6, 2026
aa3568c
Refactored UserRepository and added UserQueryService
YukiJanse Feb 6, 2026
793567f
Add README
YukiJanse Feb 7, 2026
867ebf6
Removed data manipulation methods from UserQueryService
YukiJanse Feb 9, 2026
20210e7
Refactored UserQueryService as interface
YukiJanse Feb 9, 2026
e4e91a4
Extract logic into use cases
YukiJanse Feb 9, 2026
3bfef36
Removed unneeded DTOs
YukiJanse Feb 10, 2026
401f62c
Refactored User related properties in other features
YukiJanse Feb 10, 2026
b280514
Refactor UserService into use cases and update controllers
YukiJanse Feb 10, 2026
f2ca425
Remove an unneeded class and test
YukiJanse Feb 10, 2026
e81c269
Refactor usecases and removed unneeded usecases
YukiJanse Feb 10, 2026
3ab3af1
Refactor user-related classes to use new domain model and repository …
YukiJanse Feb 11, 2026
72cd924
Refactor User mapping and reconstruction logic for improved clarity a…
YukiJanse Feb 11, 2026
4dd4688
Introduce Email value object and refactor User class to use Email type
YukiJanse Feb 12, 2026
c1ca5a8
Add unit tests for user use cases: Delete, Update Email, Update Passw…
YukiJanse Feb 13, 2026
e458974
Introduce Username value object and refactor User class to use Userna…
YukiJanse Feb 13, 2026
de908c1
Introduce HashedPassword value object and refactor User class to use …
YukiJanse Feb 14, 2026
ec4ca4d
Introduce UserRole value object and refactor User class to use UserRo…
YukiJanse Feb 14, 2026
99eb3e4
Introduce DisplayName value object and refactor User class to use Dis…
YukiJanse Feb 14, 2026
644ea1b
Refactor value object constructors to private and introduce factory m…
YukiJanse Feb 14, 2026
9189d36
Introduce Bio value object and refactor User class to use Bio type
YukiJanse Feb 14, 2026
70a1837
Introduce AvatarUrl value object and refactor User class to use Avata…
YukiJanse Feb 14, 2026
5431925
Introduce UserId value object and refactor User class to use UserId type
YukiJanse Feb 15, 2026
41a76cc
Refactor User's getters, password handling in UpdateEmail and UpdateP…
YukiJanse Feb 16, 2026
a89ef97
Add null checks and validation for User update methods
YukiJanse Feb 16, 2026
d85c7b0
Refactor value object constructors in Bio, DisplayName, and UserId cl…
YukiJanse Feb 16, 2026
4f810cb
Add documentation to classes and methods
YukiJanse Feb 16, 2026
0c3984a
Refactor Username class to introduce length constants for validation
YukiJanse Feb 16, 2026
947c447
Add documentation for user-related controllers and DTOs
YukiJanse Feb 16, 2026
598c92a
Refactor SecurityUser constructor to accept individual parameters for…
YukiJanse Feb 17, 2026
9a8e8d8
Refactor the name of UserLoadService methods
YukiJanse Feb 17, 2026
6a4c6de
Refactor primitive variables in Usecase to Value objects
YukiJanse Feb 17, 2026
8a8442b
Fixed wrong order arguments and potential bugs
YukiJanse Feb 18, 2026
1525842
Fixed wrong documentation
YukiJanse Feb 18, 2026
3234838
Fixed the mapper name and wrong class reference in the mapper
YukiJanse Feb 18, 2026
bc60e54
Fixed typo in log messages
YukiJanse Feb 18, 2026
b0a2333
Fixed assertion
YukiJanse Feb 18, 2026
6035404
Fixed a wrong test case
YukiJanse Feb 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## DDD Refactoring (Work in Progress)

This branch contains ongoing refactoring to DDD structure.
4 changes: 3 additions & 1 deletion src/main/java/se/jensen/yuki/springboot/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.persistence.autoconfigure.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan("se.jensen.yuki.springboot.model")
@EntityScan("se.jensen.yuki.springboot")
@EnableJpaRepositories("se.jensen.yuki.springboot")
public class Application {

public static void main(String[] args) {
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/se/jensen/yuki/springboot/config/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import se.jensen.yuki.springboot.exception.UnauthorizedException;
import se.jensen.yuki.springboot.exception.UserNotFoundException;
import se.jensen.yuki.springboot.model.SecurityUser;
import se.jensen.yuki.springboot.model.User;
import se.jensen.yuki.springboot.repository.UserRepository;
import se.jensen.yuki.springboot.service.JwtService;
import se.jensen.yuki.springboot.user.infrastructure.jpa.UserJpaEntity;
import se.jensen.yuki.springboot.user.usecase.UserLoadService;

import java.io.IOException;

@Slf4j
@Component
@RequiredArgsConstructor
public class JwtFilter extends OncePerRequestFilter {
private final JwtService jwtService;
private final UserRepository userRepository;
private final UserLoadService userLoadService;

@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
throws ServletException, IOException {
log.debug("Starting JWT filter for request: {}", request.getRequestURI());
final String authHeader = request.getHeader("Authorization");

if (authHeader == null || !authHeader.startsWith("Bearer ")) {
Expand All @@ -39,24 +42,32 @@ protected void doFilterInternal(HttpServletRequest request,

if (!jwtService.validateToken(jwt)) {
// invalid token: do not set authentication, just continue
log.warn("Invalid JWT token");
filterChain.doFilter(request, response);
return;
}

Long userId = jwtService.extractUserId(jwt);

User user = userRepository.findById(userId).orElseThrow(() -> new UnauthorizedException("User not found"));

SecurityUser securityUser = new SecurityUser(user);
UsernamePasswordAuthenticationToken authToken =
new UsernamePasswordAuthenticationToken(
securityUser,
null,
securityUser.getAuthorities()
);
try {
UserJpaEntity user = userLoadService.requireJpaById(userId);

if (SecurityContextHolder.getContext().getAuthentication() == null) {
SecurityContextHolder.getContext().setAuthentication(authToken);
SecurityUser securityUser = new SecurityUser(user.getId(), user.getEmail(), user.getRole(), user.getPassword());
UsernamePasswordAuthenticationToken authToken =
new UsernamePasswordAuthenticationToken(
securityUser,
null,
securityUser.getAuthorities()
);

if (SecurityContextHolder.getContext().getAuthentication() == null) {
SecurityContextHolder.getContext().setAuthentication(authToken);
}
} catch (UserNotFoundException e) {
log.warn("User not found for ID extracted from JWT: {}", userId);
SecurityContextHolder.clearContext();
// no response manipulation → treat as unauthenticated
}
filterChain.doFilter(request, response);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
import org.mapstruct.Mapping;
import org.mapstruct.NullValuePropertyMappingStrategy;
import se.jensen.yuki.springboot.dto.auth.AuthRegisterRequestDTO;
import se.jensen.yuki.springboot.model.User;
import se.jensen.yuki.springboot.user.infrastructure.jpa.UserJpaEntity;

@Mapper(componentModel = "spring", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface AuthMapper {
@Mapping(target = "role", constant = "USER")
@Mapping(target = "displayName", source = "username")
@Mapping(target = "bio", constant = "")
User registerDtoToUser(AuthRegisterRequestDTO dto);
UserJpaEntity registerDtoToUser(AuthRegisterRequestDTO dto);


// User loginDtoToUser(LoginDTO dto);
//
// LoginDTO toLoginDTO(User user);
// LoginDTO toLoginDTO(UserJpaEntity user);

// User toUser(AuthRegisterRequestDTO requestDTO) {
// User user = new User();
// UserJpaEntity user = new User();
// user.setUsername(requestDTO.username());
// user.setEmail(requestDTO.email());
// user.setPassword(requestDTO.password());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import se.jensen.yuki.springboot.dto.comment.CommentUpdateResponse;
import se.jensen.yuki.springboot.model.Comment;
import se.jensen.yuki.springboot.model.Post;
import se.jensen.yuki.springboot.model.User;
import se.jensen.yuki.springboot.user.infrastructure.jpa.UserJpaEntity;

@Mapper(componentModel = "spring", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface CommentMapper {
Expand All @@ -19,14 +19,14 @@ public interface CommentMapper {
@Mapping(target = "parent", ignore = true)
@Mapping(target = "deleted", expression = "java(false)")
@Mapping(target = "edited", expression = "java(false)")
Comment createRequestToComment(Post post, User author, CommentCreateRequest request);
Comment createRequestToComment(Post post, UserJpaEntity author, CommentCreateRequest request);

@Mapping(target = "id", ignore = true)
@Mapping(target = "content", source = "request.content")
@Mapping(target = "parent", source = "parent")
@Mapping(target = "deleted", expression = "java(false)")
@Mapping(target = "edited", expression = "java(false)")
Comment createRequestToComment(Post post, User author, Comment parent, CommentCreateRequest request);
Comment createRequestToComment(Post post, UserJpaEntity author, Comment parent, CommentCreateRequest request);

@Mapping(target = "authorUserId", source = "comment.author.id")
@Mapping(target = "authorUsername", source = "comment.author.username")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
import se.jensen.yuki.springboot.dto.post.PostCreateResponse;
import se.jensen.yuki.springboot.dto.post.PostFeedResponse;
import se.jensen.yuki.springboot.model.Post;
import se.jensen.yuki.springboot.model.User;
import se.jensen.yuki.springboot.user.infrastructure.jpa.UserJpaEntity;

@Mapper(componentModel = "spring", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface PostMapper {

@Mapping(target = "id", ignore = true)
@Mapping(target = "user", expression = "java(author)")
@Mapping(target = "originalPost", ignore = true)
Post PostCreateToPost(PostCreateRequest requestDTO, User author);
Post PostCreateToPost(PostCreateRequest requestDTO, UserJpaEntity author);

PostCreateResponse toCreateResponse(Post post);


PostFeedResponse toFeedResponse(Post post);
}
26 changes: 0 additions & 26 deletions src/main/java/se/jensen/yuki/springboot/mapper/UserMapper.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/java/se/jensen/yuki/springboot/model/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import se.jensen.yuki.springboot.user.infrastructure.jpa.UserJpaEntity;

@Entity
@Setter
Expand All @@ -26,10 +27,10 @@ public class Block extends BaseEntity {
@ManyToOne(optional = false)
@JoinColumn(name = "blocking_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User blocking;
private UserJpaEntity blocking;

@ManyToOne(optional = false)
@JoinColumn(name = "blocked_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User blocked;
private UserJpaEntity blocked;
}
3 changes: 2 additions & 1 deletion src/main/java/se/jensen/yuki/springboot/model/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import se.jensen.yuki.springboot.user.infrastructure.jpa.UserJpaEntity;

@Entity
@Getter
Expand All @@ -23,7 +24,7 @@ public class Comment extends BaseEntity {
@ManyToOne(optional = false)
@JoinColumn(name = "user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User author;
private UserJpaEntity author;

@ManyToOne(optional = false)
@JoinColumn(name = "post_id", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import se.jensen.yuki.springboot.user.infrastructure.jpa.UserJpaEntity;

@Entity
@Setter
Expand All @@ -26,7 +27,7 @@ public class CommentLike extends BaseEntity {
@ManyToOne(optional = false)
@JoinColumn(name = "user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User user;
private UserJpaEntity user;

@ManyToOne(optional = false)
@JoinColumn(name = "comment_id", nullable = false)
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/se/jensen/yuki/springboot/model/Follow.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import se.jensen.yuki.springboot.user.infrastructure.jpa.UserJpaEntity;

@Entity
@Setter
Expand All @@ -26,10 +27,10 @@ public class Follow extends BaseEntity {
@ManyToOne(optional = false)
@JoinColumn(name = "follower_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User follower;
private UserJpaEntity follower;

@ManyToOne(optional = false)
@JoinColumn(name = "followed_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User followed;
private UserJpaEntity followed;
}
5 changes: 3 additions & 2 deletions src/main/java/se/jensen/yuki/springboot/model/Mute.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import se.jensen.yuki.springboot.user.infrastructure.jpa.UserJpaEntity;

@Entity
@Setter
Expand All @@ -26,10 +27,10 @@ public class Mute extends BaseEntity {
@ManyToOne(optional = false)
@JoinColumn(name = "muting_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User muting;
private UserJpaEntity muting;

@ManyToOne(optional = false)
@JoinColumn(name = "muted_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User muted;
private UserJpaEntity muted;
}
Loading