From 859585cf6d942468d30802cd99dfc8ab1fe15306 Mon Sep 17 00:00:00 2001 From: Souvik Mazumder Date: Mon, 24 Jan 2022 02:58:35 -0500 Subject: [PATCH] Fixed major bugs --- .../.config/dotnet-tools.json | 12 ++ Library.Encyclopedia.API/appsettings.json | 4 +- .../DataAccess/MainDataAccess.cs | 115 ++++++++++-------- 3 files changed, 82 insertions(+), 49 deletions(-) create mode 100644 Library.Encyclopedia.API/.config/dotnet-tools.json diff --git a/Library.Encyclopedia.API/.config/dotnet-tools.json b/Library.Encyclopedia.API/.config/dotnet-tools.json new file mode 100644 index 0000000..0d1da73 --- /dev/null +++ b/Library.Encyclopedia.API/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "6.0.1", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/Library.Encyclopedia.API/appsettings.json b/Library.Encyclopedia.API/appsettings.json index 7b695ce..aa3949b 100644 --- a/Library.Encyclopedia.API/appsettings.json +++ b/Library.Encyclopedia.API/appsettings.json @@ -6,10 +6,12 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "App-Base-Url": "http://localhost:4200", + //"App-Base-Url": "http://localhost:4200", + "App-Base-Url": "https://tools.library.pfw.edu/encyclopedia", "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Server=localhost;Database=Encyclopedia;User=root;Password=root" + //"DefaultConnection": "Server=localhost;Database=Encyclopedia;User=root;Password=RW_qh+-ta5hW*2s" }, "ApiKey": "5929b003-8895-4fb3-bbb0-2eb101c48f66", "FTPSettings": { diff --git a/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs b/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs index ff8696f..b405de9 100644 --- a/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs +++ b/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs @@ -29,31 +29,30 @@ public MainDataAccess(IApplicationDbContext dbcontext, IConfiguration configurat #region GET async Task IMainDataAccess.GetAsync(string query, int offset, int pagesize, int previewSize, bool ascending) { - // random cleanup - //await CleanUpData(); - query = query != null ? query.ToLower() : string.Empty; - var temp = _dbcontext.Main.Where(s => s.RawDescription.ToLower().Contains(query) || s.Title.ToLower().Contains(query)) - .Skip(offset) - .Take(pagesize) - .Include(s => s.Links); - IEnumerable
data; + if (ascending) - data = await temp.OrderBy(s => s.Title) - .ThenBy(s => s.RawDescription) - .ToListAsync(); + data = await _dbcontext.Main.Where(s => s.RawDescription.ToLower().Contains(query) || s.Title.ToLower().Contains(query)) + .OrderBy(s => s.Title.ToLower()) + .ThenBy(s => s.RawDescription.ToLower()) + .Skip(offset) + .Take(pagesize) + .Include(s => s.Links) + .ToListAsync(); else - data = await temp.OrderByDescending(s => s.Title) - .ThenByDescending(s => s.RawDescription) - .ToListAsync(); + data = await _dbcontext.Main.Where(s => s.RawDescription.ToLower().Contains(query) || s.Title.ToLower().Contains(query)) + .OrderByDescending(s => s.Title.ToLower()) + .ThenByDescending(s => s.RawDescription.ToLower()) + .Skip(offset) + .Take(pagesize) + .Include(s => s.Links) + .ToListAsync(); var total = await _dbcontext.Main.CountAsync(s => s.RawDescription.ToLower().Contains(query) || s.Title.ToLower().Contains(query)); MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.MinimizeWithQuery(query, previewSize), total); - - return result; } @@ -62,20 +61,26 @@ async Task IMainDataAccess.GetByCategoryAsync(s if (!string.IsNullOrEmpty(category)) { category = category.ToLower(); - List
rawData = await _dbcontext.Main.ToListAsync(); - var temp = rawData.Where(s => s.Category != null).Where(s => s.Category.ToLower().Split(',', StringSplitOptions.None).Contains(category)) - .Skip(offset) - .Take(pagesize); IEnumerable
data; if (ascending) - data = temp.OrderBy(s => s.Title) - .ThenBy(s => s.RawDescription); + data = await _dbcontext.Main.Where(s => s.Category != null) + .Where(s => s.Category.ToLower().Contains(category + ",") || s.Category.ToLower().Contains("," + category) || s.Category.ToLower() == category) + .OrderBy(s => s.Title.ToLower()) + .ThenBy(s => s.RawDescription.ToLower()) + .Skip(offset) + .Take(pagesize) + .ToListAsync(); else - data = temp.OrderByDescending(s => s.Title) - .ThenByDescending(s => s.RawDescription); + data = await _dbcontext.Main.Where(s => s.Category != null) + .Where(s => s.Category.ToLower().Contains(category + ",") || s.Category.ToLower().Contains("," + category) || s.Category.ToLower() == category) + .OrderByDescending(s => s.Title.ToLower()) + .ThenByDescending(s => s.RawDescription.ToLower()) + .Skip(offset) + .Take(pagesize) + .ToListAsync(); - var total = rawData.Where(s => s.Category != null).Count(s => s.Category.ToLower().Split(',', StringSplitOptions.None).Contains(category)); + var total = await _dbcontext.Main.Where(s => s.Category != null).CountAsync(s => s.Category.ToLower().Contains(category + ",") || s.Category.ToLower().Contains("," + category) || s.Category.ToLower() == category); MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(previewSize), total); @@ -100,9 +105,8 @@ async Task IMainDataAccess.GetByAlphabetAsync(c s.Title.StartsWith("6") || s.Title.StartsWith("7") || s.Title.StartsWith("8") || - s.Title.StartsWith("9")) - .Skip(offset) - .Take(pagesize); + s.Title.StartsWith("9")); + total = await _dbcontext.Main.CountAsync(s => s.Title.StartsWith("0") || s.Title.StartsWith("1") || s.Title.StartsWith("2") || @@ -116,20 +120,22 @@ async Task IMainDataAccess.GetByAlphabetAsync(c } else { - temp = _dbcontext.Main.Where(s => s.Title.ToLower().StartsWith(startingAlphabet.ToString().ToLower())) - .Skip(offset) - .Take(pagesize); + temp = _dbcontext.Main.Where(s => s.Title.ToLower().StartsWith(startingAlphabet.ToString().ToLower())); total = await _dbcontext.Main.CountAsync(s => s.Title.ToLower().StartsWith(startingAlphabet.ToString().ToLower())); } IEnumerable
data; if (ascending) - data = await temp.OrderBy(s => s.Title) - .ThenBy(s => s.RawDescription) + data = await temp.OrderBy(s => s.Title.ToLower()) + .ThenBy(s => s.RawDescription.ToLower()) + .Skip(offset) + .Take(pagesize) .ToListAsync(); else - data = await temp.OrderByDescending(s => s.Title) - .ThenByDescending(s => s.RawDescription) + data = await temp.OrderByDescending(s => s.Title.ToLower()) + .ThenByDescending(s => s.RawDescription.ToLower()) + .Skip(offset) + .Take(pagesize) .ToListAsync(); MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(previewSize), total); @@ -163,23 +169,23 @@ public async Task CreateAsync(Main model) model.Id = Guid.NewGuid(); - List links = ReplaceInternalLinks(model); + model = ReplaceInternalLinks(model); //santize data - model.RawDescription = StripHTML(model.RichDescription); + model.RawDescription = StripHTML(model.RawDescription); await _dbcontext.Main.AddAsync(model); - await _dbcontext.Links.AddRangeAsync(links); + await _dbcontext.Links.AddRangeAsync(model.Links); await _dbcontext.SaveChanges(); return model.Id; } - private List ReplaceInternalLinks(Main model) + private Main ReplaceInternalLinks(Main model) { string searchStartString = ""; - var links = new List(); + model.Links = new List(); if (model.RichDescription != null) { @@ -204,6 +210,7 @@ private List ReplaceInternalLinks(Main model) } var newDesc = new string(model.RichDescription); + var newDesc2 = new string(model.RichDescription); for (int i = 0; i < startIndexes.Count; i++) { @@ -214,9 +221,10 @@ private List ReplaceInternalLinks(Main model) var desc = value.Substring(value.IndexOf(">") + 1, value.IndexOf("<", value.IndexOf(">")) - value.IndexOf(">") - 1); newDesc = newDesc.Replace(value, $"$$${id}$$$"); + newDesc2 = newDesc2.Replace(value, desc); // add to links list - links.Add(new Links + model.Links.Add(new Links { Id = Guid.NewGuid(), MainId = model.Id, @@ -229,9 +237,10 @@ private List ReplaceInternalLinks(Main model) } model.RichDescription = newDesc; + model.RawDescription = newDesc2; } - return links; + return model; } #endregion @@ -243,15 +252,25 @@ public async Task UpdateAsync(Guid id, MainUpdateModel model) if (entry != null) { - entry.Title = model.Title; - entry.RichDescription = model.RichDescription; + if (model != null) + { + entry.Title = model.Title ?? entry.Title; - // TODO : do conversion on raw description + if (model.RichDescription != null) + { + entry.RichDescription = model.RichDescription; - entry.Category = model.Category; + entry = ReplaceInternalLinks(entry); - _dbcontext.Main.Update(entry); - await _dbcontext.SaveChanges(); + //santize data + entry.RawDescription = StripHTML(entry.RawDescription); + } + + entry.Category = model.Category ?? entry.Category; + + _dbcontext.Main.Update(entry); + await _dbcontext.SaveChanges(); + } } else throw new UpdateFailedException(UpdateFailErrorCode.EntryNotFound);