|
| 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 | +} |
0 commit comments