Review fix Model

This commit is contained in:
Tim
2025-06-05 14:54:30 +02:00
parent 07d8446cd0
commit a2aa0bfa05
6 changed files with 50 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
package de.htwsaar.webshop.controller;
import de.htwsaar.webshop.model.ReviewModel;
import de.htwsaar.webshop.repository.entities.Review;
import de.htwsaar.webshop.service.ArticleService;
import de.htwsaar.webshop.service.ReviewService;
@@ -30,10 +31,10 @@ public class ReviewController {
}
@RequestMapping(path = REVIEW_GET_ALL, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<List<Review>> getAll(HttpServletRequest request,
@RequestParam(value = PARAM_UUID) UUID uuid) {
public ResponseEntity<List<ReviewModel>> getAll(HttpServletRequest request,
@RequestParam(value = PARAM_UUID) UUID uuid) {
logRequest(request);
List<Review> review = reviewService.getAllByUUID(uuid);
List<ReviewModel> review = reviewService.getAllByUUID(uuid).stream().map(reviewService::toModel).toList();
if(review.isEmpty()) {
return ResponseEntity.noContent().build();
}

View File

@@ -0,0 +1,27 @@
package de.htwsaar.webshop.model;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Null;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import static de.htwsaar.webshop.util.TimeUtil.VALID_MIN_MILLIS_TIMESTAMP;
@Getter
@Setter
@ToString
@AllArgsConstructor
public class ReviewModel {
@Min(VALID_MIN_MILLIS_TIMESTAMP)
long timestamp;
@Min(0)
@Max(10)
int rating;
@Null
String content;
}

View File

@@ -1,13 +1,17 @@
package de.htwsaar.webshop.repository.entities;
import de.htwsaar.webshop.util.TimeUtil;
import jakarta.persistence.*;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import static de.htwsaar.webshop.util.TimeUtil.VALID_MIN_MILLIS_TIMESTAMP;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@@ -31,4 +35,8 @@ public class Review {
@Max(10)
@Column(name = "rating", nullable = false)
private Integer rating;
@Min(VALID_MIN_MILLIS_TIMESTAMP)
@Column(name = "timestamp", nullable = false)
private Long timestamp;
}

View File

@@ -3,6 +3,7 @@ package de.htwsaar.webshop.service;
import java.util.List;
import java.util.UUID;
import de.htwsaar.webshop.model.ReviewModel;
import de.htwsaar.webshop.repository.entities.Review;
public interface ReviewService {
@@ -11,4 +12,5 @@ public interface ReviewService {
void delete(Long reviewId);
Review getReviewById(Long id);
List<Review> getAllByUUID(UUID uuid);
ReviewModel toModel(Review review);
}

View File

@@ -1,5 +1,6 @@
package de.htwsaar.webshop.service.impl;
import de.htwsaar.webshop.model.ReviewModel;
import de.htwsaar.webshop.repository.ReviewRepository;
import de.htwsaar.webshop.repository.entities.Review;
import de.htwsaar.webshop.service.ArticleService;
@@ -25,6 +26,7 @@ public class ReviewServiceImpl implements ReviewService {
@Override
public Review save(Review review) {
review.setTimestamp(System.currentTimeMillis());
return reviewRepository.save(review);
}
@@ -33,7 +35,7 @@ public class ReviewServiceImpl implements ReviewService {
if(articleUuid == null) {
return null;
}
Review review = new Review(null, content, articleService.findByUUID(articleUuid), rating);
Review review = new Review(null, content, articleService.findByUUID(articleUuid), rating, System.currentTimeMillis());
return reviewRepository.save(review);
}
@@ -51,4 +53,9 @@ public class ReviewServiceImpl implements ReviewService {
public List<Review> getAllByUUID(UUID uuid) {
return reviewRepository.getReviewsByArticle_Uuid(uuid);
}
@Override
public ReviewModel toModel(Review review) {
return new ReviewModel(review.getTimestamp(), review.getRating(), review.getContent());
}
}

View File

@@ -28,12 +28,11 @@ CREATE TABLE IF NOT EXISTS Reviews
article_id INTEGER NOT NULL,
rating INTEGER NOT NULL,
content TEXT NULL,
timestamp INTEGER NOT NULL,
FOREIGN KEY (article_id) REFERENCES Articles (id),
CONSTRAINT c_rating CHECK ( rating >= 0 AND rating <= 10)
);
CREATE TABLE IF NOT EXISTS Customers
(
id INTEGER PRIMARY KEY NOT NULL,