From 297f0bde6cbee0e27d2d5c92d55d53a853bdc9ad Mon Sep 17 00:00:00 2001 From: oguzhekim Date: Sat, 23 Nov 2024 15:30:42 +0300 Subject: [PATCH 1/2] Move mappers to separate class --- .../demo/dtos/mapper/CustomerMapper.java | 6 --- .../com/group7/demo/dtos/mapper/Mapper.java | 47 +++++++++++++++++++ .../com/group7/demo/services/PostService.java | 25 ++++------ .../demo/services/TrainingProgramService.java | 41 ++++------------ 4 files changed, 63 insertions(+), 56 deletions(-) delete mode 100644 backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/CustomerMapper.java create mode 100644 backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/Mapper.java diff --git a/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/CustomerMapper.java b/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/CustomerMapper.java deleted file mode 100644 index 87e9882..0000000 --- a/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/CustomerMapper.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.group7.demo.dtos.mapper; - - -// TODO: use mapper(mapstruct etc.) or manual -public class CustomerMapper { -} diff --git a/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/Mapper.java b/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/Mapper.java new file mode 100644 index 0000000..942435a --- /dev/null +++ b/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/Mapper.java @@ -0,0 +1,47 @@ +package com.group7.demo.dtos.mapper; + +import com.group7.demo.dtos.*; +import com.group7.demo.models.*; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.stream.Collectors; + +@Component +public class Mapper { + public PostResponse mapToPostResponse(Post post) { + return new PostResponse( + post.getId(), + post.getContent(), + post.getTags().stream().map(Tag::getName).collect(Collectors.toSet()), // Only tag names + post.getCreatedAt(), + post.getUser().getUsername() + ); + } + + public TrainingProgramResponse mapToTrainingProgramResponse(TrainingProgram program) { + return TrainingProgramResponse.builder() + .id(program.getId()) + .title(program.getTitle()) + .description(program.getDescription()) + .trainerUsername(program.getTrainer().getUsername()) + .createdAt(program.getCreatedAt()) + .exercises(program.getExercises().stream() + .map(this::mapToExerciseDetailResponse) + .collect(Collectors.toList())) + .participants(program.getParticipants() == null ? + List.of() : + program.getParticipants().stream() + .map(userTrainingProgram -> userTrainingProgram.getUser().getUsername()) + .collect(Collectors.toList())) + .build(); + } + + public ExerciseDetail mapToExerciseDetailResponse(TrainingProgramExercise trainingProgramExercise) { + return ExerciseDetail.builder() + .exercise(trainingProgramExercise.getExercise()) + .repetitions(trainingProgramExercise.getRepetitions()) + .sets(trainingProgramExercise.getSets()) + .build(); + } +} diff --git a/backend/demo-group7/src/main/java/com/group7/demo/services/PostService.java b/backend/demo-group7/src/main/java/com/group7/demo/services/PostService.java index d56864e..4e5c8a7 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/services/PostService.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/services/PostService.java @@ -2,6 +2,7 @@ import com.group7.demo.dtos.PostRequest; import com.group7.demo.dtos.PostResponse; +import com.group7.demo.dtos.mapper.Mapper; import com.group7.demo.models.Post; import com.group7.demo.models.Tag; import com.group7.demo.models.User; @@ -30,6 +31,8 @@ public class PostService { private AuthenticationService authenticationService; + private Mapper mapper; + public PostResponse createPost(PostRequest postRequest, HttpServletRequest request) { Set tags = new HashSet<>(); @@ -53,7 +56,7 @@ public PostResponse createPost(PostRequest postRequest, HttpServletRequest reque Post savedPost = postRepository.save(post); - return mapToPostResponse(savedPost); + return mapper.mapToPostResponse(savedPost); } @@ -61,7 +64,7 @@ public List getAllPosts() { List posts = postRepository.findAll(); return posts.stream() - .map(this::mapToPostResponse) + .map(mapper::mapToPostResponse) .collect(Collectors.toList()); } @@ -72,7 +75,7 @@ public List getPostsByUser(String username) { List posts = postRepository.findByUser(user); return posts.stream() - .map(this::mapToPostResponse) + .map(mapper::mapToPostResponse) .collect(Collectors.toList()); } @@ -106,7 +109,7 @@ public void deletePost(Long postId, HttpServletRequest request) throws IllegalAc public List getPostsByTags(Set tagNames) { List posts = postRepository.findPostsByTags(tagNames); return posts.stream() - .map(this::mapToPostResponse) + .map(mapper::mapToPostResponse) .collect(Collectors.toList()); } @@ -115,19 +118,7 @@ public List getRandomPosts(int count) { Collections.shuffle(allPosts); return allPosts.stream() .limit(count) - .map(this::mapToPostResponse) + .map(mapper::mapToPostResponse) .collect(Collectors.toList()); } - - private PostResponse mapToPostResponse(Post post) { - return new PostResponse( - post.getId(), - post.getContent(), - post.getTags().stream().map(Tag::getName).collect(Collectors.toSet()), // Only tag names - post.getCreatedAt(), - post.getUser().getUsername() - ); - } - - } diff --git a/backend/demo-group7/src/main/java/com/group7/demo/services/TrainingProgramService.java b/backend/demo-group7/src/main/java/com/group7/demo/services/TrainingProgramService.java index 3e42c0b..2ec93b4 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/services/TrainingProgramService.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/services/TrainingProgramService.java @@ -3,6 +3,7 @@ import com.group7.demo.dtos.ExerciseDetail; import com.group7.demo.dtos.TrainingProgramRequest; import com.group7.demo.dtos.TrainingProgramResponse; +import com.group7.demo.dtos.mapper.Mapper; import com.group7.demo.models.*; import com.group7.demo.repository.ExerciseRepository; import com.group7.demo.repository.TrainingProgramRepository; @@ -33,6 +34,8 @@ public class TrainingProgramService { private final ExerciseRepository exerciseRepository; + private final Mapper mapper; + @Transactional public TrainingProgramResponse createTrainingProgram(TrainingProgramRequest trainingProgramRequest, HttpServletRequest request) throws IllegalAccessException { User user = authenticationService.getAuthenticatedUserInternal(request); @@ -70,35 +73,7 @@ public TrainingProgramResponse createTrainingProgram(TrainingProgramRequest trai // Save the training program and return a response TrainingProgram savedProgram = trainingProgramRepository.save(trainingProgram); - return mapToTrainingProgramResponse(savedProgram); - } - - // Add a method to map TrainingProgram to TrainingProgramResponse - private TrainingProgramResponse mapToTrainingProgramResponse(TrainingProgram program) { - return TrainingProgramResponse.builder() - .id(program.getId()) - .title(program.getTitle()) - .description(program.getDescription()) - .trainerUsername(program.getTrainer().getUsername()) - .createdAt(program.getCreatedAt()) - .exercises(program.getExercises().stream() - .map(this::mapToExerciseResponse) - .collect(Collectors.toList())) - .participants(program.getParticipants() == null ? - List.of() : - program.getParticipants().stream() - .map(userTrainingProgram -> userTrainingProgram.getUser().getUsername()) - .collect(Collectors.toList())) - .build(); - } - - - private ExerciseDetail mapToExerciseResponse(TrainingProgramExercise trainingProgramExercise) { - return ExerciseDetail.builder() - .exercise(trainingProgramExercise.getExercise()) - .repetitions(trainingProgramExercise.getRepetitions()) - .sets(trainingProgramExercise.getSets()) - .build(); + return mapper.mapToTrainingProgramResponse(savedProgram); } public List getAllTrainingPrograms() { @@ -107,7 +82,7 @@ public List getAllTrainingPrograms() { // Map the list of TrainingProgram entities to TrainingProgramResponse DTOs return trainingPrograms.stream() - .map(this::mapToTrainingProgramResponse) // Use the mapping method to convert to response DTOs + .map(mapper::mapToTrainingProgramResponse) // Use the mapping method to convert to response DTOs .collect(Collectors.toList()); } @@ -117,7 +92,7 @@ public TrainingProgramResponse getTrainingProgramById(Long id) { .orElseThrow(() -> new EntityNotFoundException("Training program not found with id: " + id)); // Map the TrainingProgram entity to TrainingProgramResponse DTO - return mapToTrainingProgramResponse(trainingProgram); + return mapper.mapToTrainingProgramResponse(trainingProgram); } @Transactional @@ -127,7 +102,7 @@ public List getTrainingProgramByTrainer(String username List trainingPrograms = trainingProgramRepository.findByTrainer(user); return trainingPrograms.stream() - .map(this::mapToTrainingProgramResponse) + .map(mapper::mapToTrainingProgramResponse) .collect(Collectors.toList()); } @@ -208,7 +183,7 @@ public List getJoinedTrainingPrograms(String username) // Map the list of UserTrainingProgram entities to TrainingProgramResponse DTOs return userTrainingPrograms.stream() .map(UserTrainingProgram::getTrainingProgram) - .map(this::mapToTrainingProgramResponse) + .map(mapper::mapToTrainingProgramResponse) .collect(Collectors.toList()); } From a77ce2843c4ae2974569f90d5bd026d46c0f7dce Mon Sep 17 00:00:00 2001 From: oguzhekim Date: Sat, 23 Nov 2024 15:41:21 +0300 Subject: [PATCH 2/2] Add training program field to posts --- .../java/com/group7/demo/dtos/PostRequest.java | 1 + .../com/group7/demo/dtos/PostResponse.java | 1 + .../com/group7/demo/dtos/mapper/Mapper.java | 3 ++- .../main/java/com/group7/demo/models/Post.java | 6 +++++- .../group7/demo/repository/UserRepository.java | 2 ++ .../com/group7/demo/services/PostService.java | 18 ++++++++++++++---- .../com/group7/demo/services/UserService.java | 3 ++- 7 files changed, 27 insertions(+), 7 deletions(-) diff --git a/backend/demo-group7/src/main/java/com/group7/demo/dtos/PostRequest.java b/backend/demo-group7/src/main/java/com/group7/demo/dtos/PostRequest.java index acbfd53..9686cc9 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/dtos/PostRequest.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/dtos/PostRequest.java @@ -13,4 +13,5 @@ public class PostRequest { private String content; private Set tags; + private Long trainingProgramId; } diff --git a/backend/demo-group7/src/main/java/com/group7/demo/dtos/PostResponse.java b/backend/demo-group7/src/main/java/com/group7/demo/dtos/PostResponse.java index 7a98e83..196710c 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/dtos/PostResponse.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/dtos/PostResponse.java @@ -17,4 +17,5 @@ public class PostResponse { private Set tags; private LocalDateTime createdAt; private String username; + private TrainingProgramResponse trainingProgram; } diff --git a/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/Mapper.java b/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/Mapper.java index 942435a..3fc7d9f 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/Mapper.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/dtos/mapper/Mapper.java @@ -15,7 +15,8 @@ public PostResponse mapToPostResponse(Post post) { post.getContent(), post.getTags().stream().map(Tag::getName).collect(Collectors.toSet()), // Only tag names post.getCreatedAt(), - post.getUser().getUsername() + post.getUser().getUsername(), + post.getTrainingProgram() == null ? null : mapToTrainingProgramResponse(post.getTrainingProgram()) ); } diff --git a/backend/demo-group7/src/main/java/com/group7/demo/models/Post.java b/backend/demo-group7/src/main/java/com/group7/demo/models/Post.java index 3573d66..6b162f6 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/models/Post.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/models/Post.java @@ -1,6 +1,5 @@ package com.group7.demo.models; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import jakarta.persistence.*; import lombok.*; @@ -20,6 +19,8 @@ public class Post { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Lob + @Column(columnDefinition = "TEXT") private String content; private LocalDateTime createdAt; @@ -36,4 +37,7 @@ public class Post { @JoinColumn(name = "user_id", nullable = false) private User user; + @ManyToOne(fetch = FetchType.LAZY, optional = true) + @JoinColumn(name = "training_program_id") + private TrainingProgram trainingProgram; } diff --git a/backend/demo-group7/src/main/java/com/group7/demo/repository/UserRepository.java b/backend/demo-group7/src/main/java/com/group7/demo/repository/UserRepository.java index 4181df8..d9b4384 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/repository/UserRepository.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/repository/UserRepository.java @@ -1,12 +1,14 @@ package com.group7.demo.repository; import com.group7.demo.models.User; +import jakarta.transaction.Transactional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.Optional; @Repository +@Transactional public interface UserRepository extends JpaRepository { Optional findByUsername(String username); Optional findByEmail(String email); diff --git a/backend/demo-group7/src/main/java/com/group7/demo/services/PostService.java b/backend/demo-group7/src/main/java/com/group7/demo/services/PostService.java index 4e5c8a7..14632af 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/services/PostService.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/services/PostService.java @@ -3,11 +3,10 @@ import com.group7.demo.dtos.PostRequest; import com.group7.demo.dtos.PostResponse; import com.group7.demo.dtos.mapper.Mapper; -import com.group7.demo.models.Post; -import com.group7.demo.models.Tag; -import com.group7.demo.models.User; +import com.group7.demo.models.*; import com.group7.demo.repository.PostRepository; import com.group7.demo.repository.TagRepository; +import com.group7.demo.repository.TrainingProgramRepository; import com.group7.demo.repository.UserRepository; import jakarta.persistence.EntityNotFoundException; import jakarta.transaction.Transactional; @@ -29,10 +28,13 @@ public class PostService { private UserRepository userRepository; + private TrainingProgramRepository trainingProgramRepository; + private AuthenticationService authenticationService; private Mapper mapper; + @Transactional public PostResponse createPost(PostRequest postRequest, HttpServletRequest request) { Set tags = new HashSet<>(); @@ -46,12 +48,17 @@ public PostResponse createPost(PostRequest postRequest, HttpServletRequest reque tags.add(tag); } User user = authenticationService.getAuthenticatedUserInternal(request); + // Fetch the associated TrainingProgram if provided + TrainingProgram trainingProgram = Optional.ofNullable(postRequest.getTrainingProgramId()) + .flatMap(trainingProgramRepository::findById) + .orElse(null); Post post = Post.builder() .content(postRequest.getContent()) .createdAt(LocalDateTime.now()) .tags(tags) .user(user) // Associate the post with the user + .trainingProgram(trainingProgram) .build(); Post savedPost = postRepository.save(post); @@ -59,7 +66,7 @@ public PostResponse createPost(PostRequest postRequest, HttpServletRequest reque return mapper.mapToPostResponse(savedPost); } - + @Transactional public List getAllPosts() { List posts = postRepository.findAll(); @@ -68,6 +75,7 @@ public List getAllPosts() { .collect(Collectors.toList()); } + @Transactional public List getPostsByUser(String username) { User user = userRepository.findByUsername(username) .orElseThrow(() -> new EntityNotFoundException("User not found with username: " + username)); @@ -106,6 +114,7 @@ public void deletePost(Long postId, HttpServletRequest request) throws IllegalAc postRepository.delete(post); } + @Transactional public List getPostsByTags(Set tagNames) { List posts = postRepository.findPostsByTags(tagNames); return posts.stream() @@ -113,6 +122,7 @@ public List getPostsByTags(Set tagNames) { .collect(Collectors.toList()); } + @Transactional public List getRandomPosts(int count) { List allPosts = postRepository.findAll(); Collections.shuffle(allPosts); diff --git a/backend/demo-group7/src/main/java/com/group7/demo/services/UserService.java b/backend/demo-group7/src/main/java/com/group7/demo/services/UserService.java index 1141483..db84e79 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/services/UserService.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/services/UserService.java @@ -6,7 +6,7 @@ import com.group7.demo.models.User; import com.group7.demo.repository.UserRepository; import jakarta.servlet.http.HttpServletRequest; -import lombok.RequiredArgsConstructor; +import jakarta.transaction.Transactional; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @@ -31,6 +31,7 @@ public UserService(UserRepository userRepository, this.trainingProgramService = trainingProgramService; } + @Transactional public UserProfileResponse getUserProfile(String username) throws Exception { User user = userRepository.findByUsername(username) .orElseThrow(() -> new Exception("User not found"));