Skip to content

Commit

Permalink
Fixed major bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdes committed Jan 24, 2022
1 parent 768a66f commit 859585c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 49 deletions.
12 changes: 12 additions & 0 deletions Library.Encyclopedia.API/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "6.0.1",
"commands": [
"dotnet-ef"
]
}
}
}
4 changes: 3 additions & 1 deletion Library.Encyclopedia.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
115 changes: 67 additions & 48 deletions Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,30 @@ public MainDataAccess(IApplicationDbContext dbcontext, IConfiguration configurat
#region GET
async Task<MainMinimizedExternalCollection> 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<Main> 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;
}

Expand All @@ -62,20 +61,26 @@ async Task<MainMinimizedExternalCollection> IMainDataAccess.GetByCategoryAsync(s
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);
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);

Expand All @@ -100,9 +105,8 @@ async Task<MainMinimizedExternalCollection> 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") ||
Expand All @@ -116,20 +120,22 @@ async Task<MainMinimizedExternalCollection> 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<Main> 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);
Expand Down Expand Up @@ -163,23 +169,23 @@ public async Task<Guid> CreateAsync(Main model)
model.Id = Guid.NewGuid();


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

if (model.RichDescription != null)
{
Expand All @@ -204,6 +210,7 @@ private List<Links> ReplaceInternalLinks(Main model)
}

var newDesc = new string(model.RichDescription);
var newDesc2 = new string(model.RichDescription);

for (int i = 0; i < startIndexes.Count; i++)
{
Expand All @@ -214,9 +221,10 @@ private List<Links> 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,
Expand All @@ -229,9 +237,10 @@ private List<Links> ReplaceInternalLinks(Main model)
}

model.RichDescription = newDesc;
model.RawDescription = newDesc2;
}

return links;
return model;
}
#endregion

Expand All @@ -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);
Expand Down

0 comments on commit 859585c

Please sign in to comment.