-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
498 additions
and
40 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/base/entity/BaseMedia.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,45 @@ | ||
package com.lcaohoanq.shoppe.base.entity; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.MappedSuperclass; | ||
import jakarta.persistence.PrePersist; | ||
import jakarta.persistence.PreUpdate; | ||
import java.time.LocalDateTime; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@MappedSuperclass //avoid direct persistence of this class, any entity that extends this class will have these fields | ||
@SuperBuilder | ||
public class BaseMedia { | ||
|
||
@Column(name="created_at") | ||
@JsonProperty("created_at") | ||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS") | ||
private LocalDateTime createdAt; | ||
|
||
@Column(name="updated_at") | ||
@JsonProperty("updated_at") | ||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS") | ||
private LocalDateTime updatedAt; | ||
|
||
@PrePersist | ||
protected void onCreate() { | ||
createdAt = LocalDateTime.now(); | ||
updatedAt = LocalDateTime.now(); | ||
} | ||
|
||
@PreUpdate | ||
protected void onUpdate() { | ||
updatedAt = LocalDateTime.now(); | ||
} | ||
|
||
} |
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
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
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
14 changes: 14 additions & 0 deletions
14
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/review/IReviewService.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,14 @@ | ||
package com.lcaohoanq.shoppe.domain.review; | ||
|
||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface IReviewService { | ||
ReviewResponse create(ReviewDTO reviewDTO); | ||
List<ReviewResponse> getAll(); | ||
ReviewResponse update(long id, ReviewDTO reviewDTO); | ||
void delete (long id); | ||
Optional<ReviewResponse> getById(long id); | ||
Optional<ReviewResponse> getByOrderId(long orderId); | ||
} |
71 changes: 71 additions & 0 deletions
71
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/review/Review.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,71 @@ | ||
package com.lcaohoanq.shoppe.domain.review; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.lcaohoanq.shoppe.base.entity.BaseEntity; | ||
import com.lcaohoanq.shoppe.domain.order.Order; | ||
import com.lcaohoanq.shoppe.domain.user.User; | ||
import com.lcaohoanq.shoppe.metadata.MediaMeta; | ||
import jakarta.persistence.AttributeOverride; | ||
import jakarta.persistence.AttributeOverrides; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embedded; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.SequenceGenerator; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Entity | ||
@Table(name = "reviews") | ||
@SuperBuilder | ||
@AttributeOverrides({ | ||
@AttributeOverride(name = "createdAt", column = @Column(name = "review_created_at")), | ||
@AttributeOverride(name = "updatedAt", column = @Column(name = "review_updated_at")) | ||
}) | ||
public class Review extends BaseEntity { | ||
|
||
@Id | ||
@SequenceGenerator(name = "reviews_seq", sequenceName = "reviews_id_seq", allocationSize = 1) | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "reviews_seq") | ||
@Column(name="id", unique=true, nullable=false) | ||
@JsonProperty("id") | ||
private Long id; | ||
|
||
@Column(name = "content") | ||
private String content; | ||
|
||
@Column(name = "rating") | ||
private Integer rating; | ||
|
||
@Column(name = "is_hidden", columnDefinition = "boolean default false") | ||
private boolean isHidden; //hide username | ||
|
||
@Column(name = "product_quality") | ||
private String productQuality; | ||
|
||
@Column(name = "match_description") | ||
private String matchDescription; | ||
|
||
@Embedded | ||
private MediaMeta mediaMeta; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private User user; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "order_id", nullable = false) | ||
private Order order; | ||
} |
11 changes: 11 additions & 0 deletions
11
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/review/ReviewDTO.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,11 @@ | ||
package com.lcaohoanq.shoppe.domain.review; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record ReviewDTO( | ||
@JsonProperty("content") String content, | ||
@JsonProperty("rating") int rating, | ||
@JsonProperty("user_id") @NotNull long userId, | ||
@JsonProperty("order_id") @NotNull long orderId) | ||
{ } |
76 changes: 76 additions & 0 deletions
76
.../springboot/src/main/java/com/lcaohoanq/shoppe/domain/review/ReviewGraphQLController.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,76 @@ | ||
package com.lcaohoanq.shoppe.domain.review; | ||
|
||
import com.lcaohoanq.shoppe.base.exception.DataNotFoundException; | ||
import com.lcaohoanq.shoppe.component.LocalizationUtils; | ||
import com.lcaohoanq.shoppe.domain.order.IOrderService; | ||
import com.lcaohoanq.shoppe.domain.order.OrderResponse; | ||
import com.lcaohoanq.shoppe.enums.OrderStatus; | ||
import com.lcaohoanq.shoppe.mapper.ReviewMapper; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.graphql.data.method.annotation.Argument; | ||
import org.springframework.graphql.data.method.annotation.MutationMapping; | ||
import org.springframework.graphql.data.method.annotation.QueryMapping; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.stereotype.Controller; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
public class ReviewGraphQLController { | ||
|
||
private final IReviewService reviewService; | ||
private final LocalizationUtils localizationUtils; | ||
private final IOrderService orderService; | ||
private final ReviewMapper reviewMapper; | ||
|
||
@QueryMapping | ||
@PreAuthorize("permitAll()") | ||
public List<ReviewResponse> getAllFeedbacks() { | ||
return reviewService.getAll(); | ||
} | ||
|
||
@QueryMapping | ||
public ReviewResponse getFeedbackById(@Argument Long id) { | ||
return reviewService | ||
.getById(id) | ||
.orElseThrow(() -> new DataNotFoundException("Feedback not found: " + id)); | ||
} | ||
|
||
@QueryMapping | ||
public ReviewResponse getFeedbackByOrderId(@Argument Long orderId) { | ||
return reviewService | ||
.getByOrderId(orderId) | ||
.orElseThrow(() -> new DataNotFoundException("Feedback not found for order: " + orderId)); | ||
} | ||
|
||
@MutationMapping | ||
public ReviewResponse createFeedback(@Argument ReviewDTO review) { | ||
// Check if the order is in DELIVERED status | ||
OrderResponse order = orderService.getById(review.orderId()); | ||
if (order.getStatus() != OrderStatus.DELIVERED) { | ||
throw new IllegalStateException("Feedback can only be submitted for delivered orders."); | ||
} | ||
|
||
// Check if review already exists for this order | ||
if (reviewService.getByOrderId(review.orderId()).isPresent()) { | ||
throw new IllegalStateException("Feedback has already been submitted for this order."); | ||
} | ||
|
||
return reviewService.create(review); | ||
} | ||
|
||
@MutationMapping | ||
public ReviewResponse updateFeedback(@Argument Long id, @Argument ReviewDTO review) { | ||
return reviewService.update(id, review); | ||
} | ||
|
||
@MutationMapping | ||
public Boolean deleteFeedback(@Argument Long id) { | ||
try { | ||
reviewService.delete(id); | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/review/ReviewRepository.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,11 @@ | ||
package com.lcaohoanq.shoppe.domain.review; | ||
|
||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ReviewRepository extends JpaRepository<Review, Long> { | ||
|
||
Boolean existsByUserIdAndOrderId(Long userId, Long orderId); | ||
Optional<Review> findByOrderId(Long orderId); | ||
|
||
} |
Oops, something went wrong.