-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rating for level of difficulty of a question
- Loading branch information
1 parent
164bedc
commit f351a33
Showing
11 changed files
with
339 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...in/java/com/group1/programminglanguagesforum/DTOs/Requests/DifficultyLevelRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.group1.programminglanguagesforum.DTOs.Requests; | ||
|
||
import com.group1.programminglanguagesforum.Entities.DifficultyLevel; | ||
import lombok.*; | ||
|
||
@Builder | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class DifficultyLevelRequestDto { | ||
private DifficultyLevel difficulty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ain/java/com/group1/programminglanguagesforum/DTOs/Responses/QuestionRateResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.group1.programminglanguagesforum.DTOs.Responses; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class QuestionRateResponseDto { | ||
private Long questionId; | ||
private Long easyCount; | ||
private Long mediumCount; | ||
private Long hardCount; | ||
private Long totalCount; | ||
} |
37 changes: 37 additions & 0 deletions
37
...d/src/main/java/com/group1/programminglanguagesforum/Entities/QuestionDifficultyRate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.group1.programminglanguagesforum.Entities; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Entity | ||
@Getter | ||
@Setter | ||
@Table(name = "QUESTION_DIFFICULTY_RATE") | ||
public class QuestionDifficultyRate { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@ManyToOne | ||
@JoinColumn(name = "question_id", nullable = false) | ||
private Question question; | ||
@ManyToOne | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private User user; | ||
@Enumerated(EnumType.STRING) | ||
private DifficultyLevel difficulty; | ||
@Temporal(TemporalType.TIMESTAMP) | ||
private Date rated_at; | ||
|
||
|
||
@PrePersist | ||
protected void onCreate() { | ||
rated_at = new Date(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...a/com/group1/programminglanguagesforum/Repositories/QuestionDifficultyRateRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.group1.programminglanguagesforum.Repositories; | ||
|
||
import com.group1.programminglanguagesforum.Entities.DifficultyLevel; | ||
import com.group1.programminglanguagesforum.Entities.Question; | ||
import com.group1.programminglanguagesforum.Entities.QuestionDifficultyRate; | ||
import com.group1.programminglanguagesforum.Entities.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface QuestionDifficultyRateRepository extends JpaRepository<QuestionDifficultyRate, Long> { | ||
|
||
Optional<QuestionDifficultyRate> findByQuestionAndUser(Question question, User user); | ||
|
||
long countByDifficulty(DifficultyLevel difficultyLevel); | ||
} |
65 changes: 65 additions & 0 deletions
65
...ain/java/com/group1/programminglanguagesforum/Services/QuestionDifficultyRateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.group1.programminglanguagesforum.Services; | ||
|
||
import com.group1.programminglanguagesforum.DTOs.Responses.QuestionRateResponseDto; | ||
import com.group1.programminglanguagesforum.Entities.DifficultyLevel; | ||
import com.group1.programminglanguagesforum.Entities.Question; | ||
import com.group1.programminglanguagesforum.Entities.QuestionDifficultyRate; | ||
import com.group1.programminglanguagesforum.Entities.User; | ||
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException; | ||
import com.group1.programminglanguagesforum.Repositories.QuestionDifficultyRateRepository; | ||
import com.group1.programminglanguagesforum.Repositories.QuestionRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class QuestionDifficultyRateService { | ||
private final QuestionDifficultyRateRepository questionDifficultyRateRepository; | ||
private final UserContextService userContextService; | ||
private final QuestionRepository questionRepository; | ||
|
||
|
||
public QuestionRateResponseDto rateQuestion(Long id, DifficultyLevel difficultyLevel) throws UnauthorizedAccessException { | ||
Question question = questionRepository.findById(id).orElseThrow(() -> new NoSuchElementException("Question not found")); | ||
User user = userContextService.getCurrentUser(); | ||
Optional<QuestionDifficultyRate> questionDifficultyRateOptional = getQuestionDifficultyRate(question, user); | ||
QuestionDifficultyRate questionDifficultyRate = questionDifficultyRateOptional.orElseGet(() -> { | ||
QuestionDifficultyRate newQuestionDifficultyRate = new QuestionDifficultyRate(); | ||
newQuestionDifficultyRate.setQuestion(question); | ||
newQuestionDifficultyRate.setUser(user); | ||
return newQuestionDifficultyRate; | ||
}); | ||
questionDifficultyRate.setDifficulty(difficultyLevel); | ||
questionDifficultyRateRepository.save(questionDifficultyRate); | ||
QuestionRateCounts result = getResult(); | ||
return QuestionRateResponseDto.builder() | ||
.questionId(id) | ||
.easyCount(result.easyCount()) | ||
.mediumCount(result.mediumCount()) | ||
.hardCount(result.hardCount()) | ||
.totalCount(result.easyCount() + result.mediumCount() + result.hardCount()) | ||
.build(); | ||
|
||
|
||
|
||
} | ||
|
||
public Optional<QuestionDifficultyRate> getQuestionDifficultyRate(Question question, User user) { | ||
return questionDifficultyRateRepository.findByQuestionAndUser(question, user); | ||
} | ||
|
||
@NonNull | ||
public QuestionRateCounts getResult() { | ||
long easyCount = questionDifficultyRateRepository.countByDifficulty(DifficultyLevel.EASY); | ||
long mediumCount = questionDifficultyRateRepository.countByDifficulty(DifficultyLevel.MEDIUM); | ||
long hardCount = questionDifficultyRateRepository.countByDifficulty(DifficultyLevel.HARD); | ||
return new QuestionRateCounts(easyCount, mediumCount, hardCount); | ||
} | ||
|
||
public record QuestionRateCounts(long easyCount, long mediumCount, long hardCount) { | ||
} | ||
} |
Oops, something went wrong.