Skip to content

Commit

Permalink
working website
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdes committed Jan 23, 2022
1 parent eeba115 commit 768a66f
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 88 deletions.
21 changes: 11 additions & 10 deletions Library.Encyclopedia.API/Controllers/EncylopediaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ public async Task<IActionResult> Get(string query,
[HttpGet("category")]
public async Task<IActionResult> 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)
{
Expand Down Expand Up @@ -114,12 +115,12 @@ public async Task<IActionResult> GetByCategory(string category,
[HttpGet("alphabet")]
public async Task<IActionResult> 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)
{
Expand Down Expand Up @@ -169,8 +170,8 @@ public async Task<IActionResult> Get(Guid id)
/// </summary>
/// <returns></returns>
[HttpPost]
[ApiKey]
//[Authorize]
//[ApiKey]
[Authorize(Roles = @"EncyclopediaAdministrators")]
public async Task<IActionResult> Create([FromBody]Main model)
{
try
Expand Down Expand Up @@ -198,8 +199,8 @@ public async Task<IActionResult> Create([FromBody]Main model)
/// </summary>
/// <returns></returns>
[HttpPut("{id}")]
[ApiKey]
//[Authorize]
//[ApiKey]
[Authorize(Roles = @"EncyclopediaAdministrators")]
public async Task<IActionResult> Update(Guid id, [FromBody]MainUpdateModel model)
{
try
Expand All @@ -220,8 +221,8 @@ public async Task<IActionResult> Update(Guid id, [FromBody]MainUpdateModel model
/// </summary>
/// <returns></returns>
[HttpDelete("{id}")]
[ApiKey]
//[Authorize]
//[ApiKey]
[Authorize(Roles = @"EncyclopediaAdministrators")]
public async Task<IActionResult> Delete(Guid id)
{
try
Expand Down
2 changes: 1 addition & 1 deletion Library.Encyclopedia.API/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"iisSettings": {
"windowsAuthentication": false,
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:63327",
Expand Down
12 changes: 7 additions & 5 deletions 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.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -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<string>("FTPSettings:url");
Expand Down Expand Up @@ -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(
Expand Down
156 changes: 130 additions & 26 deletions Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,70 @@ async Task<MainMinimizedExternalCollection> IMainDataAccess.GetAsync(string quer
return result;
}

async Task<MainMinimizedExternalCollection> IMainDataAccess.GetByCategoryAsync(string category, int offset, int pagesize, bool ascending)
async Task<MainMinimizedExternalCollection> IMainDataAccess.GetByCategoryAsync(string category, int offset, int pagesize, int previewSize, bool ascending)
{
category = category.ToLower();
List<Main> 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<Main> 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<Main> 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<Main> 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<MainMinimizedExternalCollection> IMainDataAccess.GetByAlphabetAsync(char startingAlphabet, int offset, int pagesize, bool ascending)
async Task<MainMinimizedExternalCollection> 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<Main> 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<Main> data;
if (ascending)
Expand All @@ -97,9 +132,7 @@ async Task<MainMinimizedExternalCollection> 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;
}
Expand All @@ -116,7 +149,7 @@ async Task<Main> IMainDataAccess.GetAsync(Guid id)

var referenceid = item.RichDescription.Substring(startIndex, endIndex);

item.RichDescription= item.RichDescription.Replace($"$$${referenceid}$$$", $"<a href=\"{APP_BASE_URL}/{referenceid}\">{item.Links.FirstOrDefault(s => s.ReferenceId.ToString() == referenceid).Description}</a>");
item.RichDescription = item.RichDescription.Replace($"$$${referenceid}$$$", $"<a href=\"{APP_BASE_URL}/{referenceid}\">{item.Links.FirstOrDefault(s => s.ReferenceId.ToString() == referenceid).Description}</a>");
}

return item;
Expand All @@ -130,10 +163,76 @@ public async Task<Guid> CreateAsync(Main model)
model.Id = Guid.NewGuid();


List<Links> 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<Links> ReplaceInternalLinks(Main model)
{
string searchStartString = "<a href=";
string searchEndString = "</a>";
var links = new List<Links>();

if (model.RichDescription != null)
{
// Extract start indices
List<int> startIndexes = new List<int>();
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<int> endIndexes = new List<int>();
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
Expand Down Expand Up @@ -182,8 +281,13 @@ public async Task<IEnumerable<string>> GetAllCategoriesAsync()
var result = new List<string>();
data.ForEach(item =>
{
result.AddRange(item.Split(','));
if (!string.IsNullOrEmpty(item))
{
result.AddRange(item.Split(','));
}
});

result.RemoveAll(s => s == string.Empty);
return result.Distinct();
}

Expand Down
4 changes: 2 additions & 2 deletions Library.Encyclopedia.Entity/Interfaces/IMainDataAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public interface IMainDataAccess
{
#region GET
public Task<MainMinimizedExternalCollection> GetAsync(string query, int offset, int pagesize, int previewSize, bool ascending);
public Task<MainMinimizedExternalCollection> GetByCategoryAsync(string category, int offset, int pagesize, bool ascending);
public Task<MainMinimizedExternalCollection> GetByAlphabetAsync(char startingAlphabet, int offset, int pagesize, bool ascending);
public Task<MainMinimizedExternalCollection> GetByCategoryAsync(string category, int offset, int pagesize, int previewSize, bool ascending);
public Task<MainMinimizedExternalCollection> GetByAlphabetAsync(char startingAlphabet, int offset, int pagesize, int previewSize, bool ascending);
public Task<Main> GetAsync(Guid id);
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public class MainMinimizedExternal
}
public class MainMinimizedExternalCollection : QueryExternalModel<MainMinimizedExternal>
{
public MainMinimizedExternalCollection()
{
this.Count = 0;
this.Result = new List<MainMinimizedExternal>();
this.Total = 0;
}
public MainMinimizedExternalCollection(IEnumerable<MainMinimizedExternal> collection, int total)
{
this.Result = collection;
Expand Down
Loading

0 comments on commit 768a66f

Please sign in to comment.