Skip to content

Commit

Permalink
Merge pull request #570 from bounswe/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr authored Nov 25, 2024
2 parents fcb6698 + 6b2d9c5 commit 926c88d
Show file tree
Hide file tree
Showing 32 changed files with 1,717 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static class SparqlEndpoints {
public static class TagEndpoints {
public static final String BASE_PATH = "/tags";
public static final String TAG_ID = BASE_PATH + "/{id}";
public static final String TAG_FOLLOW = BASE_PATH + "/{id}/follow";
public static final String SEARCH = "/search" + BASE_PATH;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
import com.group1.programminglanguagesforum.DTOs.Responses.ErrorResponse;
import com.group1.programminglanguagesforum.DTOs.Responses.GenericApiResponse;
import com.group1.programminglanguagesforum.DTOs.Responses.GetTagDetailsResponseDto;
import com.group1.programminglanguagesforum.DTOs.Responses.TagDto;
import com.group1.programminglanguagesforum.DTOs.Responses.TagSearchResponseDto;
import com.group1.programminglanguagesforum.Entities.User;
import com.group1.programminglanguagesforum.Exceptions.ExceptionResponseHandler;
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Services.TagService;
import com.group1.programminglanguagesforum.Services.UserContextService;
import com.group1.programminglanguagesforum.Util.ApiResponseBuilder;

import jakarta.persistence.EntityExistsException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -20,11 +27,15 @@
import java.util.Arrays;
import java.util.NoSuchElementException;


@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
public class TagController extends BaseController {

private final TagService tagService;
private final UserContextService userContextService;

@GetMapping(value = EndpointConstants.TagEndpoints.SEARCH)
public ResponseEntity<GenericApiResponse<TagSearchResponseDto>> tagSearch(
@RequestParam String q,
Expand Down Expand Up @@ -107,4 +118,37 @@ public ResponseEntity<GenericApiResponse<GetTagDetailsResponseDto>> createTag(@R
}

}

@PostMapping(value = EndpointConstants.TagEndpoints.TAG_FOLLOW)
public ResponseEntity<GenericApiResponse<TagDto>> postMethodName(@PathVariable(value = "id") Long tagId) {
try {
User user = userContextService.getCurrentUser();
TagDto tagDto = tagService.followTag(user, tagId);
GenericApiResponse<TagDto> response = ApiResponseBuilder.buildSuccessResponse(TagDto.class, "Tag followed successfully", 200, tagDto);
return buildResponse(response, HttpStatus.OK);

} catch (UnauthorizedAccessException e) {
return ExceptionResponseHandler.UnauthorizedAccessException(e);
} catch (NoSuchElementException e) {
return ExceptionResponseHandler.NoSuchElementException(e);
} catch (EntityExistsException e) {
return ExceptionResponseHandler.EntityExistsException(e);
}
}

@DeleteMapping(value = EndpointConstants.TagEndpoints.TAG_FOLLOW)
public ResponseEntity<GenericApiResponse<TagDto>> deleteMethodName(@PathVariable(value = "id") Long tagId) {
try {
User user = userContextService.getCurrentUser();
TagDto tagDto = tagService.unfollowTag(user, tagId);
GenericApiResponse<TagDto> response = ApiResponseBuilder.buildSuccessResponse(TagDto.class, "Tag unfollowed successfully", 200, tagDto);
return buildResponse(response, HttpStatus.OK);

} catch (UnauthorizedAccessException e) {
return ExceptionResponseHandler.UnauthorizedAccessException(e);
} catch (NoSuchElementException e) {
return ExceptionResponseHandler.NoSuchElementException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static class AnswerResponseDto {
private Long upvoteCount;
private Long downvoteCount;
private boolean selfAnswer;
private boolean selfVoted;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class GetQuestionDetailsResponseDto {
private Long dislikeCount;
private Long commentCount;
private Boolean selfQuestion;
private Boolean selfVoted;
private String createdAt;
@Builder.Default
private List<TagDto> tags= new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.group1.programminglanguagesforum.Entities;

import java.util.HashSet;
import java.util.Set;

import jakarta.persistence.*;
import lombok.*;

Expand All @@ -18,6 +21,14 @@ public class Tag {
private String wikidataId;
private String tagName;
private String tagDescription;

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "USER_TAGS",
joinColumns = @JoinColumn(name = "tag_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private Set<User> followers = new HashSet<>();

public Tag(String wikidataId, String tagName, String tagDescription) {
this.wikidataId = wikidataId;
this.tagName = tagName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public class User implements UserDetails {
private int followingCount = 0;
@Builder.Default
private Long reputationPoints = 0L;

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
name = "USER_TAGS",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id")
)
@Builder.Default
private Set<Tag> followedTags = new HashSet<>();

@OneToMany(mappedBy = "askedBy", cascade = CascadeType.ALL, orphanRemoval = true)
@Builder.Default
private List<Question> questions = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.group1.programminglanguagesforum.DTOs.Responses.GenericApiResponse;
import com.group1.programminglanguagesforum.DTOs.Responses.UserProfileResponseDto;
import com.group1.programminglanguagesforum.Util.ApiResponseBuilder;

import jakarta.persistence.EntityExistsException;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

Expand Down Expand Up @@ -88,4 +91,43 @@ public static <T> ResponseEntity<GenericApiResponse<T>> NoSuchElementException(N
);

}

public static <T> ResponseEntity<GenericApiResponse<T>> IllegalArgumentException(IllegalArgumentException e) {
ErrorResponse errorResponse = ErrorResponse.builder()
.errorMessage(e.getMessage())
.stackTrace(Arrays.toString(e.getStackTrace()))
.build();

GenericApiResponse<T> response = ApiResponseBuilder.buildErrorResponse(
UserProfileResponseDto.class,
e.getMessage(),
HttpStatus.BAD_REQUEST.value(),
errorResponse
);

return new ResponseEntity<>(
response,
HttpStatus.valueOf(response.getStatus())
);
}

public static <T> ResponseEntity<GenericApiResponse<T>> EntityExistsException(EntityExistsException e) {
ErrorResponse errorResponse = ErrorResponse.builder()
.errorMessage(e.getMessage())
.stackTrace(Arrays.toString(e.getStackTrace()))
.build();

GenericApiResponse<T> response = ApiResponseBuilder.buildErrorResponse(
UserProfileResponseDto.class,
e.getMessage(),
HttpStatus.CONFLICT.value(),
errorResponse
);

return new ResponseEntity<>(
response,
HttpStatus.valueOf(response.getStatus())
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public interface VoteRepository extends JpaRepository<Vote,Long> {
Optional<Vote> findByUserAndQuestionAndIsUpvote(User user, Question question, boolean isUpvote);
Optional<Vote> findByUserAndAnswer(User user, Answer answer);

Optional<Vote> findByUserAndQuestion(User user, Question question);
Optional<Vote> findByUserAndAnswerAndIsUpvote(User user, Answer answer, boolean b);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.group1.programminglanguagesforum.Entities.User;
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Repositories.AnswerRepository;
import com.group1.programminglanguagesforum.Repositories.VoteRepository;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -19,6 +21,7 @@ public class AnswerService {
private final AnswerRepository answerRepository;
private final UserContextService userContextService;
private final QuestionService questionService;
private final VoteRepository voteRepository;

public CreateAnswerResponseDto createAnswer(Long questionId, CreateAnswerRequestDto createAnswerRequestDto) throws UnauthorizedAccessException {
User currentUser = userContextService.getCurrentUser();
Expand Down Expand Up @@ -92,6 +95,7 @@ public GetAnswersResponseDto getAnswersForQuestion(Long questionId) {
.upvoteCount(answer.getUpvoteCount())
.downvoteCount(answer.getDownvoteCount())
.selfAnswer(currentUser != null && currentUser.getId().equals(answer.getAnsweredBy().getId()))
.selfVoted(currentUser != null && voteRepository.findByUserAndAnswer(currentUser, answer).isPresent())
.build()).toList())
.totalItems(question.getAnswers().size())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Repositories.BookmarkRepository;
import com.group1.programminglanguagesforum.Repositories.QuestionRepository;
import com.group1.programminglanguagesforum.Repositories.VoteRepository;

import lombok.RequiredArgsConstructor;

import org.springframework.data.domain.Page;
Expand All @@ -31,6 +33,7 @@ public class QuestionService {
private final UserContextService userContextService;
private final TagService tagService;
private final BookmarkRepository bookmarkRepository;
private final VoteRepository voteRepository;

public Optional<Question> findById(Long id) {
return questionRepository.findById(id);
Expand Down Expand Up @@ -81,26 +84,23 @@ public CreateQuestionResponseDto createQuestion(CreateQuestionRequestDto dto)
}

public GetQuestionDetailsResponseDto getQuestion(Long id) throws NoSuchElementException {
Optional<Question> questionOptional = questionRepository.findById(id);
if (questionOptional.isEmpty()) {
throw new NoSuchElementException("Question not found");
}
Question question = questionOptional.get();
boolean selfQuestion;
User currentUser;
try {
User currentUser = userContextService.getCurrentUser();
selfQuestion = currentUser.getId().equals(question.getAskedBy().getId());
currentUser = userContextService.getCurrentUser();
} catch (UnauthorizedAccessException e) {
selfQuestion = false;
currentUser = null;
}

boolean isBookmarked;
try {
isBookmarked = isBookmarked(id);
} catch (UnauthorizedAccessException e) {
isBookmarked = false;
Optional<Question> questionOptional = questionRepository.findById(id);
if (questionOptional.isEmpty()) {
throw new NoSuchElementException("Question not found");
}

Question question = questionOptional.get();
boolean selfQuestion = (currentUser != null && currentUser.getId().equals(question.getAskedBy().getId()));
boolean isBookmarked = (currentUser != null && bookmarkRepository.existsByUserAndQuestion(currentUser, question));
boolean selfVoted = (currentUser != null && voteRepository.findByUserAndQuestion(currentUser, question).isPresent());

return GetQuestionDetailsResponseDto.builder()
.id(question.getId())
.title(question.getTitle())
Expand All @@ -109,6 +109,7 @@ public GetQuestionDetailsResponseDto getQuestion(Long id) throws NoSuchElementEx
.dislikeCount(question.getDownvoteCount())
.commentCount(question.getCommentCount())
.selfQuestion(selfQuestion)
.selfVoted(selfVoted)
.createdAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(question.getCreatedAt()))
.updatedAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(question.getUpdatedAt()))
.author(AuthorDto.builder()
Expand All @@ -128,12 +129,6 @@ public GetQuestionDetailsResponseDto getQuestion(Long id) throws NoSuchElementEx

}

private boolean isBookmarked(Long questionId) throws UnauthorizedAccessException {
User user = userContextService.getCurrentUser();
Question question = findById(questionId).orElseThrow();
return bookmarkRepository.existsByUserAndQuestion(user, question);
}

public String deleteQuestion(Long id) {
Optional<Question> questionOptional = questionRepository.findById(id);
if (questionOptional.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.group1.programminglanguagesforum.Entities.*;
import com.group1.programminglanguagesforum.Repositories.QuestionRepository;
import com.group1.programminglanguagesforum.Repositories.TagRepository;
import com.group1.programminglanguagesforum.Repositories.UserRepository;

import jakarta.persistence.EntityExistsException;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
Expand All @@ -21,6 +24,7 @@ public class TagService {
private final TagRepository tagRepository;
private final ModelMapper modelMapper;
private final QuestionRepository questionRepository;
private final UserRepository userRepository;

public List<Tag> findAllByIdIn(List<Long> tagIds) {
return tagRepository.findAllByIdIn(tagIds);
Expand Down Expand Up @@ -103,4 +107,49 @@ public Page<GetTagDetailsResponseDto> searchTags(String q, Pageable pageable) {
.tagType(getTagType(tag).toString())
.build());
}

public TagDto followTag(User user, Long tagId) {

Optional<Tag> tag = tagRepository.findById(tagId);
if (tag.isEmpty()) {
throw new NoSuchElementException("Tag not found");
}

Tag tagEntity = tag.get();

if (user.getFollowedTags().stream().anyMatch(t -> t.getId().equals(tagId))) {
throw new EntityExistsException("User already follows this tag");
}

user.getFollowedTags().add(tagEntity);
userRepository.save(user);

return TagDto.builder()
.id(tagEntity.getId())
.name(tagEntity.getTagName())
.build();
}

public TagDto unfollowTag(User user, Long tagId) {

Optional<Tag> tag = tagRepository.findById(tagId);
if (tag.isEmpty()) {
throw new NoSuchElementException("Tag not found");
}

Tag tagEntity = tag.get();

if (!user.getFollowedTags().stream().anyMatch(t -> t.getId().equals(tagId))) {
throw new NoSuchElementException("User does not follow this tag");
}

user.getFollowedTags().removeIf(t -> t.getId().equals(tagId));
userRepository.save(user);

return TagDto.builder()
.id(tagEntity.getId())
.name(tagEntity.getTagName())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void testCreateTag() {
.build();

// Mock the Tag returned by the repository save method
Tag savedTag = new Tag(1L, null, "New Tag", "Tag description");
Tag savedTag = new Tag(1L, null, "New Tag", "Tag description", null);

// Mock tagRepository behavior
when(tagRepository.save(any(Tag.class))).thenReturn(savedTag);
Expand All @@ -90,7 +90,7 @@ void testCreateTag() {
void testGetTagDetails_Success() {
Long tagId = 1L;

Tag mockTag = new Tag(1L, null, "Tag1", "Description1");
Tag mockTag = new Tag(1L, null, "Tag1", "Description1", null);
List<Question> mockQuestions = Arrays.asList(
new Question(1L, "Question1", "Body1", DifficultyLevel.EASY, 0L, 0L, null, null, null, null, null,null),
new Question(2L, "Question2", "Body2", DifficultyLevel.MEDIUM, 0L, 0L, null, null, null, null, null,null));
Expand Down
Loading

0 comments on commit 926c88d

Please sign in to comment.