From 768a66fb45d1b91eb5d304d920ab422c326553f9 Mon Sep 17 00:00:00 2001 From: Souvik Mazumder Date: Sun, 23 Jan 2022 02:28:29 -0500 Subject: [PATCH] working website --- .../Controllers/EncylopediaController.cs | 21 +-- .../Properties/launchSettings.json | 2 +- Library.Encyclopedia.API/Startup.cs | 12 +- .../DataAccess/MainDataAccess.cs | 156 +++++++++++++++--- .../Interfaces/IMainDataAccess.cs | 4 +- .../Models/External/MainMinimizedExternal.cs | 6 + Library.Encyclopedia.Entity/Models/Main.cs | 92 ++++++----- 7 files changed, 205 insertions(+), 88 deletions(-) diff --git a/Library.Encyclopedia.API/Controllers/EncylopediaController.cs b/Library.Encyclopedia.API/Controllers/EncylopediaController.cs index 70a47ba..e7c80ac 100644 --- a/Library.Encyclopedia.API/Controllers/EncylopediaController.cs +++ b/Library.Encyclopedia.API/Controllers/EncylopediaController.cs @@ -80,12 +80,13 @@ public async Task Get(string query, [HttpGet("category")] public async Task GetByCategory(string category, int offset = 0, - int limit = 10, + int limit = 10, int previewSize = 50, + bool asc = true) { try { - var response = await mainDataAccess.GetByCategoryAsync(category, offset, limit, asc); + var response = await mainDataAccess.GetByCategoryAsync(category, offset, limit,previewSize, asc); if (response == null) { @@ -114,12 +115,12 @@ public async Task GetByCategory(string category, [HttpGet("alphabet")] public async Task GetByStartingAlphabet(char alphabet, int offset = 0, - int limit = 10, + int limit = 10, int previewSize = 50, bool asc = true) { try { - var response = await mainDataAccess.GetByAlphabetAsync(alphabet, offset, limit, asc); + var response = await mainDataAccess.GetByAlphabetAsync(alphabet, offset, limit,previewSize, asc); if (response == null) { @@ -169,8 +170,8 @@ public async Task Get(Guid id) /// /// [HttpPost] - [ApiKey] - //[Authorize] + //[ApiKey] + [Authorize(Roles = @"EncyclopediaAdministrators")] public async Task Create([FromBody]Main model) { try @@ -198,8 +199,8 @@ public async Task Create([FromBody]Main model) /// /// [HttpPut("{id}")] - [ApiKey] - //[Authorize] + //[ApiKey] + [Authorize(Roles = @"EncyclopediaAdministrators")] public async Task Update(Guid id, [FromBody]MainUpdateModel model) { try @@ -220,8 +221,8 @@ public async Task Update(Guid id, [FromBody]MainUpdateModel model /// /// [HttpDelete("{id}")] - [ApiKey] - //[Authorize] + //[ApiKey] + [Authorize(Roles = @"EncyclopediaAdministrators")] public async Task Delete(Guid id) { try diff --git a/Library.Encyclopedia.API/Properties/launchSettings.json b/Library.Encyclopedia.API/Properties/launchSettings.json index 3984a6a..95445d5 100644 --- a/Library.Encyclopedia.API/Properties/launchSettings.json +++ b/Library.Encyclopedia.API/Properties/launchSettings.json @@ -1,6 +1,6 @@ { "iisSettings": { - "windowsAuthentication": false, + "windowsAuthentication": true, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:63327", diff --git a/Library.Encyclopedia.API/Startup.cs b/Library.Encyclopedia.API/Startup.cs index 5510e7b..07b2104 100644 --- a/Library.Encyclopedia.API/Startup.cs +++ b/Library.Encyclopedia.API/Startup.cs @@ -3,6 +3,7 @@ using Library.Encyclopedia.Entity.Interfaces; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Server.IISIntegration; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -28,12 +29,13 @@ public void ConfigureServices(IServiceCollection services) options.AddPolicy(name: MyAllowSpecificOrigins, builder => { - builder.WithOrigins("http://localhost:4200").AllowAnyHeader(); - builder.WithOrigins("https://tools.library.pfw.edu").AllowAnyHeader(); + builder.WithOrigins("http://localhost:4200").AllowAnyHeader().AllowCredentials().AllowAnyMethod(); + builder.WithOrigins("https://tools.library.pfw.edu").AllowAnyHeader().AllowCredentials().AllowAnyMethod(); }); }); - //services.AddAuthentication(IISDefaults.AuthenticationScheme); + services.AddAuthentication(IISDefaults.AuthenticationScheme); + services.AddControllers(); string url = Configuration.GetValue("FTPSettings:url"); @@ -66,8 +68,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseStaticFiles(); app.UseRouting(); - //app.UseAuthentication(); - //app.UseAuthorization(); + app.UseAuthentication(); + app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( diff --git a/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs b/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs index dba1a0b..ff8696f 100644 --- a/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs +++ b/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs @@ -57,35 +57,70 @@ async Task IMainDataAccess.GetAsync(string quer return result; } - async Task IMainDataAccess.GetByCategoryAsync(string category, int offset, int pagesize, bool ascending) + async Task IMainDataAccess.GetByCategoryAsync(string category, int offset, int pagesize, int previewSize, bool ascending) { - category = category.ToLower(); - List
rawData = await _dbcontext.Main.ToListAsync(); - var temp = rawData.Where(s => s.Category.ToLower().Split(',', StringSplitOptions.None).Contains(category)) - .Skip(offset) - .Take(pagesize); + 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); - else - data = temp.OrderByDescending(s => s.Title) - .ThenByDescending(s => s.RawDescription); + IEnumerable
data; + if (ascending) + data = temp.OrderBy(s => s.Title) + .ThenBy(s => s.RawDescription); + else + data = temp.OrderByDescending(s => s.Title) + .ThenByDescending(s => s.RawDescription); - var total = rawData.Count(s => s.Category.ToLower().Split(',', StringSplitOptions.None).Contains(category)); + var total = rawData.Where(s => s.Category != null).Count(s => s.Category.ToLower().Split(',', StringSplitOptions.None).Contains(category)); - MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(), total); + MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(previewSize), total); - return result; + return result; + } + else return new MainMinimizedExternalCollection(); } - async Task IMainDataAccess.GetByAlphabetAsync(char startingAlphabet, int offset, int pagesize, bool ascending) + async Task IMainDataAccess.GetByAlphabetAsync(char startingAlphabet, int offset, int pagesize, int previewSize, bool ascending) { - var alph = startingAlphabet.ToString().ToLower(); - var temp = _dbcontext.Main.Where(s => s.Title.ToLower().StartsWith(alph)) - .Skip(offset) - .Take(pagesize); + IQueryable
temp; + int total; + + if (startingAlphabet == '#') + { + temp = _dbcontext.Main.Where(s => s.Title.StartsWith("0") || + s.Title.StartsWith("1") || + s.Title.StartsWith("2") || + s.Title.StartsWith("3") || + s.Title.StartsWith("4") || + s.Title.StartsWith("5") || + s.Title.StartsWith("6") || + s.Title.StartsWith("7") || + s.Title.StartsWith("8") || + s.Title.StartsWith("9")) + .Skip(offset) + .Take(pagesize); + total = await _dbcontext.Main.CountAsync(s => s.Title.StartsWith("0") || + s.Title.StartsWith("1") || + s.Title.StartsWith("2") || + s.Title.StartsWith("3") || + s.Title.StartsWith("4") || + s.Title.StartsWith("5") || + s.Title.StartsWith("6") || + s.Title.StartsWith("7") || + s.Title.StartsWith("8") || + s.Title.StartsWith("9")); + } + else + { + temp = _dbcontext.Main.Where(s => s.Title.ToLower().StartsWith(startingAlphabet.ToString().ToLower())) + .Skip(offset) + .Take(pagesize); + total = await _dbcontext.Main.CountAsync(s => s.Title.ToLower().StartsWith(startingAlphabet.ToString().ToLower())); + } IEnumerable
data; if (ascending) @@ -97,9 +132,7 @@ async Task IMainDataAccess.GetByAlphabetAsync(c .ThenByDescending(s => s.RawDescription) .ToListAsync(); - var total = await _dbcontext.Main.CountAsync(s => s.Title.ToLower().StartsWith(alph)); - - MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(), total); + MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(previewSize), total); return result; } @@ -116,7 +149,7 @@ async Task
IMainDataAccess.GetAsync(Guid id) var referenceid = item.RichDescription.Substring(startIndex, endIndex); - item.RichDescription= item.RichDescription.Replace($"$$${referenceid}$$$", $"{item.Links.FirstOrDefault(s => s.ReferenceId.ToString() == referenceid).Description}"); + item.RichDescription = item.RichDescription.Replace($"$$${referenceid}$$$", $"{item.Links.FirstOrDefault(s => s.ReferenceId.ToString() == referenceid).Description}"); } return item; @@ -130,10 +163,76 @@ public async Task CreateAsync(Main model) model.Id = Guid.NewGuid(); + List links = ReplaceInternalLinks(model); + + //santize data + model.RawDescription = StripHTML(model.RichDescription); + await _dbcontext.Main.AddAsync(model); + await _dbcontext.Links.AddRangeAsync(links); + await _dbcontext.SaveChanges(); return model.Id; } + + private List ReplaceInternalLinks(Main model) + { + string searchStartString = ""; + var links = new List(); + + if (model.RichDescription != null) + { + // Extract start indices + List startIndexes = new List(); + for (int index = 0; ; index += searchStartString.Length) + { + index = model.RichDescription.IndexOf(searchStartString, index); + if (index != -1) + startIndexes.Add(index); + else break; + } + + // Extract end indices + List endIndexes = new List(); + for (int index = 0; ; index += searchEndString.Length) + { + index = model.RichDescription.IndexOf(searchEndString, index); + if (index != -1) + endIndexes.Add(index); + else break; + } + + var newDesc = new string(model.RichDescription); + + for (int i = 0; i < startIndexes.Count; i++) + { + string value = model.RichDescription.Substring(startIndexes[i], endIndexes[i] - startIndexes[i] + searchEndString.Length); + if (value.Contains(APP_BASE_URL)) + { + var id = value.Substring(value.IndexOf(APP_BASE_URL) + APP_BASE_URL.Length + 1, 36); + var desc = value.Substring(value.IndexOf(">") + 1, value.IndexOf("<", value.IndexOf(">")) - value.IndexOf(">") - 1); + + newDesc = newDesc.Replace(value, $"$$${id}$$$"); + + // add to links list + links.Add(new Links + { + Id = Guid.NewGuid(), + MainId = model.Id, + ReferenceId = Guid.Parse(id), + Link = value, + Description = desc, + IsInternal = true + }); + } + } + + model.RichDescription = newDesc; + } + + return links; + } #endregion #region UPDATE @@ -182,8 +281,13 @@ public async Task> GetAllCategoriesAsync() var result = new List(); data.ForEach(item => { - result.AddRange(item.Split(',')); + if (!string.IsNullOrEmpty(item)) + { + result.AddRange(item.Split(',')); + } }); + + result.RemoveAll(s => s == string.Empty); return result.Distinct(); } diff --git a/Library.Encyclopedia.Entity/Interfaces/IMainDataAccess.cs b/Library.Encyclopedia.Entity/Interfaces/IMainDataAccess.cs index e50686d..d00e9e8 100644 --- a/Library.Encyclopedia.Entity/Interfaces/IMainDataAccess.cs +++ b/Library.Encyclopedia.Entity/Interfaces/IMainDataAccess.cs @@ -10,8 +10,8 @@ public interface IMainDataAccess { #region GET public Task GetAsync(string query, int offset, int pagesize, int previewSize, bool ascending); - public Task GetByCategoryAsync(string category, int offset, int pagesize, bool ascending); - public Task GetByAlphabetAsync(char startingAlphabet, int offset, int pagesize, bool ascending); + public Task GetByCategoryAsync(string category, int offset, int pagesize, int previewSize, bool ascending); + public Task GetByAlphabetAsync(char startingAlphabet, int offset, int pagesize, int previewSize, bool ascending); public Task
GetAsync(Guid id); #endregion diff --git a/Library.Encyclopedia.Entity/Models/External/MainMinimizedExternal.cs b/Library.Encyclopedia.Entity/Models/External/MainMinimizedExternal.cs index df21a9e..3e36cbf 100644 --- a/Library.Encyclopedia.Entity/Models/External/MainMinimizedExternal.cs +++ b/Library.Encyclopedia.Entity/Models/External/MainMinimizedExternal.cs @@ -12,6 +12,12 @@ public class MainMinimizedExternal } public class MainMinimizedExternalCollection : QueryExternalModel { + public MainMinimizedExternalCollection() + { + this.Count = 0; + this.Result = new List(); + this.Total = 0; + } public MainMinimizedExternalCollection(IEnumerable collection, int total) { this.Result = collection; diff --git a/Library.Encyclopedia.Entity/Models/Main.cs b/Library.Encyclopedia.Entity/Models/Main.cs index 9109bca..ac92950 100644 --- a/Library.Encyclopedia.Entity/Models/Main.cs +++ b/Library.Encyclopedia.Entity/Models/Main.cs @@ -26,63 +26,67 @@ public static class MainExtensions { public static MainMinimizedExternal MinimizeWithQuery(this Main main, string query, int previewText_maxlength) { - if (!string.IsNullOrEmpty(query)) + if (!string.IsNullOrEmpty(main.RawDescription)) { - int indexOfQuery = main.RawDescription.IndexOf(query, StringComparison.OrdinalIgnoreCase); - - // find the complete word - if (indexOfQuery != -1) + if (!string.IsNullOrEmpty(query)) { - for (int i = indexOfQuery; i >= 0; i--) + int indexOfQuery = main.RawDescription.IndexOf(query, StringComparison.OrdinalIgnoreCase); + + // find the complete word + if (indexOfQuery != -1) { - if (char.IsSeparator(main.RawDescription[i])) + for (int i = indexOfQuery; i >= 0; i--) { - indexOfQuery = i; - break; + if (char.IsSeparator(main.RawDescription[i])) + { + indexOfQuery = i; + break; + } } } - } - int lengthOfPreviewText = indexOfQuery == -1 - ? Math.Min(previewText_maxlength, main.RawDescription.Length) - : Math.Min(previewText_maxlength, main.RawDescription.Length - indexOfQuery); + int lengthOfPreviewText = indexOfQuery == -1 + ? Math.Min(previewText_maxlength, main.RawDescription.Length) + : Math.Min(previewText_maxlength, main.RawDescription.Length - indexOfQuery); - return new MainMinimizedExternal + return new MainMinimizedExternal + { + Id = main.Id, + Preview = indexOfQuery == -1 + ? main.RawDescription.Length > previewText_maxlength + ? main.RawDescription.Substring(0, lengthOfPreviewText) + " ..." + : main.RawDescription.Substring(0, lengthOfPreviewText) + : main.RawDescription.Length - indexOfQuery > previewText_maxlength + ? " ... " + main.RawDescription.Substring(indexOfQuery, lengthOfPreviewText) + " ..." + : main.RawDescription.Length > previewText_maxlength + ? " ... " + main.RawDescription.Substring(main.RawDescription.Length - previewText_maxlength, previewText_maxlength) + : main.RawDescription, + Title = main.Title + }; + } + else { - Id = main.Id, - Preview = indexOfQuery == -1 - ? main.RawDescription.Length > previewText_maxlength - ? main.RawDescription.Substring(0, lengthOfPreviewText) + " ..." - : main.RawDescription.Substring(0, lengthOfPreviewText) - : main.RawDescription.Length - indexOfQuery > previewText_maxlength - ? " ... " + main.RawDescription.Substring(indexOfQuery, lengthOfPreviewText) + " ..." - : main.RawDescription.Length > previewText_maxlength - ? " ... " + main.RawDescription.Substring(main.RawDescription.Length - previewText_maxlength, previewText_maxlength) - : main.RawDescription, - Title = main.Title - }; + int lengthOfPreviewText = Math.Min(previewText_maxlength, main.RawDescription.Length); + return new MainMinimizedExternal + { + Id = main.Id, + Preview = main.RawDescription.Length > previewText_maxlength + ? main.RawDescription.Substring(0, lengthOfPreviewText) + " ..." + : main.RawDescription.Substring(0, lengthOfPreviewText), + Title = main.Title + }; + } } else - { - int lengthOfPreviewText = Math.Min(previewText_maxlength, main.RawDescription.Length); - return new MainMinimizedExternal - { + return new MainMinimizedExternal { Id = main.Id, - Preview = main.RawDescription.Length > previewText_maxlength - ? main.RawDescription.Substring(0, lengthOfPreviewText) + " ..." - : main.RawDescription.Substring(0, lengthOfPreviewText), + Preview = string.Empty, Title = main.Title - }; - } + }; } - public static MainMinimizedExternal Minimize(this Main main) + public static MainMinimizedExternal Minimize(this Main main, int previewText_maxlength) { - return new MainMinimizedExternal - { - Id = main.Id, - Preview = main.RawDescription, - Title = main.Title - }; + return main.MinimizeWithQuery(null, previewText_maxlength); } public static IEnumerable MinimizeWithQuery(this IEnumerable
collection, string query, int previewText_maxlength) { @@ -91,11 +95,11 @@ public static IEnumerable MinimizeWithQuery(this IEnumera yield return MinimizeWithQuery(item, query, previewText_maxlength); } } - public static IEnumerable Minimize(this IEnumerable
collection) + public static IEnumerable Minimize(this IEnumerable
collection, int previewText_maxlength) { foreach (var item in collection) { - yield return Minimize(item); + yield return Minimize(item, previewText_maxlength); } } }