Skip to content

Commit

Permalink
Merge pull request #513 from bounswe/lab-7/pr
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr authored Nov 19, 2024
2 parents bf45154 + f0667e9 commit 8e3dd1c
Show file tree
Hide file tree
Showing 19 changed files with 1,260 additions and 85 deletions.
31 changes: 31 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,37 @@
<version>20231013</version>
</dependency>

<!-- JUnit Jupiter API for writing tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version> <!-- Use the latest stable version -->
<scope>test</scope>
</dependency>

<!-- Mockito Core for mocking objects -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.5.0</version> <!-- Use the latest stable version -->
<scope>test</scope>
</dependency>

<!-- Mockito JUnit Integration -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>

<!-- Spring Boot Test Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.*;
import lombok.*;

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

@Builder
Expand All @@ -20,15 +21,17 @@ public class Answer {
private Long likeCount = 0L;
@Builder.Default
private Long dislikeCount = 0L;
@Lob // Use this annotation to specify that this field can hold a large amount of text
@Lob // Use this annotation to specify that this field can hold a large amount of
// text
@Column(name = "answer_body", columnDefinition = "BLOB")
private String answerBody;
private String answerDate;
@ManyToOne(fetch = FetchType.LAZY) // Fetch lazily for better performance
@JoinColumn(name = "user_id", nullable = false) // Foreign key column
private User answeredBy; // Field to hold the User who answered
@ManyToOne(fetch = FetchType.LAZY) // Fetch lazily for better performance
@JoinColumn(name = "user_id", nullable = false) // Foreign key column
private User answeredBy; // Field to hold the User who answered
@OneToMany(mappedBy = "answer", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Vote> votes;
@Builder.Default
private List<Vote> votes = new ArrayList<>();

public Long getUpvoteCount() {
return votes.stream().filter(Vote::isUpvote).count();
Expand All @@ -38,6 +41,4 @@ public Long getDownvoteCount() {
return votes.stream().filter(vote -> !vote.isUpvote()).count();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import jakarta.persistence.*;
import lombok.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -22,7 +24,7 @@ public class Question {
@Column(name = "QUESTION_BODY", columnDefinition = "BLOB")
private String questionBody;
@Builder.Default
private Long likeCount =0L;
private Long likeCount = 0L;
@Builder.Default
private Long commentCount = 0L;
@Column(name = "CREATED_AT")
Expand All @@ -37,21 +39,20 @@ public class Question {
@JoinColumn(name = "user_id", nullable = false)
private User askedBy;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(
name = "question_tags", // Name of the join table
joinColumns = @JoinColumn(name = "question_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id")
)
@JoinTable(name = "question_tags", // Name of the join table
joinColumns = @JoinColumn(name = "question_id"), inverseJoinColumns = @JoinColumn(name = "tag_id"))
@Builder.Default
private Set<Tag> tags = new HashSet<>();
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Vote> votes;
@Builder.Default
private List<Vote> votes = new ArrayList<>();

public Long getUpvoteCount() {
return votes.stream().filter(Vote::isUpvote).count();
}

public Long getDownvoteCount() {
return votes.stream().filter(vote -> !vote.isUpvote()).count();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,24 @@ private TagType getTagType(Tag tag) {
return TagType.PROGRAMMING_PARADIGM;
} else if (tag instanceof SoftwareLibraryTag) {
return TagType.SOFTWARE_LIBRARY;
} else if (
tag instanceof ComputerScienceTermTag) {
} else if (tag instanceof ComputerScienceTermTag) {
return TagType.COMPUTER_SCIENCE_TOPIC;
} else if (
tag != null
) {
} else if (tag != null) {

return TagType.USER_DEFINED;
} else {
throw new IllegalArgumentException("Unknown tag type");
}


}
public GetTagDetailsResponseDto createTag(CreateTagRequestDto dto){

public GetTagDetailsResponseDto createTag(CreateTagRequestDto dto) {
Tag tag = new Tag(null, dto.getName(), dto.getDescription());
tagRepository.save(tag);
Tag savedTag = tagRepository.save(tag); // Use the returned Tag object with the generated ID
return GetTagDetailsResponseDto.builder()
.tagId(tag.getId())
.name(tag.getTagName())
.description(tag.getTagDescription())
.tagId(savedTag.getId()) // Use savedTag.getId() to get the correct ID
.name(savedTag.getTagName())
.description(savedTag.getTagDescription())
.tagType(TagType.USER_DEFINED.toString())
.build();
}
Expand All @@ -71,19 +68,20 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) {

if (tagType == TagType.PROGRAMMING_LANGUAGE) {
ProgrammingLanguagesTag languageTag = (ProgrammingLanguagesTag) tagEntity;
GetProgrammingLanguageTagResponseDto responseDto = modelMapper.map(languageTag, GetProgrammingLanguageTagResponseDto.class);
GetProgrammingLanguageTagResponseDto responseDto = modelMapper.map(languageTag,
GetProgrammingLanguageTagResponseDto.class);
responseDto.setTagType(tagType.toString());
responseDto.setRelatedQuestions(relatedQuestions);
return responseDto;
} else if (tagType == TagType.PROGRAMMING_PARADIGM) {
ProgrammingParadigmTag paradigmTag = (ProgrammingParadigmTag) tagEntity;
GetProgrammingParadigmResponseDto responseDto = modelMapper.map(paradigmTag, GetProgrammingParadigmResponseDto.class);
GetProgrammingParadigmResponseDto responseDto = modelMapper.map(paradigmTag,
GetProgrammingParadigmResponseDto.class);
responseDto.setTagType(tagType.toString());
responseDto.setRelatedQuestions(relatedQuestions);
return responseDto;
}


return GetTagDetailsResponseDto.builder()
.tagId(tagEntity.getId())
.name(tagEntity.getTagName())
Expand All @@ -93,7 +91,6 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) {

.build();


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ public QuestionUpvoteResponseDto upvoteQuestion(Long questionId) throws Unauthor
User user = userContextService.getCurrentUser();
Vote vote = new Vote();
Question question = questionRepository.findById(questionId).orElseThrow();
question.setLikeCount(question.getLikeCount()+1);
question.setLikeCount(question.getLikeCount() + 1);
vote.setQuestion(question);
vote.setUser(user);
vote.setUpvote(true);
voteRepository.save(vote);

return QuestionUpvoteResponseDto.builder()
.questionId(questionId)
.upvoteCount(question.getUpvoteCount())
Expand Down Expand Up @@ -94,7 +95,7 @@ public AnswerVoteResponseDTO upvoteAnswer(Long answerId) throws Exception {
throw new Exception("User has already voted this answer");
}

answer.setLikeCount(answer.getLikeCount()+1);
answer.setLikeCount(answer.getLikeCount() + 1);

Vote vote = new Vote();
vote.setAnswer(answer);
Expand All @@ -117,7 +118,7 @@ public AnswerVoteResponseDTO downvoteAnswer(Long answerId) throws Exception {
throw new Exception("User has already voted this answer");
}

answer.setDislikeCount(answer.getDislikeCount()+1);
answer.setDislikeCount(answer.getDislikeCount() + 1);

Vote vote = new Vote();
vote.setAnswer(answer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package com.group1.programminglanguagesforum;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
class ProgrammingLanguagesForumApplicationTests {

@Autowired
private MockMvc mockMvc;

@Test
void contextLoads() {
}

@Test
void testSetUp() throws Exception {
mockMvc.perform(get("/api/v1/test"))
.andExpect(status().isOk())
.andExpect(content().string("test"));
}

}
//package com.group1.programminglanguagesforum;
//
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.web.servlet.MockMvc;
//
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//
//@SpringBootTest
//@AutoConfigureMockMvc
//class ProgrammingLanguagesForumApplicationTests {
//
// @Autowired
// private MockMvc mockMvc;
//
// @Test
// void contextLoads() {
// }
//
// @Test
// void testSetUp() throws Exception {
// mockMvc.perform(get("/api/v1/test"))
// .andExpect(status().isOk())
// .andExpect(content().string("test"));
// }
//
//}
Loading

0 comments on commit 8e3dd1c

Please sign in to comment.