diff --git a/.DS_Store b/.DS_Store index feca8613..3bd41f5f 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/backend/src/main/java/com/waa/project/aop/FeedbackHandlerAspect.java b/backend/src/main/java/com/waa/project/aop/FeedbackHandlerAspect.java index 03a751f1..680aeb59 100644 --- a/backend/src/main/java/com/waa/project/aop/FeedbackHandlerAspect.java +++ b/backend/src/main/java/com/waa/project/aop/FeedbackHandlerAspect.java @@ -12,12 +12,12 @@ public class FeedbackHandlerAspect { @Before("execution(* com.waa.project.controller.feedback.FeedbackController.*(..))") public void beforeMe(JoinPoint joinPoint) { - System.out.println("Execution Before JoingPoint == " + joinPoint.getSignature().getName()); + //System.out.println("Execution Before JoingPoint == " + joinPoint.getSignature().getName()); } @After("execution(* com.waa.project.controller.feedback.FeedbackController.*(..))") public void afterMe(JoinPoint joinPoint) { - System.out.println("Execution After JoingPoint == " + joinPoint.getSignature().getName()); + // System.out.println("Execution After JoingPoint == " + joinPoint.getSignature().getName()); } } diff --git a/backend/src/main/java/com/waa/project/controller/academic/AcademicController.java b/backend/src/main/java/com/waa/project/controller/academic/AcademicController.java index 33a5d00b..95466fbd 100644 --- a/backend/src/main/java/com/waa/project/controller/academic/AcademicController.java +++ b/backend/src/main/java/com/waa/project/controller/academic/AcademicController.java @@ -1,11 +1,15 @@ package com.waa.project.controller.academic; -import com.waa.project.dto.AcademicResourceDto; -import com.waa.project.dto.AcademicResourceRequest; +import com.waa.project.dto.requests.AcademicResourceRequest; +import com.waa.project.dto.responses.AcademicResourceResponse; import com.waa.project.service.AcademicResService; +import jakarta.validation.Valid; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; +import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -20,38 +24,42 @@ public class AcademicController { @Autowired private AcademicResService academicResService; - @GetMapping({"/admins/academic", "/students/academic"}) - public List getAllResources() { + @Autowired + private ModelMapper modelMapper; + + @GetMapping({"/academic"})//, "/students/academic"}) + public List getAllResources() { return academicResService.getAllAcademicRes(); } - @GetMapping({"/admins/academic/{Id}", "/students/academic/{Id}"}) - public AcademicResourceDto getResource(@PathVariable Long Id) { + @GetMapping({"/academic/{Id}"})//, "/students/academic/{Id}"}) + public AcademicResourceResponse getResource(@PathVariable Long Id) { return academicResService.getAcademicResource(Id); } - @PutMapping({"/admins/academic/{Id}", "/students/academic/{Id}"}) - public String update( - @RequestBody AcademicResourceDto dto, @PathVariable Long Id - ) { - return academicResService.update(dto, Id); + @PutMapping({"/academic/{Id}"})//, "/students/academic/{Id}"}) + public ResponseEntity update( + @Valid @RequestPart(value = "formdata") AcademicResourceRequest dto, + @PathVariable Long Id, + @RequestPart(value = "file", required = false) MultipartFile file) { + return new ResponseEntity<>(academicResService.update(dto, Id, file), HttpStatus.CREATED); } - @PostMapping(value = {"/admins/academic", "/students/academic"}, consumes = "multipart/form-data") - public String save(@RequestPart(value = "formdata") AcademicResourceRequest request, - @RequestPart(value = "file", required = false) MultipartFile file) { - return academicResService.save(request, file); + @PostMapping(value = {"/academic"})//, "/students/academic"}, consumes = "multipart/form-data") + public ResponseEntity save(@Valid @RequestPart(value = "formdata") AcademicResourceRequest request, + @RequestPart(value = "file", required = false) MultipartFile file) { + return new ResponseEntity<>(academicResService.save(request, file), HttpStatus.CREATED); } @GetMapping({"/admins/academic/search", "/students/academic/search"}) - public List search(@RequestParam String search) { + public List search(@RequestParam String search) { return academicResService.searchByResourceName(search); } - @DeleteMapping({"/admins/academic/{Id}", "/students/academic/{Id}"}) - public String delete(@PathVariable Long Id) { + @DeleteMapping({"/academic/{Id}"})//, "/students/academic/{Id}"}) + public List delete(@PathVariable Long Id) { return academicResService.delete(Id); } diff --git a/backend/src/main/java/com/waa/project/controller/academic/AcademicResourceTypeController.java b/backend/src/main/java/com/waa/project/controller/academic/AcademicResourceTypeController.java index 6230c842..14a020c4 100644 --- a/backend/src/main/java/com/waa/project/controller/academic/AcademicResourceTypeController.java +++ b/backend/src/main/java/com/waa/project/controller/academic/AcademicResourceTypeController.java @@ -1,10 +1,16 @@ package com.waa.project.controller.academic; import com.waa.project.dto.AcademicResourceTypeDto; +import com.waa.project.dto.requests.AcademicResourceTypeRequest; +import com.waa.project.dto.responses.AcademicResourceTypeResponse; +import com.waa.project.entity.AcademicResourceType; import com.waa.project.service.AcademicResourceTypeService; +import jakarta.validation.Valid; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -18,31 +24,32 @@ public class AcademicResourceTypeController { @Autowired private AcademicResourceTypeService academicResourceTypeService; - @GetMapping({"/admins/academicresources", "/students/academicresources"}) + @GetMapping({"/academicresources"})//, "/students/academicresources"}) public List getAllResourcesTypes() { return academicResourceTypeService.getAllCategories(); } - @GetMapping({"/admins/academicresources/{catId}", "/students/academicresources/{catId}"}) + @GetMapping({"/academicresources/{catId}"})//, "/students/academicresources/{catId}"}) public AcademicResourceTypeDto getResourceType(@PathVariable Long catId) { return academicResourceTypeService.getAcademicResourceType(catId); } - @PutMapping({"/admins/academicresources/{catId}", "/students/academicresources/{catId}"}) - public String update( - @RequestBody AcademicResourceTypeDto dto, + @PutMapping({"/academicresources/{catId}"})//, "/students/academicresources/{catId}"}) + public ResponseEntity update( + @Valid @RequestBody AcademicResourceTypeRequest request, @PathVariable Long catId - ) { - return academicResourceTypeService.update(dto, catId); + ) { + return new ResponseEntity<>(academicResourceTypeService.update(request, catId), HttpStatus.CREATED); } - @PostMapping({"/admins/academicresources", "/students/academicresources"}) - public String save(@RequestBody AcademicResourceTypeDto dto) { - return academicResourceTypeService.save(dto); + @PostMapping({"/academicresources"})//, "/students/academicresources"}) + public ResponseEntity save( + @Valid @RequestBody AcademicResourceTypeRequest request) { + return new ResponseEntity<>(academicResourceTypeService.save(request), HttpStatus.CREATED); } - @DeleteMapping({"/admins/academicresources/{catId}", "/students/academicresources/{catId}"}) - public String delete(@PathVariable Long catId) { + @DeleteMapping({"/academicresources/{catId}"})//, "/students/academicresources/{catId}"}) + public List delete(@PathVariable Long catId) { return academicResourceTypeService.delete(catId); } } diff --git a/backend/src/main/java/com/waa/project/controller/feedback/FeedbackCategoryController.java b/backend/src/main/java/com/waa/project/controller/feedback/FeedbackCategoryController.java index a7c4e531..15f2e9e2 100644 --- a/backend/src/main/java/com/waa/project/controller/feedback/FeedbackCategoryController.java +++ b/backend/src/main/java/com/waa/project/controller/feedback/FeedbackCategoryController.java @@ -1,10 +1,16 @@ package com.waa.project.controller.feedback; import com.waa.project.dto.FeedbackCategoryDto; +import com.waa.project.dto.requests.FeedbackCategoryRequest; +import com.waa.project.dto.responses.FeedbackCategoryResponse; +import com.waa.project.entity.FeedbackCategory; import com.waa.project.service.FeedbackCategoryService; +import jakarta.validation.Valid; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -29,20 +35,21 @@ public FeedbackCategoryDto getAllFeedCategory(@PathVariable Long catId) { } @PutMapping("/admins/feedbackcategories/{catId}") - public String getAllFeedCategory( - @RequestBody FeedbackCategoryDto feedbackCategoryDto, + public String update( + @Valid @RequestBody FeedbackCategoryDto feedbackCategoryDto, @PathVariable Long catId - ) { + ) { return feedbackCategoryService.update(feedbackCategoryDto, catId); } @PostMapping("/admins/feedbackcategories") - public String getAllFeedCategory(@RequestBody FeedbackCategoryDto feedbackCategoryDto) { - return feedbackCategoryService.save(feedbackCategoryDto); + public ResponseEntity saveFeedbackCategory( + @Valid @RequestBody FeedbackCategoryRequest feedbackCategory) { + return new ResponseEntity<>(feedbackCategoryService.save(feedbackCategory), HttpStatus.CREATED); } @DeleteMapping("/admins/feedbackcategories/{catId}") - public String delete(@PathVariable Long catId) { + public List delete(@PathVariable Long catId) { return feedbackCategoryService.delete(catId); } diff --git a/backend/src/main/java/com/waa/project/controller/feedback/FeedbackController.java b/backend/src/main/java/com/waa/project/controller/feedback/FeedbackController.java index c714b6de..509d5d83 100644 --- a/backend/src/main/java/com/waa/project/controller/feedback/FeedbackController.java +++ b/backend/src/main/java/com/waa/project/controller/feedback/FeedbackController.java @@ -1,11 +1,17 @@ package com.waa.project.controller.feedback; import com.waa.project.dto.FeedbackDto; +import com.waa.project.dto.requests.FeedbackRequest; +import com.waa.project.dto.responses.FeedbackResponse; +import com.waa.project.entity.Feedback; import com.waa.project.service.FeedbackService; import com.waa.project.service.UserService; +import jakarta.validation.Valid; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.*; @@ -28,7 +34,7 @@ public class FeedbackController { private UserService userService; @GetMapping("/feedbacks") - public List getAllFeedBack() { + public List getAllFeedBack() { return feedbackService.getAllFeedbacks(); } @@ -53,34 +59,35 @@ public List getFeedByCategory(@PathVariable Long Id) { return feedbackService.findFeedbackByCategory(Id); } - @PutMapping({"/admins/feedbacks/{Id}", "/students/feedbacks/{Id}"}) - public String update( - @RequestBody FeedbackDto feedbackDto, + @PutMapping({"/feedbacks/{Id}"})//, "/students/feedbacks/{Id}"}) + public ResponseEntity update( + @Valid @RequestBody FeedbackRequest feedbackRequest, @PathVariable Long Id, @AuthenticationPrincipal UserDetails userDetails ) { + Long currentUser = null; if (userDetails != null && userDetails.getUsername() != null) { currentUser = userService.findByUsername(userDetails.getUsername()).getId(); } + return new ResponseEntity<>(feedbackService.update(feedbackRequest, Id, currentUser), HttpStatus.CREATED); - return feedbackService.update(feedbackDto, Id, currentUser); } @PostMapping("/feedbacks") - public String save(@RequestBody FeedbackDto feedbackDto, @AuthenticationPrincipal UserDetails userDetails) { + public ResponseEntity save( + @Valid @RequestBody FeedbackRequest feedbackRequest, + @AuthenticationPrincipal UserDetails userDetails) { Long currentUser = null; - if (userDetails != null && userDetails.getUsername() != null) { currentUser = userService.findByUsername(userDetails.getUsername()).getId(); } - return feedbackService.save(feedbackDto, currentUser); + return new ResponseEntity<>(feedbackService.save(feedbackRequest, currentUser), HttpStatus.CREATED); } - @DeleteMapping({"/admins/feedbacks/{Id}", "/students/feedbacks/{Id}"}) - public String delete(@PathVariable Long Id) { - + @DeleteMapping({"/feedbacks/{Id}"})// "/students/feedbacks/{Id}"}) + public List delete(@PathVariable Long Id) { return feedbackService.delete(Id); } @@ -97,6 +104,7 @@ public Map findFeedbackByCategoryCount() { @GetMapping("/feedbacks/showMe") public Map showMe(@AuthenticationPrincipal UserDetails userDetails) { + System.out.println("userDetails===" + userDetails); var result = userService.findByUsername(userDetails.getUsername()); Map response = new HashMap<>(); response.put("name", result.getUsername()); @@ -104,6 +112,5 @@ public Map showMe(@AuthenticationPrincipal UserDetails userDetai response.put("role", result.getUsername()); response.put("id", result.getId().toString()); return response; - } } diff --git a/backend/src/main/java/com/waa/project/dto/AcademicResourceRequest.java b/backend/src/main/java/com/waa/project/dto/AcademicResourceRequest.java deleted file mode 100644 index c20f5240..00000000 --- a/backend/src/main/java/com/waa/project/dto/AcademicResourceRequest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.waa.project.dto; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.io.File; - -@Data -@AllArgsConstructor -@NoArgsConstructor -public class AcademicResourceRequest { - private String name; - private File file; - private Long resourceId; -} diff --git a/backend/src/main/java/com/waa/project/dto/FeedbackCategoryDto.java b/backend/src/main/java/com/waa/project/dto/FeedbackCategoryDto.java index ff793d09..bfc8e80a 100644 --- a/backend/src/main/java/com/waa/project/dto/FeedbackCategoryDto.java +++ b/backend/src/main/java/com/waa/project/dto/FeedbackCategoryDto.java @@ -4,7 +4,7 @@ @Data public class FeedbackCategoryDto { - private Long id; + private Long id; private String name; private String description; } diff --git a/backend/src/main/java/com/waa/project/dto/FeedbackDto.java b/backend/src/main/java/com/waa/project/dto/FeedbackDto.java index 88da4d02..517f4b2f 100644 --- a/backend/src/main/java/com/waa/project/dto/FeedbackDto.java +++ b/backend/src/main/java/com/waa/project/dto/FeedbackDto.java @@ -6,9 +6,10 @@ @Data public class FeedbackDto { - private Long id; - private String title; - private String body; + private Long id; + private String title; + private String body; private FeedbackCategory feedbackCategory; - private Student student; + private Student student; + private Long category; } diff --git a/backend/src/main/java/com/waa/project/dto/requests/AcademicResourceRequest.java b/backend/src/main/java/com/waa/project/dto/requests/AcademicResourceRequest.java new file mode 100644 index 00000000..c7bcf2c5 --- /dev/null +++ b/backend/src/main/java/com/waa/project/dto/requests/AcademicResourceRequest.java @@ -0,0 +1,30 @@ +package com.waa.project.dto.requests; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.File; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class AcademicResourceRequest { + @NotNull(message = "Name cannot be null.") + @NotBlank(message = "Name cannot be blank.") + @Size(min = 2, max = 250, message = "Name must be minimum of 2 characters long.") + private String name; + + @NotNull(message = "Description cannot be null.") + @NotBlank(message = "Description cannot be blank.") + @Size(min = 2, max = 250, message = "Description must be minimum of 2 characters long.") + private String body; + + private File file; + + @NotNull(message = "Category cannot be empty.") + private Long resourceId; +} diff --git a/backend/src/main/java/com/waa/project/dto/requests/AcademicResourceTypeRequest.java b/backend/src/main/java/com/waa/project/dto/requests/AcademicResourceTypeRequest.java new file mode 100644 index 00000000..0a22149a --- /dev/null +++ b/backend/src/main/java/com/waa/project/dto/requests/AcademicResourceTypeRequest.java @@ -0,0 +1,15 @@ +package com.waa.project.dto.requests; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class AcademicResourceTypeRequest { + @NotNull(message = "Name cannot be null.") + @NotBlank(message = "Name cannot be blank.") + @Size(min = 2, max = 250, message = "Name must be minimum of 2 characters long.") + private String name; + +} diff --git a/backend/src/main/java/com/waa/project/dto/requests/FeedbackCategoryRequest.java b/backend/src/main/java/com/waa/project/dto/requests/FeedbackCategoryRequest.java new file mode 100644 index 00000000..7f85a037 --- /dev/null +++ b/backend/src/main/java/com/waa/project/dto/requests/FeedbackCategoryRequest.java @@ -0,0 +1,19 @@ +package com.waa.project.dto.requests; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class FeedbackCategoryRequest { + @NotNull(message = "Category name cannot be null.") + @NotBlank(message = "Category name cannot be blank.") + @Size(min = 2, max = 250, message = "Category name must be minimum of 2 characters long.") + private String name; + + @NotNull(message = "Description cannot be null.") + @NotBlank(message = "Description cannot be blank.") + @Size(min = 2, max = 250, message = "Description must be minimum of 2 characters long.") + private String description; +} diff --git a/backend/src/main/java/com/waa/project/dto/requests/FeedbackRequest.java b/backend/src/main/java/com/waa/project/dto/requests/FeedbackRequest.java new file mode 100644 index 00000000..fcb18045 --- /dev/null +++ b/backend/src/main/java/com/waa/project/dto/requests/FeedbackRequest.java @@ -0,0 +1,22 @@ +package com.waa.project.dto.requests; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class FeedbackRequest { + @NotNull(message = "Title cannot be null.") + @NotBlank(message = "Title cannot be blank.") + @Size(min = 2, max = 250, message = "Title must be minimum of 2 characters long.") + private String title; + + @NotNull(message = "Description cannot be null.") + @NotBlank(message = "Description cannot be blank.") + @Size(min = 2, max = 250, message = "Description must be minimum of 2 characters long.") + private String body; + + @NotNull(message = "Category cannot be empty.") + private Long feedbackCategory; +} diff --git a/backend/src/main/java/com/waa/project/dto/AcademicResourceDto.java b/backend/src/main/java/com/waa/project/dto/responses/AcademicResourceResponse.java similarity index 65% rename from backend/src/main/java/com/waa/project/dto/AcademicResourceDto.java rename to backend/src/main/java/com/waa/project/dto/responses/AcademicResourceResponse.java index 800f4132..b70d6f99 100644 --- a/backend/src/main/java/com/waa/project/dto/AcademicResourceDto.java +++ b/backend/src/main/java/com/waa/project/dto/responses/AcademicResourceResponse.java @@ -1,17 +1,20 @@ -package com.waa.project.dto; +package com.waa.project.dto.responses; import com.waa.project.entity.AcademicResourceType; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import java.io.File; + @Data @AllArgsConstructor @NoArgsConstructor -public class AcademicResourceDto { +public class AcademicResourceResponse { private Long id; private String name; - private String file; + private String body; + private File file; private AcademicResourceType resourceType; } diff --git a/backend/src/main/java/com/waa/project/dto/responses/AcademicResourceTypeResponse.java b/backend/src/main/java/com/waa/project/dto/responses/AcademicResourceTypeResponse.java new file mode 100644 index 00000000..72fe773c --- /dev/null +++ b/backend/src/main/java/com/waa/project/dto/responses/AcademicResourceTypeResponse.java @@ -0,0 +1,18 @@ +package com.waa.project.dto.responses; + +import com.waa.project.entity.AcademicResourceType; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class AcademicResourceTypeResponse { + private Long id; + private String name; + private List academicResourceTypeList; +} + diff --git a/backend/src/main/java/com/waa/project/dto/responses/FeedbackCategoryResponse.java b/backend/src/main/java/com/waa/project/dto/responses/FeedbackCategoryResponse.java new file mode 100644 index 00000000..5a5cbaa3 --- /dev/null +++ b/backend/src/main/java/com/waa/project/dto/responses/FeedbackCategoryResponse.java @@ -0,0 +1,18 @@ +package com.waa.project.dto.responses; + +import com.waa.project.entity.FeedbackCategory; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class FeedbackCategoryResponse { + private Long id; + private String name; + private String description; + private List categoryList; +} diff --git a/backend/src/main/java/com/waa/project/dto/responses/FeedbackResponse.java b/backend/src/main/java/com/waa/project/dto/responses/FeedbackResponse.java new file mode 100644 index 00000000..3983f4f6 --- /dev/null +++ b/backend/src/main/java/com/waa/project/dto/responses/FeedbackResponse.java @@ -0,0 +1,18 @@ +package com.waa.project.dto.responses; + +import com.waa.project.entity.FeedbackCategory; +import com.waa.project.entity.Student; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class FeedbackResponse { + private Long id; + private String title; + private String body; + private FeedbackCategory feedbackCategory; + private Student student; +} diff --git a/backend/src/main/java/com/waa/project/entity/Feedback.java b/backend/src/main/java/com/waa/project/entity/Feedback.java index 83ca3b8d..2ede06e0 100644 --- a/backend/src/main/java/com/waa/project/entity/Feedback.java +++ b/backend/src/main/java/com/waa/project/entity/Feedback.java @@ -3,6 +3,8 @@ import jakarta.persistence.*; import lombok.Data; +import java.time.LocalDateTime; + @Entity @Data @Table(name = "feedbacks") @@ -24,4 +26,12 @@ public class Feedback { @ManyToOne @JoinColumn(name = "student_id", nullable = true) private Student student; + + @Column(name = "posted_date", nullable = false, updatable = false) + private LocalDateTime postedDate; + + @PrePersist + protected void onCreate() { + postedDate = LocalDateTime.now(); + } } diff --git a/backend/src/main/java/com/waa/project/entity/FeedbackCategory.java b/backend/src/main/java/com/waa/project/entity/FeedbackCategory.java index 68008779..0a1e8795 100644 --- a/backend/src/main/java/com/waa/project/entity/FeedbackCategory.java +++ b/backend/src/main/java/com/waa/project/entity/FeedbackCategory.java @@ -1,6 +1,7 @@ package com.waa.project.entity; import jakarta.persistence.*; +import jakarta.validation.constraints.Size; import lombok.Data; @Entity @@ -13,9 +14,11 @@ public class FeedbackCategory { private Long id; @Column(name = "name", nullable = false) + @Size(min = 2, max = 255, message = "Category name must be minimum of 2 characters long") private String name; @Column(name = "description") + @Size(min = 2, max = 255, message = "Description must be minimum of 2 characters long") private String description; @Embedded diff --git a/backend/src/main/java/com/waa/project/repository/DiscussionCommentsRepository.java b/backend/src/main/java/com/waa/project/repository/DiscussionCommentsRepository.java index 2af8cfed..7dcbc80c 100644 --- a/backend/src/main/java/com/waa/project/repository/DiscussionCommentsRepository.java +++ b/backend/src/main/java/com/waa/project/repository/DiscussionCommentsRepository.java @@ -4,12 +4,9 @@ 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.Modifying; import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; -import java.util.List; import java.util.Optional; @Repository @@ -26,15 +23,4 @@ public interface DiscussionCommentsRepository extends JpaRepository findByParentCommentId(Long parentCommentId, Pageable pageable); - - void deleteAllByParentCommentId(DiscussionComments id); - - List findAllByDiscussionId(Long discussionId); - - @Modifying - @Query("DELETE FROM DiscussionComments dc WHERE dc.parentCommentId.id IN :parentCommentIds") - void deleteAllByParentCommentIds(@Param("parentCommentIds") List parentCommentIds); - - void deleteByDiscussionId(Long discussionId); - } diff --git a/backend/src/main/java/com/waa/project/repository/impl/FileRepositoryImpl.java b/backend/src/main/java/com/waa/project/repository/impl/FileRepositoryImpl.java index 07b3b73f..475805cc 100644 --- a/backend/src/main/java/com/waa/project/repository/impl/FileRepositoryImpl.java +++ b/backend/src/main/java/com/waa/project/repository/impl/FileRepositoryImpl.java @@ -15,7 +15,7 @@ @Component public class FileRepositoryImpl implements FileRepository { - private static final String BASE_PATH = "uploads"; + private static final String BASE_PATH = "uploads"; private static final String DATE_FORMAT = "yyyyMMddhhmmssSSS"; @Override @@ -28,7 +28,6 @@ public String saveFile(File file, String saveLocation) { try { Path uploadsDirPath = Paths.get(getUploadsDirectory().toString(), saveLocation); - System.out.println(getUploadsDirectory().toAbsolutePath()); System.out.println(uploadsDirPath); @@ -37,8 +36,8 @@ public String saveFile(File file, String saveLocation) { } String sanitizedFilename = sanitizeFilename(file.getName()); - String uniqueFilename = generateUniqueFilename(sanitizedFilename); - Path filePath = uploadsDirPath.resolve(uniqueFilename).normalize(); + String uniqueFilename = generateUniqueFilename(sanitizedFilename); + Path filePath = uploadsDirPath.resolve(uniqueFilename).normalize(); Files.copy(file.toPath(), filePath); return filePath.toString().substring(System.getProperty("user.dir").length()); @@ -74,7 +73,7 @@ private String generateUniqueFilename(String filename) { int extensionIndex = filename.lastIndexOf('.'); if (extensionIndex > 0) { extension = filename.substring(extensionIndex); - filename = filename.substring(0, extensionIndex); + filename = filename.substring(0, extensionIndex); } return filename + "_" + timestamp + extension; diff --git a/backend/src/main/java/com/waa/project/security/config/CorsConfig.java b/backend/src/main/java/com/waa/project/security/config/CorsConfig.java index 26ebfb99..1f431f5f 100644 --- a/backend/src/main/java/com/waa/project/security/config/CorsConfig.java +++ b/backend/src/main/java/com/waa/project/security/config/CorsConfig.java @@ -14,9 +14,7 @@ public class CorsConfig { @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); - configuration.setAllowedOrigins(Arrays.asList("http://127.0.0.1:5173", "http://localhost:5173", - "http://localhost:3000" - )); + configuration.setAllowedOrigins(Arrays.asList("http://127.0.0.1:3000", "http://localhost:3000")); configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type")); configuration.setAllowCredentials(true); diff --git a/backend/src/main/java/com/waa/project/security/config/SecurityConfig.java b/backend/src/main/java/com/waa/project/security/config/SecurityConfig.java index 2e5f9523..23242032 100644 --- a/backend/src/main/java/com/waa/project/security/config/SecurityConfig.java +++ b/backend/src/main/java/com/waa/project/security/config/SecurityConfig.java @@ -31,25 +31,24 @@ public SecurityConfig(CorsConfigurationSource corsConfigurationSource) { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http, JwtTokenFilter jwtTokenFilter) throws Exception { return http.csrf(AbstractHttpConfigurer::disable) -// .cors(AbstractHttpConfigurer::disable) - .cors(cors -> cors.configurationSource(corsConfigurationSource)) - .sessionManagement( - sessionMgr -> sessionMgr.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) - .authorizeHttpRequests( - authorizeRequests -> authorizeRequests.requestMatchers("/api/v1/admins/**") - .hasRole(RoleType.ADMIN.name()) - .requestMatchers("/api/v1/students/**") - .hasRole(RoleType.STUDENT.name()) - .anyRequest() - .permitAll() - ) - .exceptionHandling( - exceptionHandler -> exceptionHandler.authenticationEntryPoint( - (request, response, authException) -> response.sendError( - HttpServletResponse.SC_UNAUTHORIZED, AuthErrorMessages.invalidToken())) - ) - .addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class) - .build(); + .cors(cors -> cors.configurationSource(corsConfigurationSource)) + .sessionManagement( + sessionMgr -> sessionMgr.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) + .authorizeHttpRequests( + authorizeRequests -> authorizeRequests.requestMatchers("/api/v1/admins/**") + .hasRole(RoleType.ADMIN.name()) + .requestMatchers("/api/v1/students/**") + .hasRole(RoleType.STUDENT.name()) + .anyRequest() + .permitAll() + ) + .exceptionHandling( + exceptionHandler -> exceptionHandler.authenticationEntryPoint( + (request, response, authException) -> response.sendError( + HttpServletResponse.SC_UNAUTHORIZED, AuthErrorMessages.invalidToken())) + ) + .addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class) + .build(); } @Bean diff --git a/backend/src/main/java/com/waa/project/security/controller/AuthController.java b/backend/src/main/java/com/waa/project/security/controller/AuthController.java index 44d4c115..57fed8c7 100644 --- a/backend/src/main/java/com/waa/project/security/controller/AuthController.java +++ b/backend/src/main/java/com/waa/project/security/controller/AuthController.java @@ -5,21 +5,21 @@ import com.waa.project.security.contract.JwtTokenResponse; import com.waa.project.security.contract.RefreshTokenRequest; import com.waa.project.security.service.AuthService; +import com.waa.project.service.TokenBlacklistService; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api/v1/auth") public class AuthController { private final AuthService authService; + private final TokenBlacklistService tokenBlacklistService; public AuthController( - AuthService authService - ) { + AuthService authService, + TokenBlacklistService tokenBlacklistService) { this.authService = authService; + this.tokenBlacklistService = tokenBlacklistService; } @PostMapping("/login") @@ -31,4 +31,14 @@ public ResponseEntity login(@RequestBody AuthRequest authReque public ResponseEntity refreshToken(@RequestBody RefreshTokenRequest tokenRequest) { return ResponseEntity.ok(authService.refreshToken(tokenRequest)); } + + @PostMapping("/logout") + public ResponseEntity logout(@RequestHeader("Authorization") String token) { + + if (token != null && token.startsWith("Bearer ")) { + token = token.substring(7); + tokenBlacklistService.addTokenToBlacklist(token); + } + return ResponseEntity.ok("Logged out successfully"); + } } diff --git a/backend/src/main/java/com/waa/project/security/filter/JwtTokenFilter.java b/backend/src/main/java/com/waa/project/security/filter/JwtTokenFilter.java index 5c9a790f..65880811 100644 --- a/backend/src/main/java/com/waa/project/security/filter/JwtTokenFilter.java +++ b/backend/src/main/java/com/waa/project/security/filter/JwtTokenFilter.java @@ -2,10 +2,13 @@ import com.waa.project.security.util.AuthErrorMessages; import com.waa.project.security.util.JwtTokenUtil; +import com.waa.project.service.TokenBlacklistService; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; @@ -20,15 +23,20 @@ @Component public class JwtTokenFilter extends OncePerRequestFilter { - private final JwtTokenUtil jwtTokenUtil; + + private static final Logger logger = LoggerFactory.getLogger(JwtTokenFilter.class); + + private final JwtTokenUtil jwtTokenUtil; private final UserDetailsService userDetailsService; + private final TokenBlacklistService tokenBlacklistService; public JwtTokenFilter( JwtTokenUtil jwtTokenUtil, - UserDetailsService userDetailsService - ) { - this.jwtTokenUtil = jwtTokenUtil; + UserDetailsService userDetailsService, + TokenBlacklistService tokenBlacklistService) { + this.jwtTokenUtil = jwtTokenUtil; this.userDetailsService = userDetailsService; + this.tokenBlacklistService = tokenBlacklistService; } @Override @@ -36,10 +44,11 @@ protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain chain - ) throws ServletException, IOException { + ) throws ServletException, IOException { // Get authorization header and validate final String header = request.getHeader(HttpHeaders.AUTHORIZATION); + if (header == null || !header.startsWith("Bearer ")) { chain.doFilter(request, response); return; @@ -47,15 +56,21 @@ protected void doFilterInternal( // get the token String token = header.substring(7).trim().trim(); + if (!jwtTokenUtil.validateToken(token)) { - chain.doFilter(request, response); + logger.warn("Invalid JWT Token"); + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid JWT Token"); return; } - // Get user identity and set it on the spring security context + if (tokenBlacklistService.isTokenBlacklisted(token)) { + logger.warn("Blacklisted JWT Token"); + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Blacklisted JWT Token"); + return; + } + // Get user identity and set it on the spring security context UserDetails userDetails; - try { userDetails = userDetailsService.loadUserByUsername(jwtTokenUtil.getUsernameFromToken(token)); diff --git a/backend/src/main/java/com/waa/project/security/service/impl/AuthServiceImpl.java b/backend/src/main/java/com/waa/project/security/service/impl/AuthServiceImpl.java index d71e2f24..9bf16b81 100644 --- a/backend/src/main/java/com/waa/project/security/service/impl/AuthServiceImpl.java +++ b/backend/src/main/java/com/waa/project/security/service/impl/AuthServiceImpl.java @@ -19,17 +19,17 @@ @Service public class AuthServiceImpl implements AuthService { private final AuthenticationManager authenticationManager; - private final UserDetailsService userDetailsService; - private final JwtTokenUtil jwtTokenUtil; + private final UserDetailsService userDetailsService; + private final JwtTokenUtil jwtTokenUtil; public AuthServiceImpl( AuthenticationManager authenticationManager, UserDetailsService userDetailsService, JwtTokenUtil jwtTokenUtil - ) { + ) { this.authenticationManager = authenticationManager; - this.userDetailsService = userDetailsService; - this.jwtTokenUtil = jwtTokenUtil; + this.userDetailsService = userDetailsService; + this.jwtTokenUtil = jwtTokenUtil; } @Override @@ -45,9 +45,8 @@ public JwtTokenResponse login(AuthRequest request) { final UserDetails userDetails = userDetailsService.loadUserByUsername(result.getName()); - final String accessToken = jwtTokenUtil.generateToken(userDetails); + final String accessToken = jwtTokenUtil.generateToken(userDetails); final String refreshToken = jwtTokenUtil.generateRefreshToken(request.getUsername()); - return new JwtTokenResponse(accessToken, refreshToken); } diff --git a/backend/src/main/java/com/waa/project/service/AcademicResService.java b/backend/src/main/java/com/waa/project/service/AcademicResService.java index edc9a234..40c2fda0 100644 --- a/backend/src/main/java/com/waa/project/service/AcademicResService.java +++ b/backend/src/main/java/com/waa/project/service/AcademicResService.java @@ -1,21 +1,21 @@ package com.waa.project.service; -import com.waa.project.dto.AcademicResourceDto; -import com.waa.project.dto.AcademicResourceRequest; +import com.waa.project.dto.requests.AcademicResourceRequest; +import com.waa.project.dto.responses.AcademicResourceResponse; import org.springframework.web.multipart.MultipartFile; import java.util.List; public interface AcademicResService { - List getAllAcademicRes(); + List getAllAcademicRes(); - AcademicResourceDto getAcademicResource(Long feedId); + AcademicResourceResponse getAcademicResource(Long feedId); - String save(AcademicResourceRequest dto, MultipartFile file); + AcademicResourceResponse save(AcademicResourceRequest request, MultipartFile file); - String update(AcademicResourceDto dto, Long fid); + AcademicResourceResponse update(AcademicResourceRequest request, Long fid, MultipartFile file); - String delete(Long fid); + List delete(Long fid); - List searchByResourceName(String resname); + List searchByResourceName(String resname); } diff --git a/backend/src/main/java/com/waa/project/service/AcademicResourceTypeService.java b/backend/src/main/java/com/waa/project/service/AcademicResourceTypeService.java index 022324f9..b8ff1d20 100644 --- a/backend/src/main/java/com/waa/project/service/AcademicResourceTypeService.java +++ b/backend/src/main/java/com/waa/project/service/AcademicResourceTypeService.java @@ -1,6 +1,9 @@ package com.waa.project.service; import com.waa.project.dto.AcademicResourceTypeDto; +import com.waa.project.dto.requests.AcademicResourceTypeRequest; +import com.waa.project.dto.responses.AcademicResourceTypeResponse; +import com.waa.project.entity.AcademicResourceType; import java.util.List; @@ -9,9 +12,9 @@ public interface AcademicResourceTypeService { AcademicResourceTypeDto getAcademicResourceType(Long feedId); - String save(AcademicResourceTypeDto resType); + AcademicResourceTypeResponse save(AcademicResourceTypeRequest request); - String update(AcademicResourceTypeDto resType, Long fid); + AcademicResourceTypeResponse update(AcademicResourceTypeRequest resType, Long fid); - String delete(Long fid); + List delete(Long fid); } diff --git a/backend/src/main/java/com/waa/project/service/DiscussionCommentsService.java b/backend/src/main/java/com/waa/project/service/DiscussionCommentsService.java index fdf52104..a437a0c5 100644 --- a/backend/src/main/java/com/waa/project/service/DiscussionCommentsService.java +++ b/backend/src/main/java/com/waa/project/service/DiscussionCommentsService.java @@ -1,7 +1,6 @@ package com.waa.project.service; import com.waa.project.dto.DiscussionCommentsDto; -import com.waa.project.entity.Discussion; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.security.core.userdetails.User; @@ -17,6 +16,4 @@ public interface DiscussionCommentsService { DiscussionCommentsDto updateDiscussionComments(long id, DiscussionCommentsDto commentsDto, User user); DiscussionCommentsDto deleteDiscussionComments(long id, User user); - - void deleteAllCommentsByDiscussionId(Discussion discussion); } diff --git a/backend/src/main/java/com/waa/project/service/FeedbackCategoryService.java b/backend/src/main/java/com/waa/project/service/FeedbackCategoryService.java index 76e95c15..3266be98 100644 --- a/backend/src/main/java/com/waa/project/service/FeedbackCategoryService.java +++ b/backend/src/main/java/com/waa/project/service/FeedbackCategoryService.java @@ -1,6 +1,9 @@ package com.waa.project.service; import com.waa.project.dto.FeedbackCategoryDto; +import com.waa.project.dto.requests.FeedbackCategoryRequest; +import com.waa.project.dto.responses.FeedbackCategoryResponse; +import com.waa.project.entity.FeedbackCategory; import java.util.List; @@ -9,9 +12,9 @@ public interface FeedbackCategoryService { FeedbackCategoryDto getCategory(Long feedId); - String save(FeedbackCategoryDto category); + FeedbackCategoryResponse save(FeedbackCategoryRequest category); String update(FeedbackCategoryDto category, Long fid); - String delete(Long fid); + List delete(Long fid); } diff --git a/backend/src/main/java/com/waa/project/service/FeedbackService.java b/backend/src/main/java/com/waa/project/service/FeedbackService.java index c145c7c5..66898a1d 100644 --- a/backend/src/main/java/com/waa/project/service/FeedbackService.java +++ b/backend/src/main/java/com/waa/project/service/FeedbackService.java @@ -1,19 +1,22 @@ package com.waa.project.service; import com.waa.project.dto.FeedbackDto; +import com.waa.project.dto.requests.FeedbackRequest; +import com.waa.project.dto.responses.FeedbackResponse; +import com.waa.project.entity.Feedback; import java.util.List; public interface FeedbackService { - List getAllFeedbacks(); + List getAllFeedbacks(); FeedbackDto getFeedback(Long feedId); - String save(FeedbackDto feedback, Long userId); + FeedbackResponse save(FeedbackRequest feedbackRequest, Long userId); - String update(FeedbackDto feedback, Long fid, Long userId); + FeedbackResponse update(FeedbackRequest feedbackRequest, Long fid, Long userId); - String delete(Long fid); + List delete(Long fid); List findFeedbackByCategory(Long id); diff --git a/backend/src/main/java/com/waa/project/service/TokenBlacklistService.java b/backend/src/main/java/com/waa/project/service/TokenBlacklistService.java new file mode 100644 index 00000000..930fd668 --- /dev/null +++ b/backend/src/main/java/com/waa/project/service/TokenBlacklistService.java @@ -0,0 +1,7 @@ +package com.waa.project.service; + +public interface TokenBlacklistService { + void addTokenToBlacklist(String token); + + boolean isTokenBlacklisted(String token); +} diff --git a/backend/src/main/java/com/waa/project/service/impl/AcademicResServiceImp.java b/backend/src/main/java/com/waa/project/service/impl/AcademicResServiceImp.java index 4094619e..92268fca 100644 --- a/backend/src/main/java/com/waa/project/service/impl/AcademicResServiceImp.java +++ b/backend/src/main/java/com/waa/project/service/impl/AcademicResServiceImp.java @@ -1,7 +1,7 @@ package com.waa.project.service.impl; -import com.waa.project.dto.AcademicResourceDto; -import com.waa.project.dto.AcademicResourceRequest; +import com.waa.project.dto.requests.AcademicResourceRequest; +import com.waa.project.dto.responses.AcademicResourceResponse; import com.waa.project.entity.AcademicResource; import com.waa.project.entity.AcademicResourceType; import com.waa.project.repository.AcademicResRepository; @@ -33,62 +33,69 @@ public class AcademicResServiceImp implements AcademicResService { private FileService fileService; @Override - public List getAllAcademicRes() { - List list = new ArrayList<>(); - academicResRepository.findAll().forEach(feed -> list.add(modelMapper.map(feed, AcademicResourceDto.class))); + public List getAllAcademicRes() { + List list = new ArrayList<>(); + academicResRepository.findAll().forEach(feed -> list.add(modelMapper.map(feed, AcademicResourceResponse.class))); return list; + } @Override - public AcademicResourceDto getAcademicResource(Long feedId) { + public AcademicResourceResponse getAcademicResource(Long feedId) { AcademicResource res = academicResRepository.findById(feedId).get(); - return modelMapper.map(res, AcademicResourceDto.class); + return modelMapper.map(res, AcademicResourceResponse.class); } @Override - public String save(AcademicResourceRequest res, MultipartFile file) { - AcademicResource resToSave = modelMapper.map(res, AcademicResource.class); - + public AcademicResourceResponse save(AcademicResourceRequest res, MultipartFile file) { + AcademicResource dataToSave = modelMapper.map(res, AcademicResource.class); AcademicResourceType resCategory = academicResTypeRepository.findById(res.getResourceId()).get(); if (file != null && !file.isEmpty()) { String folder = resCategory.getName().replace(" ", "_"); String filePath = fileService.saveFile(file, FileSaveLocations.academicResource(folder)); - resToSave.setFile(filePath); + dataToSave.setFile(filePath); } - resToSave.setResourceType(resCategory); - academicResRepository.save(resToSave); - return "AcademicResource saved successfully."; + dataToSave.setResourceType(resCategory); + dataToSave.setName(res.getName()); + dataToSave.setBody(res.getBody()); + + academicResRepository.save(dataToSave); + + return modelMapper.map(dataToSave, AcademicResourceResponse.class); } + @Override - public String update(AcademicResourceDto res, Long fid) { - AcademicResource feedToUpdate = academicResRepository.findById(fid).orElse(null); + public AcademicResourceResponse update(AcademicResourceRequest res, Long fid, MultipartFile file) { + AcademicResource dataToUpdate = academicResRepository.findById(fid).orElse(null); AcademicResourceType academicResourceType = - academicResTypeRepository.findById(res.getResourceType().getId()).get(); + academicResTypeRepository.findById(res.getResourceId()).get(); if (academicResourceType != null) - feedToUpdate.setResourceType(academicResourceType); - - feedToUpdate.setName(res.getName()); + dataToUpdate.setResourceType(academicResourceType); - academicResRepository.save(feedToUpdate); - return "AcademicResource is updated."; + dataToUpdate.setName(res.getName()); + dataToUpdate.setBody(res.getBody()); + return modelMapper.map(academicResRepository.save(dataToUpdate), AcademicResourceResponse.class); } @Override - public String delete(Long fid) { + public List delete(Long fid) { academicResRepository.deleteById(fid); - return "AcademicResource is deleted."; + + List list = new ArrayList<>(); + academicResRepository.findAll().forEach(feed -> list.add(modelMapper.map(feed, AcademicResourceResponse.class))); + return list; } @Override - public List searchByResourceName(String resname) { - List result = new ArrayList<>(); + public List searchByResourceName(String resname) { + List result = new ArrayList<>(); List res = academicResRepository.findAcademicResourceByResourceTypeName(resname); - res.forEach(ar -> result.add(modelMapper.map(ar, AcademicResourceDto.class))); + res.forEach(ar -> result.add(modelMapper.map(ar, AcademicResourceResponse.class))); return result; } } diff --git a/backend/src/main/java/com/waa/project/service/impl/AcademicResourceTypeServiceImp.java b/backend/src/main/java/com/waa/project/service/impl/AcademicResourceTypeServiceImp.java index 4f8c2af6..de1cbf1a 100644 --- a/backend/src/main/java/com/waa/project/service/impl/AcademicResourceTypeServiceImp.java +++ b/backend/src/main/java/com/waa/project/service/impl/AcademicResourceTypeServiceImp.java @@ -1,6 +1,8 @@ package com.waa.project.service.impl; import com.waa.project.dto.AcademicResourceTypeDto; +import com.waa.project.dto.requests.AcademicResourceTypeRequest; +import com.waa.project.dto.responses.AcademicResourceTypeResponse; import com.waa.project.entity.AcademicResourceType; import com.waa.project.repository.AcademicResTypeRepository; import com.waa.project.service.AcademicResourceTypeService; @@ -36,25 +38,26 @@ public AcademicResourceTypeDto getAcademicResourceType(Long id) { } @Override - public String save(AcademicResourceTypeDto dto) { - academicResTypeRepository.save(modelMapper.map(dto, AcademicResourceType.class)); - return "AcademicResourceType saved successfully."; + public AcademicResourceTypeResponse save(AcademicResourceTypeRequest request) { + AcademicResourceType academicResourceType = modelMapper.map(request, AcademicResourceType.class); + academicResTypeRepository.save(academicResourceType); + return modelMapper.map(academicResourceType, AcademicResourceTypeResponse.class); } @Override - public String update(AcademicResourceTypeDto dto, Long fid) { + public AcademicResourceTypeResponse update(AcademicResourceTypeRequest request, Long fid) { AcademicResourceType dataToUpdate = academicResTypeRepository.findById(fid) - .orElseThrow( - () -> new NoSuchElementException( - "No feedback was found.")); - dataToUpdate.setName(dto.getName()); + .orElseThrow( + () -> new NoSuchElementException( + "No feedback was found.")); + dataToUpdate.setName(request.getName()); academicResTypeRepository.save(dataToUpdate); - return "AcademicResourceType is updated."; + return modelMapper.map(academicResTypeRepository.findAll(), AcademicResourceTypeResponse.class); } @Override - public String delete(Long fid) { + public List delete(Long fid) { academicResTypeRepository.deleteById(fid); - return "AcademicResourceType is deleted."; + return academicResTypeRepository.findAll(); } } diff --git a/backend/src/main/java/com/waa/project/service/impl/FeebackCategoryServiceImp.java b/backend/src/main/java/com/waa/project/service/impl/FeebackCategoryServiceImp.java index af1be3b4..959a01bc 100644 --- a/backend/src/main/java/com/waa/project/service/impl/FeebackCategoryServiceImp.java +++ b/backend/src/main/java/com/waa/project/service/impl/FeebackCategoryServiceImp.java @@ -1,7 +1,10 @@ package com.waa.project.service.impl; import com.waa.project.dto.FeedbackCategoryDto; +import com.waa.project.dto.requests.FeedbackCategoryRequest; +import com.waa.project.dto.responses.FeedbackCategoryResponse; import com.waa.project.entity.FeedbackCategory; +import com.waa.project.exception.ResourceNotFoundException; import com.waa.project.repository.FeedbackCategoryRepository; import com.waa.project.service.FeedbackCategoryService; import org.modelmapper.ModelMapper; @@ -32,22 +35,26 @@ public List getAllCategories() { @Override public FeedbackCategoryDto getCategory(Long feedId) { - + feedbackCategoryRepository.findById(feedId).orElseThrow( + () -> new ResourceNotFoundException("Feedback category not found with id: " + feedId) + ); return modelMapper.map(feedbackCategoryRepository.findById(feedId).get(), FeedbackCategoryDto.class); } @Override - public String save(FeedbackCategoryDto feedback) { - feedbackCategoryRepository.save(modelMapper.map(feedback, FeedbackCategory.class)); - return "FeedbackCategory saved successfully."; + public FeedbackCategoryResponse save(FeedbackCategoryRequest category) { + FeedbackCategory feedbackCategory = modelMapper.map(category, FeedbackCategory.class); + feedbackCategoryRepository.save(feedbackCategory); + + return modelMapper.map(feedbackCategory, FeedbackCategoryResponse.class); } @Override public String update(FeedbackCategoryDto feedback, Long fid) { FeedbackCategory feedToUpdate = feedbackCategoryRepository.findById(fid) - .orElseThrow( - () -> new NoSuchElementException( - "No feedback was found.")); + .orElseThrow( + () -> new NoSuchElementException( + "No feedback was found.")); feedToUpdate.setName(feedback.getName()); feedToUpdate.setDescription(feedback.getDescription()); feedbackCategoryRepository.save(feedToUpdate); @@ -55,8 +62,11 @@ public String update(FeedbackCategoryDto feedback, Long fid) { } @Override - public String delete(Long fid) { + public List delete(Long fid) { + feedbackCategoryRepository.findById(fid).orElseThrow( + () -> new ResourceNotFoundException("Feedback category not found with id: " + fid) + ); feedbackCategoryRepository.deleteById(fid); - return "FeedbackCategory is deleted."; + return feedbackCategoryRepository.findAll(); } } diff --git a/backend/src/main/java/com/waa/project/service/impl/FeebackServiceImp.java b/backend/src/main/java/com/waa/project/service/impl/FeebackServiceImp.java index b977f16f..be6c2f4c 100644 --- a/backend/src/main/java/com/waa/project/service/impl/FeebackServiceImp.java +++ b/backend/src/main/java/com/waa/project/service/impl/FeebackServiceImp.java @@ -1,6 +1,8 @@ package com.waa.project.service.impl; import com.waa.project.dto.FeedbackDto; +import com.waa.project.dto.requests.FeedbackRequest; +import com.waa.project.dto.responses.FeedbackResponse; import com.waa.project.entity.Feedback; import com.waa.project.entity.FeedbackCategory; import com.waa.project.entity.Student; @@ -36,10 +38,8 @@ public class FeebackServiceImp implements FeedbackService { private UserRepository userRepository; @Override - public List getAllFeedbacks() { - List list = new ArrayList<>(); - feedbackRepository.findAll().forEach(feed -> list.add(modelMapper.map(feed, FeedbackDto.class))); - return list; + public List getAllFeedbacks() { + return feedbackRepository.findAll(); } @Override @@ -49,67 +49,56 @@ public FeedbackDto getFeedback(Long feedId) { } @Override - public String save(FeedbackDto dto, Long userId) { - Feedback feedbackToSave = modelMapper.map(dto, Feedback.class); - + public FeedbackResponse save(FeedbackRequest feedbackRequest, Long userId) { + Feedback feedbackToSave = modelMapper.map(feedbackRequest, Feedback.class); FeedbackCategory feedbackCategory = - feedbackCategoryRepository.findById(dto.getFeedbackCategory().getId()).get(); + feedbackCategoryRepository.findById(feedbackRequest.getFeedbackCategory()).get(); feedbackToSave.setCategory(feedbackCategory); if (userId != null) { User user = userRepository.findById(userId).get(); - if (!user.getRoleType().equals("ADMIN")) { + if (user.getRoleType().equals("STUDENT")) { Student student = studentRepository.findById(userId).get(); feedbackToSave.setStudent(student); } } - feedbackRepository.save(feedbackToSave); - return "Feedback saved successfully."; + + return modelMapper.map(feedbackToSave, FeedbackResponse.class); } @Override - public String update(FeedbackDto feedback, Long fid, Long userId) { + public FeedbackResponse update(FeedbackRequest feedbackRequest, Long fid, Long userId) { Feedback feedToUpdate = feedbackRepository.findById(fid).orElse(null); - if (feedToUpdate == null) { - return "Feedback not found."; - } - if (userId != null) { - User user = userRepository.findById(userId).orElse(null); - if (user == null) { - return "Anonymous Feedback cannot be updated."; - } - - var result = feedToUpdate.getStudent(); - if (result != null) { - System.out.println("role ===" + user.getRoleType()); - System.out.println("stud Id ===" + result.getId() + " ===" + userId); - Long studentId = result.getId(); - - if (userId.equals(studentId)) { - Student student = studentRepository.findById(userId).orElse(null); - if (student == null) { - return "Student not found."; - } - - feedToUpdate.setStudent(student); - feedToUpdate.setTitle(feedback.getTitle()); - feedToUpdate.setBody(feedback.getBody()); - feedbackRepository.save(feedToUpdate); - return "Feedback is updated."; - } - } else { - return "Anonymous Feedback cannot be updated."; - } - } - return "You cannot update other's Feedback."; + modelMapper.map(feedbackRequest, feedToUpdate); + System.out.println("feedToUpdate=" + feedToUpdate); +// if (userId != null) { +// User user = userRepository.findById(userId).orElse(null); +// var result = feedToUpdate.getStudent(); +// if (result != null) { +// Long studentId = result.getId(); +// if (userId.equals(studentId)) { +// Student student = studentRepository.findById(userId).orElse(null); +// feedToUpdate.setStudent(student); +// } +// } +// } + FeedbackCategory feedbackCategory = feedbackCategoryRepository.findById(feedbackRequest.getFeedbackCategory()).get(); + System.out.println(feedbackCategory); + + feedToUpdate.setCategory(feedbackCategory); + feedToUpdate.setTitle(feedbackRequest.getTitle()); + feedToUpdate.setBody(feedbackRequest.getBody()); + feedbackRepository.save(feedToUpdate); + + return modelMapper.map(feedToUpdate, FeedbackResponse.class); } @Override - public String delete(Long fid) { + public List delete(Long fid) { feedbackRepository.deleteById(fid); - return "Feedback is deleted."; + return feedbackRepository.findAll(); } @Override diff --git a/backend/src/main/java/com/waa/project/service/impl/SubCommentServiceImpl.java b/backend/src/main/java/com/waa/project/service/impl/SubCommentServiceImpl.java index a4514f40..c0c4b741 100644 --- a/backend/src/main/java/com/waa/project/service/impl/SubCommentServiceImpl.java +++ b/backend/src/main/java/com/waa/project/service/impl/SubCommentServiceImpl.java @@ -2,7 +2,6 @@ import com.waa.project.dto.DiscussionCommentsDto; import com.waa.project.entity.DiscussionComments; -import com.waa.project.exception.ResourceNotFoundException; import com.waa.project.repository.DiscussionCommentsRepository; import com.waa.project.repository.DiscussionRepository; import com.waa.project.security.contract.AuthUserResponse; @@ -43,8 +42,8 @@ public DiscussionCommentsDto createSubDiscussionComments(DiscussionCommentsDto c requestData.setStudent(getUserId(user)); DiscussionComments parentComment = repository.findById(commentsDto.getParentCommentIdId()) - .orElseThrow(() -> new ResourceNotFoundException( - "Cannot Searchable for that Comment!")); + .orElseThrow(() -> new RuntimeException( + "Comment ID not found")); requestData.setParentCommentId(parentComment); @@ -61,8 +60,8 @@ public DiscussionCommentsDto updateSubDiscussionComments(long id, DiscussionComm requestData.setId(id); DiscussionComments parentComment = repository.findByIdAndStudentId(id, userId) - .orElseThrow(() -> new ResourceNotFoundException( - "Cannot Searchable for that Comment!")); + .orElseThrow(() -> new RuntimeException( + "Comment ID not found")); requestData.setParentCommentId(parentComment.getParentCommentId()); DiscussionComments responseData = repository.save(requestData); @@ -76,8 +75,7 @@ public DiscussionCommentsDto deleteSubDiscussionComments(long id, User user) { Long userId = getUserId(user); DiscussionComments dataById = repository.findByIdAndStudentId(id, userId) - .orElseThrow(() -> new ResourceNotFoundException( - "Cannot Searchable for that Comment!")); + .orElseThrow(() -> new RuntimeException("Comment ID not found")); repository.deleteByIdAndStudentId(id, userId); diff --git a/backend/src/main/java/com/waa/project/service/impl/TokenBlacklistServiceImp.java b/backend/src/main/java/com/waa/project/service/impl/TokenBlacklistServiceImp.java new file mode 100644 index 00000000..a36e3c2c --- /dev/null +++ b/backend/src/main/java/com/waa/project/service/impl/TokenBlacklistServiceImp.java @@ -0,0 +1,18 @@ +package com.waa.project.service.impl; + +import com.waa.project.service.TokenBlacklistService; +import org.springframework.stereotype.Service; + +@Service +public class TokenBlacklistServiceImp implements TokenBlacklistService { + + @Override + public void addTokenToBlacklist(String token) { + + } + + @Override + public boolean isTokenBlacklisted(String token) { + return false; + } +} diff --git a/backend/src/main/resources/application.yml b/backend/src/main/resources/application.yml index 7224dd36..f1059aea 100644 --- a/backend/src/main/resources/application.yml +++ b/backend/src/main/resources/application.yml @@ -3,11 +3,11 @@ spring: name: project jwt: secret: secretsecretsecretsecretsecretsss - expiration: 3600 - refreshExpiration: 36000 + expiration: 360000 + refreshExpiration: 3600000 datasource: - url: jdbc:mysql://localhost:3306/waa-demo + url: jdbc:mysql://localhost:3306/waa_student_connect username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver @@ -16,7 +16,7 @@ spring: properties: hibernate: dialect: org.hibernate.dialect.MySQLDialect - format_sql: true + # format_sql: true hibernate: ddl-auto: create-drop show-sql: true @@ -35,3 +35,7 @@ spring: web: resources: static-locations: file:${user.dir} + + spring: + mvc: + static-path-pattern: /swagger-ui/** \ No newline at end of file diff --git a/backend/src/test/java/com/waa/project/ProjectApplicationTests.java b/backend/src/test/java/com/waa/project/ProjectApplicationTests.java index 28bb549b..d92d23e7 100644 --- a/backend/src/test/java/com/waa/project/ProjectApplicationTests.java +++ b/backend/src/test/java/com/waa/project/ProjectApplicationTests.java @@ -6,8 +6,9 @@ @SpringBootTest class ProjectApplicationTests { - @Test - void contextLoads() { - } + @Test + void contextLoads() { + + } } diff --git a/frontend/public/index.html b/frontend/public/index.html index 524664d0..ff597c5b 100644 --- a/frontend/public/index.html +++ b/frontend/public/index.html @@ -7,6 +7,8 @@ + +