Skip to content

Commit

Permalink
Merge pull request #207 from bounswe/issue#203-share-training-program…
Browse files Browse the repository at this point in the history
…-in-posts

Issue#203 share training program in posts
  • Loading branch information
oguzhekim authored Nov 23, 2024
2 parents f0b14f0 + a77ce28 commit 9ad7967
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class PostRequest {

private String content;
private Set<String> tags;
private Long trainingProgramId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public class PostResponse {
private Set<String> tags;
private LocalDateTime createdAt;
private String username;
private TrainingProgramResponse trainingProgram;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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(),
post.getTrainingProgram() == null ? null : mapToTrainingProgramResponse(post.getTrainingProgram())
);
}

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();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.group7.demo.models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -20,6 +19,8 @@ public class Post {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Lob
@Column(columnDefinition = "TEXT")
private String content;

private LocalDateTime createdAt;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<User, Long> {
Optional<User> findByUsername(String username);
Optional<User> findByEmail(String email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.group7.demo.dtos.PostRequest;
import com.group7.demo.dtos.PostResponse;
import com.group7.demo.models.Post;
import com.group7.demo.models.Tag;
import com.group7.demo.models.User;
import com.group7.demo.dtos.mapper.Mapper;
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;
Expand All @@ -28,8 +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<Tag> tags = new HashSet<>();

Expand All @@ -43,36 +48,42 @@ 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);

return mapToPostResponse(savedPost);
return mapper.mapToPostResponse(savedPost);
}


@Transactional
public List<PostResponse> getAllPosts() {
List<Post> posts = postRepository.findAll();

return posts.stream()
.map(this::mapToPostResponse)
.map(mapper::mapToPostResponse)
.collect(Collectors.toList());
}

@Transactional
public List<PostResponse> getPostsByUser(String username) {
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new EntityNotFoundException("User not found with username: " + username));

List<Post> posts = postRepository.findByUser(user);

return posts.stream()
.map(this::mapToPostResponse)
.map(mapper::mapToPostResponse)
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -103,31 +114,21 @@ public void deletePost(Long postId, HttpServletRequest request) throws IllegalAc
postRepository.delete(post);
}

@Transactional
public List<PostResponse> getPostsByTags(Set<String> tagNames) {
List<Post> posts = postRepository.findPostsByTags(tagNames);
return posts.stream()
.map(this::mapToPostResponse)
.map(mapper::mapToPostResponse)
.collect(Collectors.toList());
}

@Transactional
public List<PostResponse> getRandomPosts(int count) {
List<Post> allPosts = postRepository.findAll();
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()
);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<TrainingProgramResponse> getAllTrainingPrograms() {
Expand All @@ -107,7 +82,7 @@ public List<TrainingProgramResponse> 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());
}

Expand All @@ -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
Expand All @@ -127,7 +102,7 @@ public List<TrainingProgramResponse> getTrainingProgramByTrainer(String username
List<TrainingProgram> trainingPrograms = trainingProgramRepository.findByTrainer(user);

return trainingPrograms.stream()
.map(this::mapToTrainingProgramResponse)
.map(mapper::mapToTrainingProgramResponse)
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -208,7 +183,7 @@ public List<TrainingProgramResponse> 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"));
Expand Down

0 comments on commit 9ad7967

Please sign in to comment.