Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce messaging feature #508

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ dist/
# Experimental
xxx_*
xxx/*
local-messaging.txt
10 changes: 10 additions & 0 deletions db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ entity Travel : managed {
EndDate : Date;
BookingFee : Decimal(16, 3);
TotalPrice : Decimal(16, 3) @readonly;
AvgReviewRating: Decimal;
CurrencyCode : Currency;
Description : String(1024);
TravelStatus : Association to TravelStatus @readonly;
Expand All @@ -37,6 +38,7 @@ annotate Travel with @(
});



entity Booking : managed {
key BookingUUID : UUID;
BookingID : Integer @Core.Computed;
Expand Down Expand Up @@ -89,3 +91,11 @@ entity TravelStatus : CodeList {
createDeleteHidden: Boolean;
insertDeleteRestriction: Boolean; // = NOT createDeleteHidden
}

entity TravelReview : managed {
key RatingUUID : UUID;
TravelID : Integer;
Rating : Integer @assert.range: [ 0, 5 ];
Email : String;
Comment : String;
}
1 change: 1 addition & 0 deletions local-messaging.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sap.cap.reviews.review.changed {"data":{"subject":42,"count":1,"rating":5}}
3 changes: 0 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@
<requireMavenVersion>
<version>3.5.0</version>
</requireMavenVersion>
<!-- <requireJavaVersion>
<version>${jdk.version}</version>
</requireJavaVersion> -->
<requireProperty>
<property>project.artifactId</property>
<regex>[^_]+</regex>
Expand Down
11 changes: 11 additions & 0 deletions srv/post_review.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
POST http://localhost:8080/reviewer/TravelReview HTTP/1.1
Authorization: Basic YXV0aGVudGljYXRlZDo=
content-type: application/json

{
"RatingUUID": "D5055A5D-8EED-47E3-AB38-6D646120F9DD",
"Rating" : 4,
"Email" : "[email protected]",
"Comment" : "awesome",
"TravelID" : 4132
}
13 changes: 13 additions & 0 deletions srv/review-service.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using { sap.fe.cap.travel as my } from '../db/schema';

service ReviewService @(path:'/reviewer') {

entity TravelReview as projection on my.TravelReview;

@topic: 'sap.cap.reviews.review.changed'
event Reviewed : {
subject : Integer;
count : Integer;
rating : Decimal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import com.sap.cds.ql.Select;
import com.sap.cds.ql.Update;
import com.sap.cds.services.cds.CdsService;
import com.sap.cds.services.cds.CqnService;
import com.sap.cds.services.draft.DraftService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.Before;
Expand Down Expand Up @@ -42,7 +42,7 @@ public CreationHandler(PersistenceService persistenceService, DraftService draft
this.draftService = draftService;
}

@Before(event = { CdsService.EVENT_CREATE, CdsService.EVENT_UPDATE, DraftService.EVENT_DRAFT_CREATE}, entity = Travel_.CDS_NAME)
@Before(event = { CqnService.EVENT_CREATE, CqnService.EVENT_UPDATE, DraftService.EVENT_DRAFT_CREATE}, entity = Travel_.CDS_NAME)
public void setBookingDateIfNotProvided(final Travel travel) {
if (travel.getBeginDate() == null) {
travel.setBeginDate(LocalDate.now());
Expand Down Expand Up @@ -81,7 +81,7 @@ public void saveComputedValues(DraftActivateContext ctx) {
});
}

@Before(event = { CdsService.EVENT_CREATE, CdsService.EVENT_UPDATE }, entity = Travel_.CDS_NAME)
@Before(event = { CqnService.EVENT_CREATE, CqnService.EVENT_UPDATE }, entity = Travel_.CDS_NAME)
public void checkTravelEndDateIsAfterBeginDate(Travel travel) {

if (travel.getBeginDate() != null && travel.getEndDate() != null) {
Expand All @@ -97,7 +97,7 @@ public void checkTravelEndDateIsAfterBeginDate(Travel travel) {
}
}

@Before(event = CdsService.EVENT_CREATE, entity = Travel_.CDS_NAME)
@Before(event = CqnService.EVENT_CREATE, entity = Travel_.CDS_NAME)
public void calculateTravelIdBeforeCreation(final Travel travel) {
if (travel.getTravelID() == null || travel.getTravelID() == 0) {
Select<Travel_> maxIdSelect = Select.from(TravelService_.TRAVEL).columns(e -> e.TravelID().max().as(MAX_ID));
Expand All @@ -107,7 +107,7 @@ public void calculateTravelIdBeforeCreation(final Travel travel) {
}
}

@Before(event = { CdsService.EVENT_CREATE, CdsService.EVENT_UPDATE, }, entity = Travel_.CDS_NAME)
@Before(event = { CqnService.EVENT_CREATE, CqnService.EVENT_UPDATE, }, entity = Travel_.CDS_NAME)
public void fillBookingIdsBeforeCreationAndUpdate(final Travel travel) {
if (travel.getToBooking() != null) {
addBookingIds(travel);
Expand Down Expand Up @@ -152,7 +152,7 @@ private void addBookingIds(Travel travel) {
}
}

@Before(event = CdsService.EVENT_CREATE, entity = Travel_.CDS_NAME)
@Before(event = CqnService.EVENT_CREATE, entity = Travel_.CDS_NAME)
public void initialTravelStatus(final Travel travel) {
TravelStatus travelStatus = TravelStatus.create();
travelStatus.setCode("O");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.sap.cds.ql.cqn.CqnUpdate;
import com.sap.cds.services.ErrorStatuses;
import com.sap.cds.services.ServiceException;
import com.sap.cds.services.cds.CdsService;
import com.sap.cds.services.cds.CqnService;
import com.sap.cds.services.draft.DraftService;
import com.sap.cds.services.handler.EventHandler;
Expand Down Expand Up @@ -94,7 +93,7 @@ private static BigDecimal calculateTotalPriceForTravel(CqnService db, String tra
return bookingFee.add(flightPriceSum).add(supplementPriceSum);
}

@After(event = {CdsService.EVENT_UPDATE, CdsService.EVENT_CREATE}, entity = Travel_.CDS_NAME)
@After(event = {CqnService.EVENT_UPDATE, CqnService.EVENT_CREATE}, entity = Travel_.CDS_NAME)
public void calculateNewTotalPriceForActiveTravel(Travel travel) {

/*
Expand Down Expand Up @@ -149,7 +148,7 @@ public void recalculateTravelPriceIfPriceWasUpdated(final BookingSupplement book
private BigDecimal calculateAndPatchNewTotalPriceForDraft(final String travelUUID) {

BigDecimal totalPrice = calculateTotalPriceForTravel(draftService, travelUUID, false);
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map = new HashMap<>();
map.put(Travel.TRAVEL_UUID, travelUUID);
map.put(Travel.TOTAL_PRICE, totalPrice);
CqnUpdate update = Update.entity(TRAVEL).data(map);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.sap.cap.sflight.processor;

import static cds.gen.travelservice.Travel.AVG_REVIEW_RATING;
import static cds.gen.travelservice.TravelService_.TRAVEL;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.sap.cds.ql.Update;
import com.sap.cds.services.cds.CqnService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;

import cds.gen.reviewservice.ReviewService_;
import cds.gen.reviewservice.ReviewedContext;
import cds.gen.travelservice.TravelService_;

@Component
@ServiceName(TravelService_.CDS_NAME)
public class TravelReviewChangedHandler implements EventHandler {

private static final Logger logger = LoggerFactory.getLogger(TravelReviewChangedHandler.class);

private final CqnService travelService;

public TravelReviewChangedHandler(CqnService travelService) {
this.travelService = travelService;
}

@On(service = ReviewService_.CDS_NAME)
public void onReviewChanged(ReviewedContext context) {

BigDecimal rating = context.getData().getRating();
Integer travelId = context.getData().getSubject();

Map<String, Integer> updateFilter = new HashMap<>();
updateFilter.put("TravelID", travelId);

travelService.run(Update.entity(TRAVEL, t -> t.matching(updateFilter)).data(AVG_REVIEW_RATING, rating));
logger.info("Successfully updated travel with travelId {} with a new average rating of {}.", travelId, rating);
}

}
52 changes: 52 additions & 0 deletions srv/src/main/java/com/sap/cap/sflight/reviewer/ReviewHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.sap.cap.sflight.reviewer;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.sap.cds.Row;
import com.sap.cds.ql.Select;
import com.sap.cds.services.cds.CqnService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.After;
import com.sap.cds.services.handler.annotations.ServiceName;

import cds.gen.reviewservice.ReviewService_;
import cds.gen.reviewservice.Reviewed;
import cds.gen.reviewservice.ReviewedContext;
import cds.gen.reviewservice.TravelReview;
import cds.gen.reviewservice.TravelReview_;

@Component
@ServiceName(ReviewService_.CDS_NAME)
public class ReviewHandler implements EventHandler {

private final CqnService reviewService;

public ReviewHandler(@Qualifier(ReviewService_.CDS_NAME) CqnService reviewService) {
this.reviewService = reviewService;
}

@After(event = { CqnService.EVENT_CREATE, CqnService.EVENT_UPDATE, CqnService.EVENT_UPSERT,
CqnService.EVENT_DELETE }, entity = TravelReview_.CDS_NAME)
public void afterReviewWrite(TravelReview travelReview) {

Select<TravelReview_> averageRatingQuery = Select.from(ReviewService_.TRAVEL_REVIEW)
.columns(t -> t.Rating().average().as("average"), t -> t.Rating().countDistinct().as("reviewCount"))
.where(t -> t.TravelID().eq(travelReview.getTravelID()));

Row resultRow = reviewService.run(averageRatingQuery).single();


Reviewed reviewed = Reviewed.create();
reviewed.setCount(Integer.valueOf(resultRow.get("reviewCount").toString()));
reviewed.setRating(BigDecimal.valueOf(5L));
reviewed.setSubject(travelReview.getTravelID());

ReviewedContext reviewedContext = ReviewedContext.create();
reviewedContext.setData(reviewed);

reviewService.emit(reviewedContext);
}
}
8 changes: 7 additions & 1 deletion srv/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ management:
spring:
profiles: default
web.resources.static-locations: "file:./app"
logging.level.com.sap.cds.messaging: DEBUG
cds:
security.mock.users:
- name: rose
Expand All @@ -24,9 +25,14 @@ cds:
odata-v4:
endpoint:
path: "/"
messaging:
services:
- name: "messaging"
kind: "file-based-messaging"
binding: "local-messaging.txt"

server:
port: 4004
port: 8080

---
spring:
Expand Down