Skip to content

Commit efb83e9

Browse files
authored
refactor BreweryQueries (#6)
1 parent 9963703 commit efb83e9

File tree

5 files changed

+216
-266
lines changed

5 files changed

+216
-266
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using AndcultureCode.CSharp.Core.Extensions;
2+
using AutoMapper;
3+
using HotChocolate;
4+
using HotChocolate.Resolvers;
5+
using HotChocolate.Types.Relay;
6+
using OpenBreweryDB.Core.Conductors;
7+
using OpenBreweryDB.Core.Conductors.Breweries.Interfaces;
8+
using OpenBreweryDB.Core.Extensions;
9+
using OpenBreweryDB.Data.Models;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using System.Linq.Expressions;
14+
using System.Threading;
15+
16+
namespace OpenBreweryDB.API.GraphQL.Breweries
17+
{
18+
public partial class BreweryQueries
19+
{
20+
[UsePaging]
21+
public IQueryable<Brewery> GetBreweries(
22+
[GraphQLDescription("filter by brewery id"), GraphQLName("brewery_id")] string breweryId,
23+
[GraphQLDescription("filter by state")] string state,
24+
[GraphQLDescription("filter by type")] string type,
25+
[GraphQLDescription("search by city name")] string city,
26+
[GraphQLDescription("search by brewery name")] string name,
27+
[GraphQLDescription("general search")] string search,
28+
[GraphQLDescription("sort by")] List<string> sort,
29+
[GraphQLDescription("filter by tags")] List<string> tags,
30+
IResolverContext ctx,
31+
[Service] IBreweryConductor breweryConductor,
32+
[Service] IBreweryValidationConductor validationConductor,
33+
[Service] IBreweryFilterConductor filterConductor,
34+
[Service] IBreweryOrderConductor orderConductor,
35+
[Service] IMapper mapper,
36+
CancellationToken cancellationToken)
37+
{
38+
if (!validationConductor.CanSearch(state, type, out var errors))
39+
{
40+
foreach (var (key, message) in errors)
41+
{
42+
ctx.ReportError(
43+
ErrorBuilder.New()
44+
.SetCode(key)
45+
.SetPath(ctx.Path)
46+
.AddLocation(ctx.FieldSelection)
47+
.SetMessage(message)
48+
.Build()
49+
);
50+
}
51+
52+
return null;
53+
}
54+
55+
Expression<Func<Brewery, bool>> filter;
56+
57+
if (!string.IsNullOrEmpty(breweryId?.Trim()))
58+
{
59+
filter = (b) => b.BreweryId == breweryId;
60+
}
61+
else
62+
{
63+
filter = filterConductor.BuildFilter(
64+
by_name: name,
65+
by_state: state,
66+
by_type: type,
67+
by_city: city,
68+
by_tags: tags);
69+
}
70+
71+
if (!string.IsNullOrEmpty(search))
72+
{
73+
filter = filter.AndAlso(filterConductor
74+
.BuildFilter(by_name: search));
75+
}
76+
77+
// Sorting
78+
Func<IQueryable<Brewery>, IQueryable<Brewery>> orderBy = null;
79+
if (sort != null)
80+
{
81+
orderBy = orderConductor.OrderByFields(
82+
sort?
83+
.Select(s => s.FirstOrDefault() == '-'
84+
? new KeyValuePair<string, SortDirection>(s.Substring(1), SortDirection.DESC)
85+
: new KeyValuePair<string, SortDirection>(s, SortDirection.ASC))
86+
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
87+
);
88+
}
89+
90+
var result = breweryConductor.FindAllQueryable(filter: filter, orderBy: orderBy);
91+
92+
if (!result.HasErrorsOrResultIsNull())
93+
{
94+
return result.ResultObject;
95+
}
96+
97+
foreach (var err in result.Errors)
98+
{
99+
ctx.ReportError(
100+
ErrorBuilder.New()
101+
.SetCode(err.Key)
102+
.SetPath(ctx.Path)
103+
.AddLocation(ctx.FieldSelection)
104+
.SetMessage(err.Message)
105+
.Build()
106+
);
107+
}
108+
109+
return null;
110+
}
111+
}
112+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using AndcultureCode.CSharp.Core.Extensions;
2+
using HotChocolate;
3+
using HotChocolate.Resolvers;
4+
using OpenBreweryDB.Core.Conductors.Breweries.Interfaces;
5+
using OpenBreweryDB.Data.Models;
6+
using System;
7+
using System.Linq;
8+
using System.Linq.Expressions;
9+
using System.Threading;
10+
11+
namespace OpenBreweryDB.API.GraphQL.Breweries
12+
{
13+
public partial class BreweryQueries
14+
{
15+
public Brewery GetBreweryById(
16+
[GraphQLDescription("filter by brewery id"), GraphQLName("brewery_id")] string breweryId,
17+
IResolverContext ctx,
18+
[Service] IBreweryConductor breweryConductor,
19+
CancellationToken cancellationToken)
20+
{
21+
Expression<Func<Brewery, bool>> filter;
22+
23+
if (!string.IsNullOrEmpty(breweryId?.Trim()))
24+
{
25+
filter = (b) => b.BreweryId == breweryId;
26+
}
27+
else
28+
{
29+
filter = (b) => false;
30+
}
31+
32+
var result = breweryConductor.FindAllQueryable(filter: filter);
33+
34+
if (!result.HasErrorsOrResultIsNull())
35+
{
36+
return result.ResultObject.FirstOrDefault();
37+
}
38+
39+
foreach (var err in result.Errors)
40+
{
41+
ctx.ReportError(
42+
ErrorBuilder.New()
43+
.SetCode(err.Key)
44+
.SetPath(ctx.Path)
45+
.AddLocation(ctx.FieldSelection)
46+
.SetMessage(err.Message)
47+
.Build()
48+
);
49+
}
50+
51+
return null;
52+
}
53+
}
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using AndcultureCode.CSharp.Core.Extensions;
2+
using HotChocolate;
3+
using HotChocolate.AspNetCore.Authorization;
4+
using HotChocolate.Resolvers;
5+
using HotChocolate.Types.Relay;
6+
using OpenBreweryDB.Core.Conductors.Breweries.Interfaces;
7+
using OpenBreweryDB.Data.Models;
8+
using System.Linq;
9+
using System.Threading;
10+
11+
namespace OpenBreweryDB.API.GraphQL.Breweries
12+
{
13+
public partial class BreweryQueries
14+
{
15+
[UsePaging]
16+
public IQueryable<Brewery> GetNearbyBreweries(
17+
double latitude,
18+
double longitude,
19+
IResolverContext ctx,
20+
[Service] IBreweryConductor breweryConductor,
21+
CancellationToken cancellationToken)
22+
{
23+
var result = breweryConductor.FindAllByLocation(latitude, longitude);
24+
25+
if (!result.HasErrorsOrResultIsNull())
26+
{
27+
return result.ResultObject;
28+
}
29+
30+
foreach (var err in result.Errors)
31+
{
32+
ctx.ReportError(
33+
ErrorBuilder.New()
34+
.SetCode(err.Key)
35+
.SetPath(ctx.Path)
36+
.AddLocation(ctx.FieldSelection)
37+
.SetMessage(err.Message)
38+
.Build()
39+
);
40+
}
41+
42+
return null;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)