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

Alleop - reviews and ratings to have separate from 1 to 5 starts #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 223 additions & 3 deletions dotnet/GraphQL/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,44 @@ public Query(IProductReviewService productReviewService)
var searchResult = productReviewService.GetReviews();
IList<Review> searchData = await productReviewService.FilterReviews(searchResult.Result, searchTerm, orderBy, status);
int totalCount = searchData.Count;

int total5 = 0;
int total4 = 0;
int total3 = 0;
int total2 = 0;
int total1 = 0;
for (int i = 0; i < searchData.Count; i++)
{
if (searchData[i].Rating == 5)
{
total5++;
}
else if (searchData[i].Rating == 4)
{
total4++;
}
else if (searchData[i].Rating == 3)
{
total3++;
}
else if (searchData[i].Rating == 2)
{
total2++;
}
else if (searchData[i].Rating == 1)
{
total1++;
}
}
Console.WriteLine($"total3 = {total3}");

searchData = await productReviewService.LimitReviews(searchData, from, to);
Console.WriteLine($"totalCount = {totalCount} : Filtered to {searchData.Count}");
SearchResponse searchResponse = new SearchResponse
{
Data = new DataElement { data = searchData },
Range = new SearchRange { From = from, To = to, Total = totalCount }
Range = new SearchRange { From = from, To = to, Total = totalCount },
Totals = new Totals { Total5 = total5, Total4 = total4, Total3 = total3, Total2 = total2, Total1 = total1 }
};

return searchResponse;
Expand Down Expand Up @@ -83,12 +115,44 @@ public Query(IProductReviewService productReviewService)
var searchResult = await productReviewService.GetReviewsByProductId(productId);
IList<Review> searchData = await productReviewService.FilterReviews(searchResult, searchTerm, orderBy, status);
int totalCount = searchData.Count;

int total5 = 0;
int total4 = 0;
int total3 = 0;
int total2 = 0;
int total1 = 0;
for (int i = 0; i < searchData.Count; i++)
{
if (searchData[i].Rating == 5)
{
total5++;
}
else if (searchData[i].Rating == 4)
{
total4++;
}
else if (searchData[i].Rating == 3)
{
total3++;
}
else if (searchData[i].Rating == 2)
{
total2++;
}
else if (searchData[i].Rating == 1)
{
total1++;
}
}
Console.WriteLine($"total3 = {total3}");

searchData = await productReviewService.LimitReviews(searchData, from, to);
Console.WriteLine($"totalCount = {totalCount} : Filtered to {searchData.Count}");
SearchResponse searchResponse = new SearchResponse
{
Data = new DataElement { data = searchData },
Range = new SearchRange { From = from, To = to, Total = totalCount }
Range = new SearchRange { From = from, To = to, Total = totalCount },
Totals = new Totals { Total5 = total5, Total4 = total4, Total3 = total3, Total2 = total2, Total1 = total1 }
};

return searchResponse;
Expand Down Expand Up @@ -130,6 +194,130 @@ public Query(IProductReviewService productReviewService)
}
);

FieldAsync<IntGraphType>(
"totalReviewsByProductId5",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 0;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));

if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++) {
if (searchResult[i].Rating == 5)
{
count++;
}
}
}

return count;
}
);

FieldAsync<IntGraphType>(
"totalReviewsByProductId4",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 0;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));

if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++)
{
if (searchResult[i].Rating == 4)
{
count++;
}
}
}

return count;
}
);

FieldAsync<IntGraphType>(
"totalReviewsByProductId3",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 0;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));

if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++)
{
if (searchResult[i].Rating == 3)
{
count++;
}
}
}

return count;
}
);

FieldAsync<IntGraphType>(
"totalReviewsByProductId2",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 0;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));

if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++)
{
if (searchResult[i].Rating == 2)
{
count++;
}
}
}

return count;
}
);

FieldAsync<IntGraphType>(
"totalReviewsByProductId1",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 1;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));

if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++)
{
if (searchResult[i].Rating == 1)
{
count++;
}
}
}

return count;
}
);
Comment on lines +197 to +319
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
FieldAsync<IntGraphType>(
"totalReviewsByProductId5",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 0;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));
if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++) {
if (searchResult[i].Rating == 5)
{
count++;
}
}
}
return count;
}
);
FieldAsync<IntGraphType>(
"totalReviewsByProductId4",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 0;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));
if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++)
{
if (searchResult[i].Rating == 4)
{
count++;
}
}
}
return count;
}
);
FieldAsync<IntGraphType>(
"totalReviewsByProductId3",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 0;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));
if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++)
{
if (searchResult[i].Rating == 3)
{
count++;
}
}
}
return count;
}
);
FieldAsync<IntGraphType>(
"totalReviewsByProductId2",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 0;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));
if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++)
{
if (searchResult[i].Rating == 2)
{
count++;
}
}
}
return count;
}
);
FieldAsync<IntGraphType>(
"totalReviewsByProductId1",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "productId", Description = "Product Id" }
),
resolve: async context =>
{
int count = 1;
var searchResult = await productReviewService.GetReviewsByProductId(context.GetArgument<string>("productId"));
if (searchResult != null && searchResult.Count > 0)
{
for (int i = 0; i < searchResult.Count; i++)
{
if (searchResult[i].Rating == 1)
{
count++;
}
}
}
return count;
}
);

Can remove those resolvers


FieldAsync<SearchResponseType>(
"reviewsByShopperId",
arguments: new QueryArguments(
Expand All @@ -152,12 +340,44 @@ public Query(IProductReviewService productReviewService)
var searchResult = productReviewService.GetReviewsByShopperId(shopperId);
IList<Review> searchData = await productReviewService.FilterReviews(searchResult.Result, searchTerm, orderBy, status);
int totalCount = searchData.Count;

int total5 = 0;
int total4 = 0;
int total3 = 0;
int total2 = 0;
int total1 = 0;
for (int i = 0; i < searchData.Count; i++)
{
if (searchData[i].Rating == 5)
{
total5++;
}
else if (searchData[i].Rating == 4)
{
total4++;
}
else if (searchData[i].Rating == 3)
{
total3++;
}
else if (searchData[i].Rating == 2)
{
total2++;
}
else if (searchData[i].Rating == 1)
{
total1++;
}
}
Console.WriteLine($"total3 = {total3}");

searchData = await productReviewService.LimitReviews(searchData, from, to);
Console.WriteLine($"totalCount = {totalCount} : Filtered to {searchData.Count}");
SearchResponse searchResponse = new SearchResponse
{
Data = new DataElement { data = searchData },
Range = new SearchRange { From = from, To = to, Total = totalCount }
Range = new SearchRange { From = from, To = to, Total = totalCount },
Totals = new Totals { Total5 = total5, Total4 = total4, Total3 = total3, Total2 = total2, Total1 = total1 }
};

return searchResponse;
Expand Down
1 change: 1 addition & 0 deletions dotnet/GraphQL/Types/SearchResponseType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public SearchResponseType(IProductReviewService productReviewService)
Name = "SearchResponse";
Field(b => b.Data, type: typeof(ListGraphType<ReviewType>)).Description("List of Reviews.");
Field(b => b.Range, type: typeof(RangeType)).Description("Pagination values.");
Field(b => b.Totals, type: typeof(TotalsType)).Description("Total values.");
//Field<ListGraphType<ReviewType>>("data");
//Field<RangeType>("range");
}
Expand Down
24 changes: 24 additions & 0 deletions dotnet/GraphQL/Types/TotalsType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using GraphQL;
using GraphQL.Types;
using ReviewsRatings.Models;
using ReviewsRatings.Services;
using System;
using System.Collections.Generic;
using System.Text;

namespace ReviewsRatings.GraphQL.Types
{
[GraphQLMetadata("Totals")]
public class TotalsType : ObjectGraphType<Totals>
{
public TotalsType(IProductReviewService productReviewService)
{
Name = "Range";
Field(b => b.Total5);
Field(b => b.Total4);
Field(b => b.Total3);
Field(b => b.Total2);
Field(b => b.Total1);
}
}
}
21 changes: 21 additions & 0 deletions dotnet/Models/SearchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ public class SearchRange
public long To { get; set; }
}

public class Totals
{
[DataMember(Name = "total5")]
public long Total5 { get; set; }

[DataMember(Name = "total4")]
public long Total4 { get; set; }

[DataMember(Name = "total3")]
public long Total3 { get; set; }

[DataMember(Name = "total2")]
public long Total2 { get; set; }

[DataMember(Name = "total1")]
public long Total1 { get; set; }
}

public class SearchResponse
{
//SearchResponse searchResponse;
Expand All @@ -29,6 +47,9 @@ public class SearchResponse
[DataMember(Name = "range")]
public SearchRange Range { get; set; }

[DataMember(Name = "totals")]
public Totals Totals { get; set; }

//public IEnumerator<SearchResponse> GetEnumerator()
//{
//foreach (var searchResponse in searchResponses)
Expand Down
7 changes: 7 additions & 0 deletions graphql/reviews.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,12 @@ query Reviews(
from
to
}
totals {
total5
total4
total3
total2
total1
}
}
}
7 changes: 7 additions & 0 deletions graphql/reviewsByProductId.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,12 @@ query ReviewsByProductId(
from
to
}
totals {
total5
total4
total3
total2
total1
}
}
}
13 changes: 13 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ type Data {
approved: Boolean
}

type Totals {
total5: Int
total4: Int
total3: Int
total2: Int
total1: Int
}

type Range {
total: Int
from: Int
Expand Down Expand Up @@ -72,6 +80,11 @@ type Query {
averageRatingByProductId(productId: String!): Float
@cacheControl(scope: PRIVATE)
totalReviewsByProductId(productId: String!): Int @cacheControl(scope: PRIVATE)
totalReviewsByProductId5(productId: String!): Int @cacheControl(scope: PRIVATE)
totalReviewsByProductId4(productId: String!): Int @cacheControl(scope: PRIVATE)
totalReviewsByProductId3(productId: String!): Int @cacheControl(scope: PRIVATE)
totalReviewsByProductId2(productId: String!): Int @cacheControl(scope: PRIVATE)
totalReviewsByProductId1(productId: String!): Int @cacheControl(scope: PRIVATE)
Comment on lines +83 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove those queries and just add the new field totals to ReviewsResult

Suggested change
totalReviewsByProductId5(productId: String!): Int @cacheControl(scope: PRIVATE)
totalReviewsByProductId4(productId: String!): Int @cacheControl(scope: PRIVATE)
totalReviewsByProductId3(productId: String!): Int @cacheControl(scope: PRIVATE)
totalReviewsByProductId2(productId: String!): Int @cacheControl(scope: PRIVATE)
totalReviewsByProductId1(productId: String!): Int @cacheControl(scope: PRIVATE)

reviewsByShopperId(
shopperId: String!
from: Int
Expand Down
3 changes: 3 additions & 0 deletions graphql/totalReviewsByProductId1.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query TotalReviewsByProductId1($productId: String!) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to create new queries, just add a new field ratingHistogram in ReviewsResult.

totalReviewsByProductId1(productId: $productId)
}
3 changes: 3 additions & 0 deletions graphql/totalReviewsByProductId2.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query TotalReviewsByProductId2($productId: String!) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

totalReviewsByProductId2(productId: $productId)
}
Loading