72 lines
1.8 KiB
Java
72 lines
1.8 KiB
Java
package com.drinkool.services;
|
|
|
|
import com.drinkool.InternalValue;
|
|
import com.drinkool.Role;
|
|
import com.drinkool.Url;
|
|
import com.drinkool.dtos.DtoMapper;
|
|
import com.drinkool.dtos.SendReview;
|
|
import com.drinkool.entities.ReviewEntity;
|
|
import com.drinkool.enums.SortBy;
|
|
import io.quarkus.panache.common.Sort;
|
|
import io.vertx.core.http.HttpServerRequest;
|
|
import jakarta.annotation.security.RolesAllowed;
|
|
import jakarta.inject.Inject;
|
|
import jakarta.transaction.Transactional;
|
|
import jakarta.ws.rs.DefaultValue;
|
|
import jakarta.ws.rs.GET;
|
|
import jakarta.ws.rs.POST;
|
|
import jakarta.ws.rs.Path;
|
|
import jakarta.ws.rs.PathParam;
|
|
import jakarta.ws.rs.QueryParam;
|
|
import jakarta.ws.rs.core.Response;
|
|
import java.util.UUID;
|
|
|
|
@Path(Url.Review)
|
|
public class ReviewService {
|
|
|
|
@Inject
|
|
HttpServerRequest request;
|
|
|
|
@Inject
|
|
DtoMapper dtoMapper;
|
|
|
|
@GET
|
|
@Transactional
|
|
@Path("/{eateryId}")
|
|
public Response getReviewsByEatery(
|
|
@PathParam("eateryId") UUID eateryId,
|
|
@QueryParam("sort") @DefaultValue("LATEST") SortBy sortBy
|
|
) {
|
|
Sort sort = switch (sortBy) {
|
|
case LATEST -> Sort.descending("createDate");
|
|
case OLDEST -> Sort.ascending("createDate");
|
|
case HIGHEST -> Sort.descending("rating");
|
|
case LOWEST -> Sort.ascending("rating");
|
|
};
|
|
|
|
return Response.ok()
|
|
.entity(ReviewEntity.find("eatery.id = ?1", sort, eateryId).list())
|
|
.build();
|
|
}
|
|
|
|
@POST
|
|
@RolesAllowed(Role.Customer)
|
|
@Transactional
|
|
public Response receiveReview(SendReview input) {
|
|
UUID reviewerId = UUID.fromString(request.getHeader(InternalValue.userId));
|
|
|
|
try {
|
|
ReviewEntity entity = dtoMapper.toReviewEntity(input);
|
|
entity.setReviewerId(reviewerId);
|
|
|
|
entity.persist();
|
|
|
|
return Response.status(Response.Status.CREATED).build();
|
|
} catch (Exception e) {
|
|
return Response.status(Response.Status.BAD_REQUEST)
|
|
.entity(e.getMessage())
|
|
.build();
|
|
}
|
|
}
|
|
}
|