Skip to content

Commit

Permalink
Cleanedup data
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdes committed Dec 15, 2021
1 parent d07af5e commit cc89517
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +51,6 @@ public async Task<IActionResult> Get(string query,
try
{
var response = await mainDataAccess.GetAsync(query, offset, size, asc);

if (response == null)
{
return StatusCode(204);
Expand Down Expand Up @@ -168,6 +168,7 @@ public async Task<IActionResult> Get(Guid id)
/// <returns></returns>
[HttpPost]
[ApiKey]
//[Authorize]
public async Task<IActionResult> Create([FromBody]Main model)
{
try
Expand Down Expand Up @@ -196,6 +197,7 @@ public async Task<IActionResult> Create([FromBody]Main model)
/// <returns></returns>
[HttpPut("{id}")]
[ApiKey]
//[Authorize]
public async Task<IActionResult> Update(Guid id, [FromBody]MainUpdateModel model)
{
try
Expand All @@ -217,6 +219,7 @@ public async Task<IActionResult> Update(Guid id, [FromBody]MainUpdateModel model
/// <returns></returns>
[HttpDelete("{id}")]
[ApiKey]
//[Authorize]
public async Task<IActionResult> Delete(Guid id)
{
try
Expand Down
5 changes: 4 additions & 1 deletion Library.Encyclopedia.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =>
Expand Down Expand Up @@ -62,7 +64,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}

app.UseRouting();

//app.UseAuthentication();
//app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
Expand Down
83 changes: 83 additions & 0 deletions Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,6 +43,9 @@ async Task<MainMinimizedExternalCollection> IMainDataAccess.GetAsync(string quer

MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(), total);

// random cleanup
await CleanUpData();

return result;
}

Expand Down Expand Up @@ -157,6 +162,84 @@ public async Task<IEnumerable<string>> GetAllCategoriesAsync()
});
return result.Distinct();
}

/// <summary>
/// This was created to clean up the existing data
/// Is disabled currently as cleanup is done
/// </summary>
/// <returns></returns>
public async Task CleanUpData()
{
string searchStartString = "<a href=";
string searchEndString = "</a>";
var links = new List<Links>();
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<int> startIndexes = new List<int>();
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<int> endIndexes = new List<int>();
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
}
}
12 changes: 12 additions & 0 deletions Library.Encyclopedia.Entity/Models/PFWCredentials.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}

0 comments on commit cc89517

Please sign in to comment.