diff --git a/Library.Encyclopedia.API/Controllers/EncylopediaController.cs b/Library.Encyclopedia.API/Controllers/EncylopediaController.cs index 4ea4134..8d264b4 100644 --- a/Library.Encyclopedia.API/Controllers/EncylopediaController.cs +++ b/Library.Encyclopedia.API/Controllers/EncylopediaController.cs @@ -4,6 +4,7 @@ using Library.Encyclopedia.Entity.Interfaces; using Library.Encyclopedia.Entity.Models; using Library.Encyclopedia.Entity.Models.External; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; @@ -50,7 +51,6 @@ public async Task Get(string query, try { var response = await mainDataAccess.GetAsync(query, offset, size, asc); - if (response == null) { return StatusCode(204); @@ -168,6 +168,7 @@ public async Task Get(Guid id) /// [HttpPost] [ApiKey] + //[Authorize] public async Task Create([FromBody]Main model) { try @@ -196,6 +197,7 @@ public async Task Create([FromBody]Main model) /// [HttpPut("{id}")] [ApiKey] + //[Authorize] public async Task Update(Guid id, [FromBody]MainUpdateModel model) { try @@ -217,6 +219,7 @@ public async Task Update(Guid id, [FromBody]MainUpdateModel model /// [HttpDelete("{id}")] [ApiKey] + //[Authorize] public async Task Delete(Guid id) { try diff --git a/Library.Encyclopedia.API/Startup.cs b/Library.Encyclopedia.API/Startup.cs index dff2820..17530f6 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.AspNetCore.SpaServices.AngularCli; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -22,6 +23,7 @@ public Startup(IConfiguration configuration) // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + //services.AddAuthentication(IISDefaults.AuthenticationScheme); services.AddControllersWithViews(); // In production, the Angular files will be served from this directory services.AddSpaStaticFiles(configuration => @@ -62,7 +64,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) } app.UseRouting(); - + //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 8bf15de..3374177 100644 --- a/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs +++ b/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs @@ -5,8 +5,10 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Library.Encyclopedia.DataAccess.DataAccess @@ -41,6 +43,9 @@ async Task IMainDataAccess.GetAsync(string quer MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(), total); + // random cleanup + await CleanUpData(); + return result; } @@ -157,6 +162,84 @@ public async Task> GetAllCategoriesAsync() }); return result.Distinct(); } + + /// + /// This was created to clean up the existing data + /// Is disabled currently as cleanup is done + /// + /// + public async Task CleanUpData() + { + string searchStartString = ""; + var links = new List(); + var allData = await _dbcontext.Main.ToListAsync(); + + //using StreamWriter file = new StreamWriter(@"C:\temp\temp.txt", append: true); + foreach (var item in allData) + { + if (item.Description != null) + { + // Extract start indices + List startIndexes = new List(); + for (int index = 0; ; index += searchStartString.Length) + { + index = item.Description.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 = item.Description.IndexOf(searchEndString, index); + if (index != -1) + endIndexes.Add(index); + else break; + } + + // + var newDesc = new string(item.Description); + for (int i = 0; i < startIndexes.Count; i++) + { + string value = item.Description.Substring(startIndexes[i], endIndexes[i] - startIndexes[i] + searchEndString.Length); + if (value.Contains("http://")) + { + var id = value.Substring(value.IndexOf("id=") + 3, 36); + var desc = value.Substring(value.IndexOf(">") + 1, value.IndexOf("<", value.IndexOf(">")) - value.IndexOf(">") - 1); + + //await file.WriteLineAsync($"id = {id} | Description = {desc}"); + + newDesc = newDesc.Replace(value, $"$$${id}$$$"); + //await file.WriteLineAsync($"new description = {newDesc}"); + + // add to links list + links.Add(new Links + { + Id = Guid.NewGuid(), + MainId = Guid.Parse(id), + Link = value, + Description = desc, + IsInternal = true + }); + } + } + + // update the description + item.Description = newDesc; + } + } + + // add to the links table + await _dbcontext.Links.AddRangeAsync(links); + + // update the main data + _dbcontext.Main.UpdateRange(allData); + + await _dbcontext.SaveChanges(); + } #endregion } } \ No newline at end of file diff --git a/Library.Encyclopedia.Entity/Models/PFWCredentials.cs b/Library.Encyclopedia.Entity/Models/PFWCredentials.cs new file mode 100644 index 0000000..a230719 --- /dev/null +++ b/Library.Encyclopedia.Entity/Models/PFWCredentials.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Library.Encyclopedia.Entity.Models +{ + public class PFWCredentials + { + public string Username { get; set; } + public string Password { get; set; } + } +}