From b17e6222800915970e09fb72609fd17bc3b99309 Mon Sep 17 00:00:00 2001 From: Thomas Hounsell Date: Thu, 10 Sep 2015 21:01:39 +0100 Subject: [PATCH] More amends, inc a bit of Localisation --- BuildFeed/Controllers/apiController.cs | 247 +++++++++++++------------ BuildFeed/Local/Common.Designer.cs | 54 ++++++ BuildFeed/Local/Common.qps-ploc.resx | 18 ++ BuildFeed/Local/Common.resx | 18 ++ BuildFeed/Models/Build.cs | 103 +++++++++-- 5 files changed, 300 insertions(+), 140 deletions(-) diff --git a/BuildFeed/Controllers/apiController.cs b/BuildFeed/Controllers/apiController.cs index ae90caf..7e661a4 100644 --- a/BuildFeed/Controllers/apiController.cs +++ b/BuildFeed/Controllers/apiController.cs @@ -1,4 +1,5 @@ -using BuildFeed.Models; +using BuildFeed.Local; +using BuildFeed.Models; using BuildFeed.Models.ApiModel; using System; using System.Collections.Generic; @@ -8,141 +9,147 @@ namespace BuildFeed.Controllers { - public class apiController : ApiController - { - public IEnumerable GetBuilds() - { - return new Build().SelectInBuildOrder(); - } + public class apiController : ApiController + { + private Build bModel; - public IEnumerable GetWin10Labs() - { - Build b = new Build(); - List labs = new List(); - labs.AddRange(b.SelectBuildLabs(6, 4)); - labs.AddRange(b.SelectBuildLabs(10, 0)); + public apiController() : base() + { + bModel = new Build(); + } - return labs.GroupBy(l => l).Select(l => l.Key).Where(l => l.All(c => c != '(')).ToArray(); - } + public IEnumerable GetBuilds() + { + return bModel.SelectInBuildOrder(); + } - [HttpPost] - public bool AddWin10Builds(NewBuild apiModel) - { - if (apiModel == null) + public IEnumerable GetWin10Labs() + { + List labs = new List(); + labs.AddRange(bModel.SelectBuildLabs(6, 4)); + labs.AddRange(bModel.SelectBuildLabs(10, 0)); + + return labs.GroupBy(l => l).Select(l => l.Key).Where(l => l.All(c => c != '(')).ToArray(); + } + + [HttpPost] + public bool AddWin10Builds(NewBuild apiModel) + { + if (apiModel == null) + { + return false; + } + if (Membership.ValidateUser(apiModel.Username, apiModel.Password)) + { + bModel.InsertAll(apiModel.NewBuilds.Select(nb => new BuildModel() { - return false; - } - if (Membership.ValidateUser(apiModel.Username, apiModel.Password)) - { - new Build().InsertAll(apiModel.NewBuilds.Select(nb => new BuildModel() - { - MajorVersion = nb.MajorVersion, - MinorVersion = nb.MinorVersion, - Number = nb.Number, - Revision = nb.Revision, - Lab = nb.Lab, - BuildTime = nb.BuildTime, - FlightLevel = nb.FlightLevel - })); - return true; - } - else - { - return false; - } - } + MajorVersion = nb.MajorVersion, + MinorVersion = nb.MinorVersion, + Number = nb.Number, + Revision = nb.Revision, + Lab = nb.Lab, + BuildTime = nb.BuildTime, + FlightLevel = nb.FlightLevel + })); + return true; + } + else + { + return false; + } + } - public IEnumerable GetSearchResult(string query) - { - if (string.IsNullOrWhiteSpace(query)) - { - return new SearchResult[0]; - } + public IEnumerable GetSearchResult(string query) + { + if (string.IsNullOrWhiteSpace(query)) + { + return new SearchResult[0]; + } - List results = new List(); + List results = new List(); - var sourceResults = from s in Enum.GetValues(typeof(TypeOfSource)).Cast().Select(s => new { Text = DisplayHelpers.GetDisplayTextForEnum(s), Value = s }) - where s.Text.ToLower().Contains(query.ToLower()) - orderby s.Text.ToLower().IndexOf(query.ToLower(), StringComparison.Ordinal) ascending - select new SearchResult() - { - Url = Url.Route("Source Root", new { controller = "front", action = "viewSource", source = s.Value }), - Label = s.Text.Replace(query, "" + query + ""), - Title = s.Text, - Group = "Source" - }; - - results.AddRange(sourceResults); - - - var versionResults = from v in new Build().SelectBuildVersions() - where $"{v.Major}.{v.Minor}".StartsWith(query) - orderby v.Major descending, v.Minor descending - select new SearchResult() - { - Url = Url.Route("Version Root", new { controller = "front", action = "viewVersion", major = v.Major, minor = v.Minor }), - Label = $"{v.Major}.{v.Minor}".Replace(query, "" + query + ""), - Title = "", - Group = "Version" - }; - - results.AddRange(versionResults); - - - var yearResults = from y in new Build().SelectBuildYears() - where y.ToString().Contains(query) - orderby y descending - select new SearchResult() - { - Url = Url.Route("Year Root", new { controller = "front", action = "viewYear", year = y }), - Label = y.ToString().Replace(query, "" + query + ""), - Title = "", - Group = "Year" - }; - - results.AddRange(yearResults); - - - var labResults = from l in new Build().SelectBuildLabs() - where l.ToLower().Contains(query.ToLower()) - orderby l.ToLower().IndexOf(query.ToLower(), StringComparison.Ordinal) ascending + var sourceResults = from s in Enum.GetValues(typeof(TypeOfSource)).Cast().Select(s => new { Text = DisplayHelpers.GetDisplayTextForEnum(s), Value = s }) + where s.Text.ToLower().Contains(query.ToLower()) + orderby s.Text.ToLower().IndexOf(query.ToLower(), StringComparison.Ordinal) ascending select new SearchResult() { - Url = Url.Route("Lab Root", new { controller = "front", action = "viewLab", lab = l }), - Label = l.Replace(query, "" + query + ""), - Title = l, - Group = "Lab" + Url = Url.Route("Source Root", new { controller = "front", action = "viewSource", source = s.Value }), + Label = s.Text.Replace(query, "" + query + ""), + Title = s.Text, + Group = Common.SearchSource }; - results.AddRange(labResults); + results.AddRange(sourceResults); - var buildResults = from b in new Build().Select() - where b.FullBuildString.ToLower().Contains(query.ToLower()) - orderby b.FullBuildString.ToLower().IndexOf(query.ToLower(), StringComparison.Ordinal) ascending, - b.BuildTime descending - select new SearchResult() - { - Url = Url.Route("Build", new { controller = "front", action = "viewBuild", id = b.Id }), - Label = b.FullBuildString.Replace(query, "" + query + ""), - Title = b.FullBuildString, - Group = "Build" - }; + var versionResults = from v in bModel.SelectBuildVersions() + where $"{v.Major}.{v.Minor}".StartsWith(query) + orderby v.Major descending, v.Minor descending + select new SearchResult() + { + Url = Url.Route("Version Root", new { controller = "front", action = "viewVersion", major = v.Major, minor = v.Minor }), + Label = $"{v.Major}.{v.Minor}".Replace(query, "" + query + ""), + Title = "", + Group = Common.SearchVersion + }; - results.AddRange(buildResults); + results.AddRange(versionResults); - if (results.Count == 0) + var yearResults = from y in bModel.SelectBuildYears() + where y.ToString().Contains(query) + orderby y descending + select new SearchResult() + { + Url = Url.Route("Year Root", new { controller = "front", action = "viewYear", year = y }), + Label = y.ToString().Replace(query, "" + query + ""), + Title = "", + Group = Common.SearchYear + }; + + results.AddRange(yearResults); + + + var labResults = from l in bModel.SearchBuildLabs(query) + orderby l.IndexOf(query.ToLower()) ascending, + l.Length ascending + select new SearchResult() + { + Url = Url.Route("Lab Root", new { controller = "front", action = "viewLab", lab = l }), + Label = l.Replace(query, $"{query}"), + Title = l, + Group = Common.SearchLab + }; + + results.AddRange(labResults); + + + var buildResults = from b in bModel.Select() + where b.FullBuildString.ToLower().Contains(query.ToLower()) + orderby b.FullBuildString.ToLower().IndexOf(query.ToLower(), StringComparison.Ordinal) ascending, + b.BuildTime descending + select new SearchResult() + { + Url = Url.Route("Build", new { controller = "front", action = "viewBuild", id = b.Id }), + Label = b.FullBuildString.Replace(query, $"{query}"), + Title = b.FullBuildString, + Group = Common.SearchBuild + }; + + results.AddRange(buildResults); + + + if (results.Count == 0) + { + results.Add(new SearchResult() { - results.Add(new SearchResult() - { - Url = "/", - Label = "No Results found", - Group = "" - }); - } + Url = "/", + Label = Common.SearchEmpty, + Group = "" + }); + } - return results.Take(15); - } - } + return results.Take(15); + } + } } diff --git a/BuildFeed/Local/Common.Designer.cs b/BuildFeed/Local/Common.Designer.cs index ff33cd1..8342794 100644 --- a/BuildFeed/Local/Common.Designer.cs +++ b/BuildFeed/Local/Common.Designer.cs @@ -159,6 +159,15 @@ public static string Search { } } + /// + /// Looks up a localized string similar to Build. + /// + public static string SearchBuild { + get { + return ResourceManager.GetString("SearchBuild", resourceCulture); + } + } + /// /// Looks up a localized string similar to Search BuildFeed.... /// @@ -168,6 +177,24 @@ public static string SearchBuildFeed { } } + /// + /// Looks up a localized string similar to No results found. + /// + public static string SearchEmpty { + get { + return ResourceManager.GetString("SearchEmpty", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Lab. + /// + public static string SearchLab { + get { + return ResourceManager.GetString("SearchLab", resourceCulture); + } + } + /// /// Looks up a localized string similar to Search query. /// @@ -177,6 +204,33 @@ public static string SearchQuery { } } + /// + /// Looks up a localized string similar to Source. + /// + public static string SearchSource { + get { + return ResourceManager.GetString("SearchSource", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Version. + /// + public static string SearchVersion { + get { + return ResourceManager.GetString("SearchVersion", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Year. + /// + public static string SearchYear { + get { + return ResourceManager.GetString("SearchYear", resourceCulture); + } + } + /// /// Looks up a localized string similar to Sitemap. /// diff --git a/BuildFeed/Local/Common.qps-ploc.resx b/BuildFeed/Local/Common.qps-ploc.resx index a305d99..6fcb093 100644 --- a/BuildFeed/Local/Common.qps-ploc.resx +++ b/BuildFeed/Local/Common.qps-ploc.resx @@ -174,4 +174,22 @@ [!!! | Þáϱè {0} !!!] + + [!!! ßúïℓδ ℓ !!!] + + + [!!! Nô řèƨúℓƭƨ ƒôúñδ ℓôřè !!!] + + + [!!! £áβ !!!] + + + [!!! §ôúrçè l !!!] + + + [!!! Vèřƨïôñ ℓ !!!] + + + [!!! Ýèář !!!] + \ No newline at end of file diff --git a/BuildFeed/Local/Common.resx b/BuildFeed/Local/Common.resx index 659bf5c..30abd07 100644 --- a/BuildFeed/Local/Common.resx +++ b/BuildFeed/Local/Common.resx @@ -174,4 +174,22 @@ | Page {0} + + Build + + + No results found + + + Lab + + + Source + + + Version + + + Year + \ No newline at end of file diff --git a/BuildFeed/Models/Build.cs b/BuildFeed/Models/Build.cs index 65ba772..2bdb8fd 100644 --- a/BuildFeed/Models/Build.cs +++ b/BuildFeed/Models/Build.cs @@ -1,4 +1,5 @@ using BuildFeed.Local; +using BuildFeed.Models.ApiModel; using BuildFeed.Models.ViewModel.Front; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; @@ -350,45 +351,97 @@ public long SelectVersionCount(int major, int minor) } [DataObjectMethod(DataObjectMethodType.Select, false)] - public IEnumerable SelectBuildVersions() + public List SelectBuildVersions() { - var task = _buildCollection.DistinctAsync(b => new BuildVersion() { Major = b.MajorVersion, Minor = b.MinorVersion }, b => true); + var task = _buildCollection.Aggregate() + .Group(b => new BuildVersion() + { + Major = b.MajorVersion, + Minor = b.MinorVersion, + }, + // incoming bullshit hack + bg => new Tuple(bg.Key)) + .SortByDescending(b => b.Item1.Major) + .ThenByDescending(b => b.Item1.Minor) + .ToListAsync(); + task.Wait(); - var outTask = task.Result.ToListAsync(); - outTask.Wait(); - return outTask.Result - .OrderByDescending(y => y.Major) - .ThenByDescending(y => y.Minor); + + // work ourselves out of aforementioned bullshit hack + return task.Result.Select(b => b.Item1).ToList(); } [DataObjectMethod(DataObjectMethodType.Select, false)] public IEnumerable SelectBuildYears() { - var task = _buildCollection.DistinctAsync(b => b.BuildTime.Value.Year, b => b.BuildTime.HasValue); + var task = _buildCollection.Aggregate() + .Match(b => b.BuildTime != null) + .Group(b => ((DateTime)b.BuildTime).Year, + // incoming bullshit hack + bg => new Tuple(bg.Key)) + .SortByDescending(b => b.Item1) + .ToListAsync(); + task.Wait(); - var outTask = task.Result.ToListAsync(); - outTask.Wait(); - return outTask.Result.OrderBy(b => b); + + // work ourselves out of aforementioned bullshit hack + return task.Result.Select(b => b.Item1).ToList(); + } + + [DataObjectMethod(DataObjectMethodType.Select, false)] + public List SearchBuildLabs(string query) + { + var task = _buildCollection.Aggregate() + .Match(b => b.Lab != null) + .Match(b => b.Lab != "") + .Match(b => b.Lab.ToLower().Contains(query.ToLower())) + .Group(b => b.Lab.ToLower(), + // incoming bullshit hack + bg => new Tuple(bg.Key)) + .ToListAsync(); + + task.Wait(); + + // work ourselves out of aforementioned bullshit hack + return task.Result.Select(b => b.Item1).ToList(); } [DataObjectMethod(DataObjectMethodType.Select, false)] public IEnumerable SelectBuildLabs() { - var task = _buildCollection.DistinctAsync(b => b.Lab.ToLower(), b => !string.IsNullOrWhiteSpace(b.Lab)); + var task = _buildCollection.Aggregate() + .Match(b => b.Lab != null) + .Match(b => b.Lab != "") + .Group(b => b.Lab.ToLower(), + // incoming bullshit hack + bg => new Tuple(bg.Key)) + .SortBy(b => b.Item1) + .ToListAsync(); + task.Wait(); - var outTask = task.Result.ToListAsync(); - outTask.Wait(); - return outTask.Result.OrderBy(b => b); + + // work ourselves out of aforementioned bullshit hack + return task.Result.Select(b => b.Item1).ToList(); } [DataObjectMethod(DataObjectMethodType.Select, false)] public IEnumerable SelectBuildLabs(byte major, byte minor) { - var task = _buildCollection.DistinctAsync(b => b.Lab.ToLower(), b => !string.IsNullOrWhiteSpace(b.Lab) && b.MajorVersion == major && b.MinorVersion == minor); + var task = _buildCollection.Aggregate() + .Match(b => b.MajorVersion == major) + .Match(b => b.MinorVersion == minor) + .Match(b => b.Lab != null) + .Match(b => b.Lab != "") + .Group(b => b.Lab.ToLower(), + // incoming bullshit hack + bg => new Tuple(bg.Key)) + .SortBy(b => b.Item1) + .ToListAsync(); + task.Wait(); - var outTask = task.Result.ToListAsync(); - outTask.Wait(); - return outTask.Result.OrderBy(b => b); + + // work ourselves out of aforementioned bullshit hack + return task.Result.Select(b => b.Item1).ToList(); } [DataObjectMethod(DataObjectMethodType.Insert, true)] @@ -470,11 +523,21 @@ public enum LevelOfFlight High = 3 } - public struct BuildVersion + public class BuildVersion { public byte Major { get; set; } public byte Minor { get; set; } + public BuildVersion() + { + } + + public BuildVersion(byte major, byte minor) + { + Major = major; + Minor = minor; + } + public override string ToString() { return $"{Major}.{Minor}";