From 5ffdd5ec52b4ca08b9074651c1918a0fb43506f7 Mon Sep 17 00:00:00 2001 From: RudiThoeni Date: Wed, 3 Aug 2022 13:50:24 +0200 Subject: [PATCH 1/4] using dictionary instead of list --- Helper/Postgres/PostgresSQLQueryExtensions.cs | 12 +++++++++++- Helper/Postgres/PostgresSQLWhereBuilder.cs | 6 +++--- .../Controllers/api/ODHActivityPoiController.cs | 2 +- .../Controllers/helper/ODHActivityPoiHelper.cs | 1 + .../Controllers/other/CompatiblityApiController.cs | 2 +- OdhApiCore/GenericHelpers/STARequestHelper.cs | 2 +- 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Helper/Postgres/PostgresSQLQueryExtensions.cs b/Helper/Postgres/PostgresSQLQueryExtensions.cs index d2714ce6d..9822ea004 100644 --- a/Helper/Postgres/PostgresSQLQueryExtensions.cs +++ b/Helper/Postgres/PostgresSQLQueryExtensions.cs @@ -961,7 +961,17 @@ public static Query TaggingFilter_OR(this Query query, string tagkey, IReadOnlyC public static Query TaggingFilter_AND(this Query query, string tagkey, IReadOnlyCollection taglist) => query.Where(q => taglist.Aggregate(q, (q, tag) => - q.WhereRaw(@$"(data->>'Tags')::jsonb @? '$.{tagkey}\[*\] ? (@.Id == ""{tag}"")'"))); + q.WhereRaw(@$"(data->>'Tags')::jsonb @? '$.{tagkey}\[*\] ? (@.Id == ""{tag}"")'"))); + + public static Query TaggingFilter_OR(this Query query, IReadOnlyDictionary tagdict) => + query.Where(q => + tagdict.Aggregate(q, (q, tag) => + q.OrWhereRaw(@$"(data->>'Tags')::jsonb @? '$.{tag.Key}\[*\] ? (@.Id == ""{tag.Value}"")'"))); + + public static Query TaggingFilter_AND(this Query query, IReadOnlyDictionary tagdict) => + query.Where(q => + tagdict.Aggregate(q, (q, tag) => + q.WhereRaw(@$"(data->>'Tags')::jsonb @? '$.{tag.Key}\[*\] ? (@.Id == ""{tag.Value}"")'"))); #endregion diff --git a/Helper/Postgres/PostgresSQLWhereBuilder.cs b/Helper/Postgres/PostgresSQLWhereBuilder.cs index 762b9c0f0..e6d24151d 100644 --- a/Helper/Postgres/PostgresSQLWhereBuilder.cs +++ b/Helper/Postgres/PostgresSQLWhereBuilder.cs @@ -265,7 +265,7 @@ public static Query ODHActivityPoiWhereExpression( IReadOnlyCollection categorycodeslist, IReadOnlyCollection dishcodeslist, IReadOnlyCollection ceremonycodeslist, IReadOnlyCollection facilitycodeslist, IReadOnlyCollection activitytypelist, IReadOnlyCollection poitypelist, IReadOnlyCollection difficultylist, bool distance, int distancemin, int distancemax, bool duration, int durationmin, int durationmax, bool altitude, int altitudemin, int altitudemax, - string tagbehaviour, IReadOnlyCollection taglist, + string tagbehaviour, IReadOnlyDictionary tagdict, IReadOnlyCollection publishedonlist, string? searchfilter, string? language, string? lastchange, bool filterClosedData, bool reducedData) { LogMethodInfo( @@ -310,8 +310,8 @@ public static Query ODHActivityPoiWhereExpression( .DurationFilter(duration, durationmin, durationmax) .AltitudeFilter(altitude, altitudemin, altitudemax) .PublishedOnFilter(publishedonlist) - .When(taglist != null && taglist.Count > 0 && tagbehaviour == "and", q => q.TaggingFilter_AND("idm", taglist)) - .When(taglist != null && taglist.Count > 0 && tagbehaviour == "or", q => q.TaggingFilter_OR("idm", taglist)) + .When(tagdict != null && tagdict.Count > 0 && tagbehaviour == "and", q => q.TaggingFilter_AND(tagdict)) + .When(tagdict != null && tagdict.Count > 0 && tagbehaviour == "or", q => q.TaggingFilter_OR(tagdict)) .SearchFilter(TitleFieldsToSearchFor(language), searchfilter) .LastChangedFilter_GeneratedColumn(lastchange) //.When(filterClosedData, q => q.FilterClosedData_GeneratedColumn()); diff --git a/OdhApiCore/Controllers/api/ODHActivityPoiController.cs b/OdhApiCore/Controllers/api/ODHActivityPoiController.cs index 52d08b180..229047df1 100644 --- a/OdhApiCore/Controllers/api/ODHActivityPoiController.cs +++ b/OdhApiCore/Controllers/api/ODHActivityPoiController.cs @@ -269,7 +269,7 @@ private Task GetFiltered(string[] fields, string? language, uint difficultylist: myodhactivitypoihelper.difficultylist, distance: myodhactivitypoihelper.distance, distancemin: myodhactivitypoihelper.distancemin, distancemax: myodhactivitypoihelper.distancemax, duration: myodhactivitypoihelper.duration, durationmin: myodhactivitypoihelper.durationmin, durationmax: myodhactivitypoihelper.durationmax, altitude: myodhactivitypoihelper.altitude, altitudemin: myodhactivitypoihelper.altitudemin, altitudemax: myodhactivitypoihelper.altitudemax, - tagbehaviour: myodhactivitypoihelper.tagfilterbehaviour, taglist: myodhactivitypoihelper.taglist, publishedonlist: myodhactivitypoihelper.publishedonlist, + tagbehaviour: myodhactivitypoihelper.tagfilterbehaviour, tagdict: myodhactivitypoihelper.tagdict, publishedonlist: myodhactivitypoihelper.publishedonlist, searchfilter: searchfilter, language: language, lastchange: myodhactivitypoihelper.lastchange, filterClosedData: FilterClosedData, reducedData: ReducedData) .ApplyRawFilter(rawfilter) diff --git a/OdhApiCore/Controllers/helper/ODHActivityPoiHelper.cs b/OdhApiCore/Controllers/helper/ODHActivityPoiHelper.cs index 19bb71030..bfae1eccb 100644 --- a/OdhApiCore/Controllers/helper/ODHActivityPoiHelper.cs +++ b/OdhApiCore/Controllers/helper/ODHActivityPoiHelper.cs @@ -50,6 +50,7 @@ public class ODHActivityPoiHelper public string tagfilterbehaviour; public List taglist; + public Dictionary tagdict; //New Publishedonlist public List publishedonlist; diff --git a/OdhApiCore/Controllers/other/CompatiblityApiController.cs b/OdhApiCore/Controllers/other/CompatiblityApiController.cs index e4cae5a1f..21bec1e03 100644 --- a/OdhApiCore/Controllers/other/CompatiblityApiController.cs +++ b/OdhApiCore/Controllers/other/CompatiblityApiController.cs @@ -611,7 +611,7 @@ private Task GetODHActivityPoiReduced( activitytypelist: helper.activitytypelist, poitypelist: helper.poitypelist, difficultylist: helper.difficultylist, distance: helper.distance, distancemin: helper.distancemin, distancemax: helper.distancemax, duration: helper.duration, durationmin: helper.durationmin, durationmax: helper.durationmax, altitude: helper.altitude, altitudemin: helper.altitudemin, altitudemax: helper.altitudemax, - tagbehaviour: helper.tagfilterbehaviour, taglist: helper.taglist, publishedonlist: helper.publishedonlist, + tagbehaviour: helper.tagfilterbehaviour, tagdict: helper.tagdict, publishedonlist: helper.publishedonlist, searchfilter: searchfilter, language: language, lastchange: null, filterClosedData: FilterClosedData, reducedData: ReducedData ) .ApplyRawFilter(rawfilter) diff --git a/OdhApiCore/GenericHelpers/STARequestHelper.cs b/OdhApiCore/GenericHelpers/STARequestHelper.cs index 6fe5679c1..7d66db11a 100644 --- a/OdhApiCore/GenericHelpers/STARequestHelper.cs +++ b/OdhApiCore/GenericHelpers/STARequestHelper.cs @@ -115,7 +115,7 @@ public static async Task GenerateJSONODHActivityPoiForSTA(QueryFactory queryFact activitytypelist: new List(), poitypelist: new List(), difficultylist: new List(), distance: false, distancemin: 0, distancemax: 0, duration: false, durationmin: 0, durationmax: 0, altitude: false, altitudemin: 0, altitudemax: 0, - tagbehaviour: "", taglist: new List(), publishedonlist: new List(), + tagbehaviour: "", tagdict: new Dictionary(), publishedonlist: new List(), searchfilter: null, language: language, lastchange: null, filterClosedData: true, reducedData: true) .OrderByRaw(orderby); From 736d2eff07939744c03b9d39df8d6678bc9bd94a Mon Sep 17 00:00:00 2001 From: RudiThoeni Date: Wed, 3 Aug 2022 16:00:35 +0200 Subject: [PATCH 2/4] improving tagfiltering --- DataModel/SuedtirolDB.cs | 1 + Helper/Postgres/PostgresSQLQueryExtensions.cs | 8 +-- Helper/Postgres/PostgresSQLWhereBuilder.cs | 8 +-- .../Controllers/helper/GenericHelper.cs | 63 ++++++++++++++++++- .../helper/ODHActivityPoiHelper.cs | 5 +- OdhApiCore/GenericHelpers/STARequestHelper.cs | 2 +- OdhApiCore/Startup.cs | 4 +- 7 files changed, 78 insertions(+), 13 deletions(-) diff --git a/DataModel/SuedtirolDB.cs b/DataModel/SuedtirolDB.cs index 42ea630d9..676e2d8eb 100644 --- a/DataModel/SuedtirolDB.cs +++ b/DataModel/SuedtirolDB.cs @@ -3487,6 +3487,7 @@ public EventShort() public string? Id { get; set; } public string? Source { get; set; } + [SwaggerEnum(new[] { "NOI", "EC" })] public string? EventLocation { get; set; } diff --git a/Helper/Postgres/PostgresSQLQueryExtensions.cs b/Helper/Postgres/PostgresSQLQueryExtensions.cs index 9822ea004..c1b9b6e69 100644 --- a/Helper/Postgres/PostgresSQLQueryExtensions.cs +++ b/Helper/Postgres/PostgresSQLQueryExtensions.cs @@ -963,15 +963,15 @@ public static Query TaggingFilter_AND(this Query query, string tagkey, IReadOnly taglist.Aggregate(q, (q, tag) => q.WhereRaw(@$"(data->>'Tags')::jsonb @? '$.{tagkey}\[*\] ? (@.Id == ""{tag}"")'"))); - public static Query TaggingFilter_OR(this Query query, IReadOnlyDictionary tagdict) => + public static Query TaggingFilter_OR(this Query query, IDictionary tagdict) => query.Where(q => tagdict.Aggregate(q, (q, tag) => - q.OrWhereRaw(@$"(data->>'Tags')::jsonb @? '$.{tag.Key}\[*\] ? (@.Id == ""{tag.Value}"")'"))); + q.OrWhereRaw(@$"(data->>'Tags')::jsonb @? '$.{tag.Value}\[*\] ? (@.Id == ""{tag.Key}"")'"))); - public static Query TaggingFilter_AND(this Query query, IReadOnlyDictionary tagdict) => + public static Query TaggingFilter_AND(this Query query, IDictionary tagdict) => query.Where(q => tagdict.Aggregate(q, (q, tag) => - q.WhereRaw(@$"(data->>'Tags')::jsonb @? '$.{tag.Key}\[*\] ? (@.Id == ""{tag.Value}"")'"))); + q.WhereRaw(@$"(data->>'Tags')::jsonb @? '$.{tag.Value}\[*\] ? (@.Id == ""{tag.Key}"")'"))); #endregion diff --git a/Helper/Postgres/PostgresSQLWhereBuilder.cs b/Helper/Postgres/PostgresSQLWhereBuilder.cs index e6d24151d..47ae97c1e 100644 --- a/Helper/Postgres/PostgresSQLWhereBuilder.cs +++ b/Helper/Postgres/PostgresSQLWhereBuilder.cs @@ -265,7 +265,7 @@ public static Query ODHActivityPoiWhereExpression( IReadOnlyCollection categorycodeslist, IReadOnlyCollection dishcodeslist, IReadOnlyCollection ceremonycodeslist, IReadOnlyCollection facilitycodeslist, IReadOnlyCollection activitytypelist, IReadOnlyCollection poitypelist, IReadOnlyCollection difficultylist, bool distance, int distancemin, int distancemax, bool duration, int durationmin, int durationmax, bool altitude, int altitudemin, int altitudemax, - string tagbehaviour, IReadOnlyDictionary tagdict, + string tagbehaviour, IDictionary> tagdict, IReadOnlyCollection publishedonlist, string? searchfilter, string? language, string? lastchange, bool filterClosedData, bool reducedData) { LogMethodInfo( @@ -309,9 +309,9 @@ public static Query ODHActivityPoiWhereExpression( .DistanceFilter(distance, distancemin, distancemax) .DurationFilter(duration, durationmin, durationmax) .AltitudeFilter(altitude, altitudemin, altitudemax) - .PublishedOnFilter(publishedonlist) - .When(tagdict != null && tagdict.Count > 0 && tagbehaviour == "and", q => q.TaggingFilter_AND(tagdict)) - .When(tagdict != null && tagdict.Count > 0 && tagbehaviour == "or", q => q.TaggingFilter_OR(tagdict)) + .PublishedOnFilter(publishedonlist) + .When(tagdict != null && tagdict.ContainsKey("and") && tagdict["and"].Count > 0, q => q.TaggingFilter_AND(tagdict["and"])) + .When(tagdict != null && tagdict.ContainsKey("or") && tagdict["or"].Count > 0, q => q.TaggingFilter_OR(tagdict["or"])) .SearchFilter(TitleFieldsToSearchFor(language), searchfilter) .LastChangedFilter_GeneratedColumn(lastchange) //.When(filterClosedData, q => q.FilterClosedData_GeneratedColumn()); diff --git a/OdhApiCore/Controllers/helper/GenericHelper.cs b/OdhApiCore/Controllers/helper/GenericHelper.cs index 71d7ae165..3e6627d07 100644 --- a/OdhApiCore/Controllers/helper/GenericHelper.cs +++ b/OdhApiCore/Controllers/helper/GenericHelper.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; @@ -41,6 +42,66 @@ public static async Task> RetrieveLocFilterDataAsync( where region.TourismvereinIds != null from tid in region.TourismvereinIds ?? Enumerable.Empty() select tid).Distinct().ToList(); - } + } + + #region Tag Filter + + public static IDictionary> RetrieveTagFilter(string tagfilter) + { + try + { + if (tagfilter == null) + return null; + + var tagstofilter = new Dictionary>(); + + //Examples + //tagfilter = and(idm.Winter,idm.Sommer) + //tagfilter = or(lts.Winter,lts.Sommer) + //tagfilter = or(lts.Winter,idm.Sommer)and(lts.Winter,idm.Sommer) not wokring at the moment + + //Get Tagoperator + string tagoperator = tagfilter.Split('(').First(); + + //Get data inside brackets + var bracketdatalist = GetSubStrings(tagfilter, "(", ")"); + + foreach(var bracketdata in bracketdatalist) + { + var splittedelements = bracketdata.Split(","); + + var splitdict = new Dictionary(); + + foreach (var splittedelement in splittedelements) + { + var splittedtag = splittedelement.Split("."); + if (splittedtag.Length > 1) + { + splitdict.Add(splittedtag[1], splittedtag[0]); + } + } + + tagstofilter.Add(tagoperator, splitdict); + } + + + + return tagstofilter; + } + catch(Exception ex) + { + return null; + } + } + + private static IEnumerable GetSubStrings(string input, string start, string end) + { + Regex r = new Regex(Regex.Escape(start) + "(.*?)" + Regex.Escape(end)); + MatchCollection matches = r.Matches(input); + foreach (Match match in matches) + yield return match.Groups[1].Value; + } + + #endregion } } diff --git a/OdhApiCore/Controllers/helper/ODHActivityPoiHelper.cs b/OdhApiCore/Controllers/helper/ODHActivityPoiHelper.cs index bfae1eccb..0cfd71cbe 100644 --- a/OdhApiCore/Controllers/helper/ODHActivityPoiHelper.cs +++ b/OdhApiCore/Controllers/helper/ODHActivityPoiHelper.cs @@ -50,7 +50,8 @@ public class ODHActivityPoiHelper public string tagfilterbehaviour; public List taglist; - public Dictionary tagdict; + + public IDictionary> tagdict; //New Publishedonlist public List publishedonlist; @@ -271,6 +272,8 @@ private ODHActivityPoiHelper( taglist = Helper.CommonListCreator.CreateIdList(tagfilter); + tagdict = GenericHelper.RetrieveTagFilter(tagfilter); + this.lastchange = lastchange; } diff --git a/OdhApiCore/GenericHelpers/STARequestHelper.cs b/OdhApiCore/GenericHelpers/STARequestHelper.cs index 7d66db11a..9aab6878d 100644 --- a/OdhApiCore/GenericHelpers/STARequestHelper.cs +++ b/OdhApiCore/GenericHelpers/STARequestHelper.cs @@ -115,7 +115,7 @@ public static async Task GenerateJSONODHActivityPoiForSTA(QueryFactory queryFact activitytypelist: new List(), poitypelist: new List(), difficultylist: new List(), distance: false, distancemin: 0, distancemax: 0, duration: false, durationmin: 0, durationmax: 0, altitude: false, altitudemin: 0, altitudemax: 0, - tagbehaviour: "", tagdict: new Dictionary(), publishedonlist: new List(), + tagbehaviour: "", tagdict: null, publishedonlist: new List(), searchfilter: null, language: language, lastchange: null, filterClosedData: true, reducedData: true) .OrderByRaw(orderby); diff --git a/OdhApiCore/Startup.cs b/OdhApiCore/Startup.cs index f74115769..0f34625ed 100644 --- a/OdhApiCore/Startup.cs +++ b/OdhApiCore/Startup.cs @@ -355,9 +355,9 @@ public void ConfigureServices(IServiceCollection services) // Set the comments path for the Swagger JSON and UI. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); - var xmlPathdatamodel = Path.Combine(AppContext.BaseDirectory, $"DataModel.xml"); + //var xmlPathdatamodel = Path.Combine(AppContext.BaseDirectory, $"DataModel.xml"); c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true); - c.IncludeXmlComments(xmlPathdatamodel, includeControllerXmlComments: true); + //c.IncludeXmlComments(xmlPathdatamodel, includeControllerXmlComments: true); c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { In = ParameterLocation.Header, From bea4f2491ab4981a3793004b05858355b15c0178 Mon Sep 17 00:00:00 2001 From: RudiThoeni Date: Wed, 3 Aug 2022 17:13:44 +0200 Subject: [PATCH 3/4] adding parameter comment --- OdhApiCore/Controllers/api/ODHActivityPoiController.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/OdhApiCore/Controllers/api/ODHActivityPoiController.cs b/OdhApiCore/Controllers/api/ODHActivityPoiController.cs index 229047df1..052f9f90f 100644 --- a/OdhApiCore/Controllers/api/ODHActivityPoiController.cs +++ b/OdhApiCore/Controllers/api/ODHActivityPoiController.cs @@ -67,6 +67,7 @@ public ODHActivityPoiController(IWebHostEnvironment env, ISettings settings, ILo /// Altitude Range Filter (Separator ',' example Value: 500,1000 Altitude from 500 up to 1000 metres), (default:'null') /// Duration Range Filter (Separator ',' example Value: 1,3 Duration from 1 to 3 hours), (default:'null') /// Difficulty Filter (possible values: '1' = easy, '2' = medium, '3' = difficult), (default:'null') + /// Filter on Tags. Syntax =and/or(TagSource.TagId,TagSource.TagId) example or(idm.summer,lts.hiking) and(idm.themed hikes,lts.family hikings), default: 'null') /// Published On Filter (Separator ',' List of publisher IDs), (default:'null') /// Returns data changed after this date Format (yyyy-MM-dd), (default: 'null') /// Language field selector, displays data and fields available in the selected language (default:'null' all languages are displayed) From 4e19d4af6526d539f63244878c28bdf596ab86c4 Mon Sep 17 00:00:00 2001 From: RudiThoeni <30471361+RudiThoeni@users.noreply.github.com> Date: Wed, 3 Aug 2022 18:13:34 +0200 Subject: [PATCH 4/4] Update Readme.md --- RawQueryParser/Readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/RawQueryParser/Readme.md b/RawQueryParser/Readme.md index 1c0e09f69..a6fb70a3b 100644 --- a/RawQueryParser/Readme.md +++ b/RawQueryParser/Readme.md @@ -33,6 +33,9 @@ in(Features.[*].Id, "a3067617-771a-4b84-b85e-206e5cf4402b") // all entries in in(Features.[].Id, "a3067617-771a-4b84-b85e-206e5cf4402b") // all entries in an Array with a specific feature ID - alternative notation ``` +**Attention**: On the field _ImageGallery _if data is retrieved as anonymous there is a Transformer applied after getting the Result from the DB which cuts out all not CC0 license Images. So there the rawfilter possible can produce false results. + + > `` is described [here](#Supported-value-types:) > `` is described [here](#Field-syntax:)