Skip to content

Commit

Permalink
Merge pull request #628 from bounswe/backend/feature/604-backend-get-…
Browse files Browse the repository at this point in the history
…usersidfollowed-tags-returning-tags-a-user-follows

Add followed tags to users' profile responses.
  • Loading branch information
EnesBaserr authored Dec 6, 2024
2 parents dce3fb9 + c8b9817 commit 7d88548
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ protected void configure() {

}
});
modelMapper.addMappings(new PropertyMap <User,UserProfileResponseDto>() {
@Override
protected void configure() {
skip(destination.getFollowedTags());

}
});
modelMapper.addMappings(new PropertyMap<Question, GetQuestionWithTagDto>() {
@Override
protected void configure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import com.group1.programminglanguagesforum.Entities.User;
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Exceptions.UserNotFoundException;
import com.group1.programminglanguagesforum.Services.AnswerService;
import com.group1.programminglanguagesforum.Services.QuestionService;
import com.group1.programminglanguagesforum.Services.UserContextService;
import com.group1.programminglanguagesforum.Services.UserService;
import com.group1.programminglanguagesforum.Services.*;
import com.group1.programminglanguagesforum.Util.ApiResponseBuilder;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
Expand All @@ -33,6 +30,7 @@ public class UserController extends BaseController {
private final ModelMapper modelMapper;
private final QuestionService questionService;
private final AnswerService answerService;
private final TagService tagService;

@GetMapping(value = EndpointConstants.UserEndpoints.USER_ME)
public ResponseEntity<GenericApiResponse<SelfProfileResponseDto>> getUser() {
Expand All @@ -43,6 +41,9 @@ public ResponseEntity<GenericApiResponse<SelfProfileResponseDto>> getUser() {
SelfProfileResponseDto.class);
List<QuestionSummaryDto> questions = questionService.findByAuthorId(user.getId());
List<GetAnswerDtoForProfile> answers = answerService.findByAnsweredBy(user.getId());
selfProfileResponseDto.setFollowedTags(
tagService.getFollowedTags(user.getId())
);
selfProfileResponseDto.setQuestionCount((long) questions.size());
selfProfileResponseDto.setQuestions(
questions);
Expand Down Expand Up @@ -81,6 +82,10 @@ public ResponseEntity<GenericApiResponse<UserProfileResponseDto>> getUserById(
UserProfileResponseDto userProfileResponseDto = modelMapper.map(user.get(),
UserProfileResponseDto.class);
userProfileResponseDto.setSelfFollowing(userService.selfFollowing(user.get()));
userProfileResponseDto.setFollowedTags(
tagService.getFollowedTags(user.get().getId())
);


GenericApiResponse<UserProfileResponseDto> response = ApiResponseBuilder.buildSuccessResponse(
userProfileResponseDto.getClass(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.group1.programminglanguagesforum.DTOs.Responses;
import com.group1.programminglanguagesforum.Entities.ExperienceLevel;
import com.group1.programminglanguagesforum.Entities.TagType;
import lombok.*;

import java.util.List;
Expand All @@ -25,4 +26,18 @@ public class SelfProfileResponseDto {
private ExperienceLevel experienceLevel;
private List<QuestionSummaryDto> questions;
private List<GetAnswerDtoForProfile> answers;
private List<FollowedTags> followedTags;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public static class FollowedTags {

private Long id;
private String name;
private TagType tagType;
private String description;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import com.group1.programminglanguagesforum.Entities.ExperienceLevel;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -20,4 +23,7 @@ public class UserProfileResponseDto {
private boolean selfFollowing;
private int reputationPoints;
private ExperienceLevel experienceLevel;
@Builder.Default
private List<SelfProfileResponseDto.FollowedTags> followedTags = new ArrayList<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -14,4 +16,6 @@ public interface TagRepository extends JpaRepository<Tag,Long> {
List<Tag> findAllByIdIn(List<Long> ids);

Page<Tag> findTagsByTagNameContainingIgnoreCase(String tagName, Pageable pageable);
@Query("SELECT t FROM Tag t JOIN t.followers u WHERE u.id = :userId")
List<Tag> findTagByFollowers(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) {
.build();

}
public List<SelfProfileResponseDto.FollowedTags> getFollowedTags(Long userId) {
return tagRepository.findTagByFollowers(userId).stream()
.map(tag -> SelfProfileResponseDto.FollowedTags.builder()
.id(tag.getId())
.name(tag.getTagName())
.tagType(getTagType(tag))
.description(tag.getTagDescription())
.build())
.toList();
}

public Page<GetTagDetailsResponseDto> searchTags(String q, Pageable pageable) {
Page<Tag> tags = tagRepository.findTagsByTagNameContainingIgnoreCase(q, pageable);
Expand Down

0 comments on commit 7d88548

Please sign in to comment.