Skip to content

Commit

Permalink
Fixed a UI bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdes committed Jan 24, 2022
1 parent 859585c commit c231867
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
11 changes: 9 additions & 2 deletions Library.Encyclopedia.API/Controllers/EncylopediaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,16 @@ public async Task<IActionResult> Update(Guid id, [FromBody]MainUpdateModel model
{
try
{
await mainDataAccess.UpdateAsync(id, model);
var response = await mainDataAccess.UpdateAsync(id, model);

return Ok();
if (response == null)
{
return StatusCode(500);
}
else
{
return Ok(response);
}
}
catch (Exception ex)
{
Expand Down
4 changes: 2 additions & 2 deletions Library.Encyclopedia.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"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"
//"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
55 changes: 52 additions & 3 deletions Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public async Task<Guid> CreateAsync(Main model)
// assign a random id
model.Id = Guid.NewGuid();


model = ReplaceInternalLinks(model);
model = ReplaceLineBreaks(model);

//santize data
model.RawDescription = StripHTML(model.RawDescription);
Expand All @@ -181,6 +181,51 @@ public async Task<Guid> CreateAsync(Main model)
return model.Id;
}

private Main ReplaceLineBreaks(Main model)
{
string searchStartString = "<p>";
string searchEndString = "</p>";

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 tag = model.RichDescription.Substring(startIndexes[i], endIndexes[i] - startIndexes[i] + searchEndString.Length);
string value = model.RichDescription.Substring(startIndexes[i] + searchStartString.Length, endIndexes[i] - startIndexes[i] - +searchStartString.Length);
if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
{
newDesc = newDesc.Replace(tag, "<br>");
}
}

model.RichDescription = newDesc;
}

return model;
}

private Main ReplaceInternalLinks(Main model)
{
string searchStartString = "<a href=";
Expand Down Expand Up @@ -245,10 +290,10 @@ private Main ReplaceInternalLinks(Main model)
#endregion

#region UPDATE
public async Task UpdateAsync(Guid id, MainUpdateModel model)
public async Task<Main> UpdateAsync(Guid id, MainUpdateModel model)
{
// find the entry
var entry = await _dbcontext.Main.FirstOrDefaultAsync(s => s.Id == id);
var entry = await _dbcontext.Main.Include(s => s.Links).FirstOrDefaultAsync(s => s.Id == id);

if (entry != null)
{
Expand All @@ -261,6 +306,7 @@ public async Task UpdateAsync(Guid id, MainUpdateModel model)
entry.RichDescription = model.RichDescription;

entry = ReplaceInternalLinks(entry);
entry = ReplaceLineBreaks(entry);

//santize data
entry.RawDescription = StripHTML(entry.RawDescription);
Expand All @@ -270,10 +316,13 @@ public async Task UpdateAsync(Guid id, MainUpdateModel model)

_dbcontext.Main.Update(entry);
await _dbcontext.SaveChanges();
return entry;
}
}
else
throw new UpdateFailedException(UpdateFailErrorCode.EntryNotFound);

return null;
}
#endregion

Expand Down
2 changes: 1 addition & 1 deletion Library.Encyclopedia.Entity/Interfaces/IMainDataAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IMainDataAccess


public Task<Guid> CreateAsync(Main main);
public Task UpdateAsync(Guid id, MainUpdateModel model);
public Task<Main> UpdateAsync(Guid id, MainUpdateModel model);
public Task DeleteAsync(Guid id);

public Task<IEnumerable<string>> GetAllCategoriesAsync();
Expand Down

0 comments on commit c231867

Please sign in to comment.