Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public class WorkplaceReviewController {

private User currentUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null || !auth.isAuthenticated()) throw new AccessDeniedException("Unauthenticated");
if (auth == null || !auth.isAuthenticated())
throw new AccessDeniedException("Unauthenticated");
Object principal = auth.getPrincipal();
if (principal instanceof User u) return u;
if (principal instanceof User u)
return u;
if (principal instanceof UserDetails ud) {
String key = ud.getUsername();
return userRepository.findByEmail(key).or(() -> userRepository.findByUsername(key))
Expand All @@ -39,44 +41,52 @@ private User currentUser() {
// === REVIEWS ===
@PostMapping("/review")
public ResponseEntity<ReviewResponse> create(@PathVariable Long workplaceId,
@RequestBody @Valid ReviewCreateRequest req) {
@RequestBody @Valid ReviewCreateRequest req) {
var res = reviewService.createReview(workplaceId, req, currentUser());
return ResponseEntity.ok(res);
}

@GetMapping("/review")
public ResponseEntity<PaginatedResponse<ReviewResponse>> list(@PathVariable Long workplaceId,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(required = false) String ratingFilter,
@RequestParam(required = false) String sortBy,
@RequestParam(required = false) Boolean hasComment,
@RequestParam(required = false) String policy,
@RequestParam(required = false) Integer policyMin) {
var res = reviewService.listReviews(workplaceId, page, size, ratingFilter, sortBy, hasComment, policy, policyMin);
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(required = false) String ratingFilter,
@RequestParam(required = false) String sortBy,
@RequestParam(required = false) Boolean hasComment,
@RequestParam(required = false) String policy,
@RequestParam(required = false) Integer policyMin) {
var res = reviewService.listReviews(workplaceId, page, size, ratingFilter, sortBy, hasComment, policy,
policyMin, currentUser());
return ResponseEntity.ok(res);
}

@GetMapping("/review/{reviewId}")
public ResponseEntity<ReviewResponse> getOne(@PathVariable Long workplaceId,
@PathVariable Long reviewId) {
return ResponseEntity.ok(reviewService.getOne(workplaceId, reviewId));
@PathVariable Long reviewId) {
return ResponseEntity.ok(reviewService.getOne(workplaceId, reviewId, currentUser()));
}

@PutMapping("/review/{reviewId}")
public ResponseEntity<ReviewResponse> update(@PathVariable Long workplaceId,
@PathVariable Long reviewId,
@RequestBody @Valid ReviewUpdateRequest req) {
@PathVariable Long reviewId,
@RequestBody @Valid ReviewUpdateRequest req) {
var res = reviewService.updateReview(workplaceId, reviewId, req, currentUser());
return ResponseEntity.ok(res);
}

@DeleteMapping("/review/{reviewId}")
public ResponseEntity<ApiMessage> delete(@PathVariable Long workplaceId,
@PathVariable Long reviewId) {
@PathVariable Long reviewId) {
boolean isAdmin = false; // TODO: implement isAdmin check in milestone 3
reviewService.deleteReview(workplaceId, reviewId, currentUser(), isAdmin);
return ResponseEntity.ok(ApiMessage.builder().message("Review deleted").code("REVIEW_DELETED").build());
}

@PostMapping("/review/{reviewId}/helpful")
public ResponseEntity<ReviewResponse> toggleHelpful(@PathVariable Long workplaceId,
@PathVariable Long reviewId) {
var res = reviewService.toggleHelpful(workplaceId, reviewId, currentUser());
return ResponseEntity.ok(res);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ReviewResponse {
private Double overallRating;
private Map<String, Integer> ethicalPolicyRatings;
private ReplyResponse reply;
private boolean isHelpfulByUser;
private Instant createdAt;
private Instant updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.bounswe.jobboardbackend.workplace.model;

import jakarta.persistence.*;
import lombok.*;
import org.bounswe.jobboardbackend.auth.model.User;
import org.hibernate.annotations.CreationTimestamp;

import java.time.Instant;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(uniqueConstraints = {
@UniqueConstraint(columnNames = { "review_id", "user_id" })
})
public class ReviewReaction {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "review_id")
private Review review;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id")
private User user;

@CreationTimestamp
@Column(nullable = false, updatable = false)
private Instant createdAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.bounswe.jobboardbackend.workplace.repository;

import org.bounswe.jobboardbackend.workplace.model.ReviewReaction;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface ReviewReactionRepository extends JpaRepository<ReviewReaction, Long> {
boolean existsByReview_IdAndUser_Id(Long reviewId, Long userId);

Optional<ReviewReaction> findByReview_IdAndUser_Id(Long reviewId, Long userId);

List<ReviewReaction> findByUser_IdAndReview_IdIn(Long userId, List<Long> reviewIds);
}
Loading