Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Refactor forum #27

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion src/main/java/dev/wms/pwrapi/api/EportalAPI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.wms.pwrapi.api;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -62,7 +64,6 @@ public ResponseEntity<List<EportalSection>> getEportalSekcje(@RequestParam Strin
@GetMapping("/kursy/{id}/oceny")
@Operation(summary = "Returns all marks for the given course", description = "You can fetch the course ID using /kursy endpoint")
public ResponseEntity<List<MarkSummary>> getEportalOceny(@RequestParam String login, @RequestParam String password, @PathVariable int id) throws JsonProcessingException {

return ResponseEntity.status(HttpStatus.OK).body(eService.getEportalOceny(login, password, id));
}

Expand Down
169 changes: 62 additions & 107 deletions src/main/java/dev/wms/pwrapi/api/ForumAPI.java
Original file line number Diff line number Diff line change
@@ -1,161 +1,116 @@
package dev.wms.pwrapi.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import dev.wms.pwrapi.entity.forum.Review;
import dev.wms.pwrapi.entity.forum.Teacher;
import dev.wms.pwrapi.service.forum.ForumService;
import dev.wms.pwrapi.entity.forum.TeacherInfoDTO;
import dev.wms.pwrapi.entity.forum.TeacherWithReviewsDTO;
import dev.wms.pwrapi.service.forum.ForumServiceImpl;
import dev.wms.pwrapi.utils.forum.dto.DatabaseMetadataDTO;
import dev.wms.pwrapi.utils.forum.exceptions.*;
import dev.wms.pwrapi.utils.generalExceptions.InvalidIdException;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.List;

@RestController("/api/forum")
@RequestMapping(value = "/api/forum", produces = "application/json")
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.util.Set;

@RestController
@RequestMapping("/api/forum")
@RequiredArgsConstructor
@Validated
public class ForumAPI {

private final ForumService forumService;

@Autowired
public ForumAPI(ForumService forumService){
this.forumService = forumService;
}
private final ForumServiceImpl forumService;

@Cacheable("metadata")
@GetMapping
@Operation(summary = "Returns database metadata such as number of records in each category and latest refresh timestamp.")
public ResponseEntity<DatabaseMetadataDTO> getDatabaseMetadata() throws JsonProcessingException {
DatabaseMetadataDTO result = forumService.getDatabaseMetadata();
return ResponseEntity.status(HttpStatus.OK).body(result);
public ResponseEntity<DatabaseMetadataDTO> getDatabaseMetadata() {
return ResponseEntity.ok(forumService.getDatabaseMetadata());
}

@Cacheable("reviews")
@GetMapping("/opinie")
@Operation(summary = "Returns total number of reviews.")
public ResponseEntity<DatabaseMetadataDTO> getTotalReviews() throws JsonProcessingException {
DatabaseMetadataDTO result = forumService.getTotalReviews();
return ResponseEntity.status(HttpStatus.OK).body(result);
@Operation(summary = "Returns total number of teacher reviews collected.")
public ResponseEntity<DatabaseMetadataDTO> getTotalReviews() {
return ResponseEntity.ok(forumService.getTotalReviews());
}

// TODO -> emtpy return instead of exception in case review is not found
@GetMapping("/opinie/{reviewId}")
@Operation(summary = "Returns review with specified id.")
public ResponseEntity<Review> getReviewById(@PathVariable @Min(1) int reviewId) throws JsonProcessingException {
try {
Review result = forumService.getReviewById(reviewId);
return ResponseEntity.status(HttpStatus.OK).body(result);
}catch (EmptyResultDataAccessException e){
throw new ReviewNotFoundException(reviewId);
}
@Operation(summary = "Returns review with specified reviewId.")
public ResponseEntity<Review> getReviewById(@PathVariable @Positive(message = "reviewId powinno być >= 0") Long reviewId) {
return ResponseEntity.ok(forumService.getReviewById(reviewId));
}

@Cacheable("teachers")
@GetMapping("/prowadzacy")
@Operation(summary = "Returns total number of teachers.")
public ResponseEntity<DatabaseMetadataDTO> getTotalTeachers() throws JsonProcessingException {
DatabaseMetadataDTO result = forumService.getTotalTeachers();
return ResponseEntity.status(HttpStatus.OK).body(result);
public ResponseEntity<DatabaseMetadataDTO> getTotalTeachers() {
return ResponseEntity.ok(forumService.getTotalTeachers());
}

@GetMapping(value = "/prowadzacy/{teacherId}")
@Operation(summary = "Returns teacher with specified id.")
public ResponseEntity<Teacher> fetchAllTeacherReviewsById(@PathVariable int teacherId) throws JsonProcessingException {
if(teacherId <= 0) {
throw new InvalidIdException(teacherId);
}

try{
Teacher result = forumService.fetchLimitedTeacherReviewsById(teacherId, -1);
return ResponseEntity.status(HttpStatus.OK).body(result);
}catch(EmptyResultDataAccessException e){
throw new TeacherNotFoundByIdException(teacherId);
}
public ResponseEntity<TeacherWithReviewsDTO> getTeacherWithReviews(
@PathVariable @Positive (message = "teacherId powinno być >= 0") Long teacherId) {
return ResponseEntity.ok(forumService.getTeacherWithAllReviewsById(teacherId));
}

@GetMapping("/prowadzacy/szukajId")
@Operation(summary = "Returns certain teacher specified by id and limited number of reviews.",
description = "Maximal number of fetched reviews is specified by the limit parameter, set limit = -1 to fetch all available reviews.")
public ResponseEntity<Teacher> fetchLimitedTeacherReviewsById(@RequestParam("teacherId") int teacherId, @RequestParam("limit") int limit) throws JsonProcessingException {
if(teacherId <= 0){
throw new InvalidIdException(teacherId);
}

if(limit >= -1) {
try {
Teacher response = forumService.fetchLimitedTeacherReviewsById(teacherId, limit);
return ResponseEntity.status(HttpStatus.OK).body(response);
}catch(EmptyResultDataAccessException e){
throw new TeacherNotFoundByIdException(teacherId);
}
}else{
throw new InvalidLimitException(limit);
}
description = "Maximal number of fetched reviews is specified by the limit parameter, set limit = -1 to " +
"fetch all available reviews.")
public ResponseEntity<TeacherWithReviewsDTO> getTeacherWithLimitedReviewsById(
@RequestParam("teacherId") @Positive (message = "teacherId powinno być >= 0") Long teacherId,
@RequestParam("limit") @Min(value = -1, message = "limit powinien być >= -1") Long limit) {
return ResponseEntity.ok(forumService.getTeacherWithLimitedReviewsById(teacherId, limit));
}

@GetMapping("/prowadzacy/szukajImie")
@Operation(summary = "Returns certain teacher specified by full name and limited number of reviews.",
description = "Parameters firstName and lastName are interchangeable, query is based on pattern matching. " +
"Maximal number of reviews is specified by the limit parameter, set limit = -1 to fetch all available reviews.")
public ResponseEntity<Teacher> fetchTeacherReviewsByFullName(@RequestParam("firstName") String firstName,
@RequestParam("lastName") String lastName, @RequestParam("limit") int limit) throws JsonProcessingException {
if(limit >= -1){
try {
Teacher response = forumService.fetchLimitedTeacherReviewsByFullName(firstName, lastName, limit);
return ResponseEntity.status(HttpStatus.OK).body(response);
}catch(EmptyResultDataAccessException e){
throw new TeacherNotFoundByFullNameException(firstName, lastName);
}
}else{
throw new InvalidLimitException(limit);
}
description = "Parameters firstName and lastName are interchangeable, query is based on pattern matching. " +
"Maximal number of reviews is specified by the limit parameter, set limit = -1 to fetch all available reviews.")
public ResponseEntity<TeacherWithReviewsDTO> getTeacherWithLimitedReviewsByFullName(
@RequestParam("firstName") @NotNull(message = "firstName jest wymagane") String firstName,
@RequestParam("lastName") @NotNull(message = "lastName jest wymagane") String lastName,
@RequestParam("limit") @Min(value = -1, message = "limit powinien być >= -1") Long limit) {
return ResponseEntity.ok(forumService.getTeacherWithLimitedReviewsByFullName(firstName, lastName, limit));
}

@GetMapping("/prowadzacy/kategoria/{category}")
@Operation(summary = "Returns all teachers who belong to the specified category.")
public ResponseEntity<List<Teacher>> getTeachersByCategory(@PathVariable String category) throws JsonProcessingException {
List<Teacher> response = forumService.getTeachersByCategory(category);
if(response.isEmpty()){
throw new CategoryMembersNotFoundException(category);
}
return ResponseEntity.status(HttpStatus.OK).body(response);
public ResponseEntity<Set<TeacherInfoDTO>> getTeachersByCategory(
@PathVariable String category) {
return ResponseEntity.ok(forumService.getTeachersInfoByCategory(category));
}

//TODO -> add number of reviews (feature)
@GetMapping("/prowadzacy/ranking")
@Operation(summary = "Returns teachers who belong to the specified category ranked by their average rating.")
public ResponseEntity<List<Teacher>> getTeachersRankedByCategory(@RequestParam("kategoria") String category) throws JsonProcessingException {
List<Teacher> response = forumService.getBestTeachersRankedByCategory(category);
if(response.isEmpty()){
throw new CategoryMembersNotFoundException(category);
}
return ResponseEntity.status(HttpStatus.OK).body(response);
public ResponseEntity<Set<TeacherInfoDTO>> getTeachersRankedByCategory(
@RequestParam(name = "kategoria") String category) {
return ResponseEntity.ok(forumService.getBestTeachersOfCategory(category));
}

@GetMapping("/prowadzacy/{category}/ranking/najlepsi")
@Operation(summary = "Returns limited number of best rated teachers who belong to the specified category, example reviews are provided.",
description = "Number of return teachers is specified by the limit parameter, each teacher has a maximal example of three associated reviews.")
public ResponseEntity<List<Teacher>> getBestRankedTeachersByCategoryWithReviewsLimited(@PathVariable String category, @RequestParam("limit") @Min(-1) int limit) throws JsonProcessingException {

List<Teacher> response = forumService.getBestRankedTeachersByCategoryLimited(category, limit);
if(response.isEmpty()){
throw new CategoryMembersNotFoundException(category);
}

return ResponseEntity.status(HttpStatus.OK).body(response);
@Operation(summary = "Returns limited number of best rated teachers who belong to the specified category, " +
"example reviews are provided.",
description = "Number of return teachers is specified by the limit parameter, each teacher has a maximal " +
"example of three associated reviews.")
public ResponseEntity<Set<TeacherWithReviewsDTO>> getBestRankedTeachersByCategoryWithReviewsLimited(
@PathVariable String category, @RequestParam("limit") @Positive(message = "limit powinien być >= 0") int limit) {
return ResponseEntity.ok(forumService.getLimitedBestTeachersOfCategoryWithExampleReviews(category, limit));
}

@GetMapping("/prowadzacy/{category}/ranking/najgorsi")
@Operation(summary = "Returns limited number of worst rated teachers who belong to the specified category, example reviews are provided.",
description = "Number of return teachers is specified by the limit parameter, each teacher has a maximal example of three associated reviews.")
public ResponseEntity<List<Teacher>> getWorstRankedTeachersByCategoryWithReviewsLimited(@PathVariable String category, @RequestParam("limit") @Min(-1) int limit) throws JsonProcessingException {
List<Teacher> response = forumService.getWorstRankedTeachersByCategoryLimited(category, limit);
if(response.isEmpty()){
throw new CategoryMembersNotFoundException(category);
}
return ResponseEntity.status(HttpStatus.OK).body(response);
public ResponseEntity<Set<TeacherWithReviewsDTO>> getWorstRankedTeachersByCategoryWithReviewsLimited(
@PathVariable String category, @RequestParam("limit") @Positive(message = "limit powinien być >= 0") int limit) {
return ResponseEntity.ok(forumService.getLimitedWorstTeachersOfCategoryWithExampleReviews(category, limit));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.wms.pwrapi.dao.forum;

import dev.wms.pwrapi.utils.forum.dto.DatabaseMetadataDTO;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface DatabaseMetadataRepository extends PagingAndSortingRepository<DatabaseMetadataDTO, Long> {

@Query("SELECT (SELECT COUNT(*) FROM teacher) AS 'total_teachers', (SELECT COUNT(*) FROM review) AS 'total_reviews', " +
"(SELECT refresh_date FROM refresh_data) AS 'latest_refresh'")
DatabaseMetadataDTO getDatabaseMetadata();
}
65 changes: 0 additions & 65 deletions src/main/java/dev/wms/pwrapi/dao/forum/ForumDAO.java

This file was deleted.

Loading