Feedback remarks added

This commit is contained in:
Sidharth Prabhu
2026-04-18 22:48:22 +05:30
parent c36b4e4de0
commit 665ef4e3de
7 changed files with 3619 additions and 36 deletions

3447
backend/my_startup.log Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -4,10 +4,12 @@ import com.rit.canteen.sales.model.Feedback;
import com.rit.canteen.sales.model.ItemRating;
import com.rit.canteen.sales.model.Order;
import com.rit.canteen.sales.repository.FeedbackRepository;
import com.rit.canteen.sales.repository.ItemRatingRepository;
import com.rit.canteen.sales.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
@@ -24,6 +26,9 @@ public class FeedbackController {
@Autowired
private OrderRepository orderRepository;
@Autowired
private ItemRatingRepository itemRatingRepository;
@GetMapping
public Page<Feedback> getAllFeedback(
@RequestParam(defaultValue = "0") int page,
@@ -32,29 +37,66 @@ public class FeedbackController {
}
@GetMapping("/item-details")
@Transactional(readOnly = true)
public Page<Map<String, Object>> getItemDetails(
@RequestParam String productName,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
Page<ItemRating> ratings = feedbackRepository.findByProductName(productName, PageRequest.of(page, size));
String trimmedName = productName != null ? productName.trim() : "";
// Sort by feedback.createdAt DESC
Sort sort = Sort.by(Sort.Direction.DESC, "feedback.createdAt");
Page<ItemRating> ratings = itemRatingRepository.findByProductNameIgnoreCase(trimmedName, PageRequest.of(page, size, sort));
return ratings.map(r -> {
Map<String, Object> map = new HashMap<>();
map.put("rating", r.getRating());
// Priority: Item-specific comment -> parent Feedback comment -> empty
String comment = r.getComment();
Feedback f = r.getFeedback();
if (comment == null || comment.isBlank()) {
if (r.getFeedback() != null) {
comment = r.getFeedback().getComment();
if (f != null) {
comment = f.getComment();
}
}
map.put("comment", comment != null ? comment : "");
map.put("date", r.getFeedback() != null ? r.getFeedback().getCreatedAt() : java.time.LocalDateTime.now());
map.put("date", f != null ? f.getCreatedAt() : java.time.LocalDateTime.now());
map.put("userName", f != null ? f.getUserName() : "Anonymous");
map.put("orderNumber", (f != null && f.getOrder() != null) ? f.getOrder().getOrderNumber() : "N/A");
return map;
});
}
@GetMapping("/item-stats")
public Map<String, Object> getItemStats(@RequestParam String productName) {
Map<String, Object> stats = new HashMap<>();
List<Object[]> distribution = feedbackRepository.getItemRatingDistribution(productName);
List<Map<String, Object>> distList = new ArrayList<>();
long totalCount = 0;
double sum = 0;
for (Object[] row : distribution) {
Map<String, Object> item = new HashMap<>();
int rating = ((Number) row[0]).intValue();
long count = ((Number) row[1]).longValue();
item.put("rating", rating);
item.put("count", count);
distList.add(item);
totalCount += count;
sum += (rating * count);
}
stats.put("distribution", distList);
stats.put("totalReviews", totalCount);
stats.put("averageRating", totalCount > 0 ? sum / totalCount : 0.0);
return stats;
}
@GetMapping("/stats")
public Map<String, Object> getFeedbackStats() {
Map<String, Object> stats = new HashMap<>();
@@ -66,8 +108,8 @@ public class FeedbackController {
List<Map<String, Object>> distList = new ArrayList<>();
for (Object[] row : distribution) {
Map<String, Object> item = new HashMap<>();
item.put("rating", row[0]);
item.put("count", row[1]);
item.put("rating", ((Number) row[0]).intValue());
item.put("count", ((Number) row[1]).longValue());
distList.add(item);
}
stats.put("distribution", distList);
@@ -77,8 +119,8 @@ public class FeedbackController {
for (Object[] row : topRated) {
Map<String, Object> item = new HashMap<>();
item.put("name", row[0]);
item.put("average", row[1]);
item.put("count", row[2]);
item.put("average", row[1] != null ? ((Number) row[1]).doubleValue() : 0.0);
item.put("count", row[2] != null ? ((Number) row[2]).longValue() : 0);
topList.add(item);
}
stats.put("ratedItems", topList);

View File

@@ -1,6 +1,7 @@
package com.rit.canteen.sales.repository;
import com.rit.canteen.sales.model.Feedback;
import com.rit.canteen.sales.model.ItemRating;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@@ -25,6 +26,6 @@ public interface FeedbackRepository extends JpaRepository<Feedback, Long> {
@Query("SELECT ir.productName, AVG(ir.rating), COUNT(ir) FROM ItemRating ir GROUP BY ir.productName ORDER BY AVG(ir.rating) DESC")
List<Object[]> getTopRatedItems();
@Query("SELECT ir FROM ItemRating ir WHERE TRIM(LOWER(ir.productName)) = TRIM(LOWER(:productName)) ORDER BY ir.feedback.createdAt DESC")
Page<com.rit.canteen.sales.model.ItemRating> findByProductName(@org.springframework.data.repository.query.Param("productName") String productName, Pageable pageable);
@Query("SELECT ir.rating, COUNT(ir) FROM ItemRating ir WHERE TRIM(LOWER(ir.productName)) = TRIM(LOWER(:productName)) GROUP BY ir.rating")
List<Object[]> getItemRatingDistribution(@org.springframework.data.repository.query.Param("productName") String productName);
}

View File

@@ -0,0 +1,16 @@
package com.rit.canteen.sales.repository;
import com.rit.canteen.sales.model.ItemRating;
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.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface ItemRatingRepository extends JpaRepository<ItemRating, Long> {
@Query(value = "SELECT ir FROM ItemRating ir LEFT JOIN FETCH ir.feedback WHERE TRIM(LOWER(ir.productName)) = TRIM(LOWER(:productName))",
countQuery = "SELECT COUNT(ir) FROM ItemRating ir WHERE TRIM(LOWER(ir.productName)) = TRIM(LOWER(:productName))")
Page<ItemRating> findByProductNameIgnoreCase(String productName, Pageable pageable);
}

View File

@@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -17,6 +18,15 @@ public class OrderArchiverService {
@Autowired
private OrderRepository orderRepository;
/**
* Ensures all old orders are archived immediately upon application startup.
*/
@PostConstruct
public void init() {
System.out.println("Application startup: Triggering proactive order archival...");
archivePreviousDayOrders();
}
/**
* Automatically archives orders from previous days at midnight (12:00 AM).
* Cron: 0 0 0 * * * (Second Minute Hour Day Month DayOfWeek)