From 1f26368913c03159360cad2e01d12ec191d2a900 Mon Sep 17 00:00:00 2001 From: Souvik Mazumder Date: Mon, 24 Jan 2022 20:59:19 -0500 Subject: [PATCH] Added delete. --- .../DataAccess/MainDataAccess.cs | 25 +++++++++++++------ .../Exceptions/GetFailedException.cs | 18 +++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 Library.Encyclopedia.Entity/Exceptions/GetFailedException.cs diff --git a/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs b/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs index bdc7d83..3f7d650 100644 --- a/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs +++ b/Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs @@ -147,15 +147,23 @@ async Task
IMainDataAccess.GetAsync(Guid id) { Main item = await _dbcontext.Main.Include(s => s.Files).Include(s => s.Links).FirstOrDefaultAsync(s => s.Id == id); - // replace links in item - while (item.RichDescription.IndexOf("$$$") != -1) + if (item != null) { - var startIndex = item.RichDescription.IndexOf("$$$") + 3; - var endIndex = item.RichDescription.Substring(startIndex).IndexOf("$$$"); + if (item.RichDescription != null) + { + // replace links in item + while (item.RichDescription.IndexOf("$$$") != -1) + { + var startIndex = item.RichDescription.IndexOf("$$$") + 3; + var endIndex = item.RichDescription.Substring(startIndex).IndexOf("$$$"); - var referenceid = item.RichDescription.Substring(startIndex, endIndex); + var referenceid = item.RichDescription.Substring(startIndex, endIndex); - item.RichDescription = item.RichDescription.Replace($"$$${referenceid}$$$", $"{item.Links.FirstOrDefault(s => s.ReferenceId.ToString() == referenceid).Description}"); + item.RichDescription = item.RichDescription.Replace($"$$${referenceid}$$$", $"{item.Links.FirstOrDefault(s => s.ReferenceId.ToString() == referenceid).Description}"); + } + } + + return item; } return item; @@ -309,7 +317,7 @@ public async Task
UpdateAsync(Guid id, MainUpdateModel model) if (model.RichDescription != null) { - if (model.RichDescription!=null && model.RichDescription.Contains("$$$")) + if (model.RichDescription != null && model.RichDescription.Contains("$$$")) throw new UpdateFailedException(UpdateFailErrorCode.INVALID_INPUT_SEQUENCE); entry.RichDescription = model.RichDescription; @@ -339,10 +347,11 @@ public async Task
UpdateAsync(Guid id, MainUpdateModel model) public async Task DeleteAsync(Guid id) { // 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) { + if (entry.Links != null && entry.Links.Any()) _dbcontext.Links.RemoveRange(entry.Links); _dbcontext.Main.Remove(entry); await _dbcontext.SaveChanges(); } diff --git a/Library.Encyclopedia.Entity/Exceptions/GetFailedException.cs b/Library.Encyclopedia.Entity/Exceptions/GetFailedException.cs new file mode 100644 index 0000000..8fdf675 --- /dev/null +++ b/Library.Encyclopedia.Entity/Exceptions/GetFailedException.cs @@ -0,0 +1,18 @@ +using EnumsNET; +using System; +using System.ComponentModel; + +namespace Library.Encyclopedia.Entity.Exceptions +{ + public class GetFailedException : Exception + { + public GetFailedException(GetFailErrorCode code) : base(((GetFailErrorCode)code).AsString(EnumFormat.Description)) + { + } + } + + public enum GetFailErrorCode + { + + } +} \ No newline at end of file