From 82f2ae265a69eaeb1edbecb722a76c90e67dd163 Mon Sep 17 00:00:00 2001 From: uppuv Date: Fri, 25 Aug 2023 18:24:46 -0400 Subject: [PATCH] Db context changes for auto insertion --- .../MocData.cs | 4 ++-- .../Controllers/AnswersController.cs | 4 ++-- .../Db/AnswerDbContext.cs | 8 ++++++- .../Interfaces/IAnswersProvider.cs | 4 ++-- .../Profiles/AnswersProfile.cs | 1 + .../Providers/AnswerProvider.cs | 14 ++++++----- .../MockData.cs | 8 +++---- .../Controllers/AttachmentsController.cs | 4 ++-- .../Db/AttachmentsDbContext.cs | 7 ++++++ .../Interfaces/IAttachmentsProvider.cs | 4 ++-- .../Interfaces/IUploadService.cs | 6 ++--- .../Models/Attachment.cs | 10 ++++---- .../Profiles/AttachmentsProfiles.cs | 1 + .../Providers/AttachmentsProvider.cs | 19 ++++++++------- .../Providers/UploadService.cs | 20 ++++++++-------- .../Db/QuestionDbContext.cs | 23 +++++++++++++------ 16 files changed, 83 insertions(+), 54 deletions(-) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers.Test/MocData.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers.Test/MocData.cs index e65d4f4..02466b6 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers.Test/MocData.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers.Test/MocData.cs @@ -43,9 +43,9 @@ namespace DamageAssesment.Api.Answers.Test return (false, list, null); } - public static async Task getInputAnswerData() + public static async Task getInputAnswerData() { - return new Answers.Db.Answer { Id = 1, AnswerText = "Yes", Comment = "", QuestionId = 1, SurveyResponseId = 1 }; + return new Answers.Models.Answer { Id = 1, AnswerText = "Yes", Comment = "", QuestionId = 1, SurveyResponseId = 1 }; } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Controllers/AnswersController.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Controllers/AnswersController.cs index 95b2aab..241b357 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Controllers/AnswersController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Controllers/AnswersController.cs @@ -78,7 +78,7 @@ namespace DamageAssesment.Api.Answers.Controllers /// [HttpPut("Answers")] - public IActionResult UpdateAnswer(Db.Answer answer) + public IActionResult UpdateAnswer(Models.Answer answer) { if (answer != null) { @@ -99,7 +99,7 @@ namespace DamageAssesment.Api.Answers.Controllers /// [HttpPost("Answers")] - public IActionResult CreateAnswer(Db.Answer answer) + public IActionResult CreateAnswer(Models.Answer answer) { if (answer != null) { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Db/AnswerDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Db/AnswerDbContext.cs index 8d3a8ef..1f3c996 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Db/AnswerDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Db/AnswerDbContext.cs @@ -10,7 +10,13 @@ namespace DamageAssesment.Api.Answers.Db } public DbSet Answers { get; set; } - + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity() + .Property(item => item.Id) + .ValueGeneratedOnAdd(); + } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Interfaces/IAnswersProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Interfaces/IAnswersProvider.cs index 4b5fcc1..d948124 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Interfaces/IAnswersProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Interfaces/IAnswersProvider.cs @@ -6,8 +6,8 @@ Task<(bool IsSuccess, IEnumerable Answers, string ErrorMessage)> GetAnswersByQuestionAsync(int questionId); Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> GetAnswerByIdAsync(int Id); Task<(bool IsSuccess, IEnumerable Answers, string ErrorMessage)> GetAnswersAsync(int responseId); - (bool IsSuccess, Models.Answer Answer, string ErrorMessage) PostAnswerAsync(Db.Answer Answer); - (bool IsSuccess, Models.Answer Answer, string ErrorMessage) UpdateAnswerAsync(Db.Answer Answer); + (bool IsSuccess, Models.Answer Answer, string ErrorMessage) PostAnswerAsync(Models.Answer Answer); + (bool IsSuccess, Models.Answer Answer, string ErrorMessage) UpdateAnswerAsync(Models.Answer Answer); Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> DeleteAnswerAsync(int Id); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Profiles/AnswersProfile.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Profiles/AnswersProfile.cs index 0a5e6ae..ce01163 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Profiles/AnswersProfile.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Profiles/AnswersProfile.cs @@ -7,6 +7,7 @@ namespace DamageAssesment.Api.Answers.Profiles public AnswersProfile() { CreateMap(); + CreateMap(); } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs index 6249712..e9720d1 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs @@ -108,16 +108,17 @@ namespace DamageAssesment.Api.Answers.Providers return (false, null, ex.Message); } } - public (bool IsSuccess, Models.Answer Answer, string ErrorMessage) PostAnswerAsync(Db.Answer Answer) + public (bool IsSuccess, Models.Answer Answer, string ErrorMessage) PostAnswerAsync(Models.Answer Answer) { try { logger?.LogInformation("Query Answer"); if (!AnswerExists(Answer.Id)) { - answerDbContext.Answers.Add(Answer); + Db.Answer answer = mapper.Map(Answer); + answerDbContext.Answers.Add(answer); answerDbContext.SaveChanges(); - var result = mapper.Map(Answer); + var result = mapper.Map(answer); return (true, result, null); } return (false, null, "Answer is already exits"); @@ -128,7 +129,7 @@ namespace DamageAssesment.Api.Answers.Providers return (false, null, ex.Message); } } - public (bool IsSuccess, Models.Answer Answer, string ErrorMessage) UpdateAnswerAsync(Db.Answer Answer) + public (bool IsSuccess, Models.Answer Answer, string ErrorMessage) UpdateAnswerAsync(Models.Answer Answer) { try { @@ -137,9 +138,10 @@ namespace DamageAssesment.Api.Answers.Providers var existing = answerDbContext.Answers.AsNoTracking().FirstOrDefault(x => x.Id == Answer.Id); if (existing != null) { - answerDbContext.Answers.Update(Answer); + Db.Answer answer = mapper.Map(Answer); + answerDbContext.Answers.Update(answer); answerDbContext.SaveChanges(); - return (true, mapper.Map(Answer), "Successful"); + return (true, mapper.Map(answer), "Successful"); } else { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments.Test/MockData.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments.Test/MockData.cs index 284830d..c097902 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments.Test/MockData.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments.Test/MockData.cs @@ -17,7 +17,7 @@ namespace DamageAssesment.Api.Attachments.Test for (int i = 0; i < 10; i++) { - list.Add(new Attachments.Models.Attachment(i, Guid.NewGuid().ToString() + "@gmail.com") + list.Add(new Attachments.Models.Attachment() { Id = i, AnswerId = i, @@ -73,10 +73,10 @@ namespace DamageAssesment.Api.Attachments.Test return (false, list, null); } - public static async Task> getInputAttachmentData() + public static async Task> getInputAttachmentData() { - List Attachments=new List(); - Attachments.Add(new Db.Attachment{ Id = 0, AnswerId = 10, ResponseId = 10, URI = "sample", IsDeleted = false,FileName="sample1" }) ; + List Attachments=new List(); + Attachments.Add(new Models.Attachment{ Id = 0, AnswerId = 10, ResponseId = 10, URI = "sample", IsDeleted = false,FileName="sample1" }) ; return Attachments; } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Controllers/AttachmentsController.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Controllers/AttachmentsController.cs index 745db41..f4f0785 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Controllers/AttachmentsController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Controllers/AttachmentsController.cs @@ -90,7 +90,7 @@ namespace DamageAssesment.Api.Attachments.Controllers if (attachmentInfo.Answers.Count > 0) { var Attachments = this.AttachmentProvider.GetAttachmentCounter(); - List attachments = UploadService.UploadAttachment(attachmentInfo.ResponseId, Attachments.counter, attachmentInfo.Answers); + List attachments = UploadService.UploadAttachment(attachmentInfo.ResponseId, Attachments.counter, attachmentInfo.Answers); var result = this.AttachmentProvider.PostAttachmentAsync(attachments); if (result.IsSuccess) { @@ -119,7 +119,7 @@ namespace DamageAssesment.Api.Attachments.Controllers var res = this.AttachmentProvider.GetAttachmentInfo(attachmentInfo.Answers); if (res.IsSuccess) { - List attachments = UploadService.UpdateAttachments(attachmentInfo.ResponseId, attachmentInfo.Answers, res.Attachments); + List attachments = UploadService.UpdateAttachments(attachmentInfo.ResponseId, attachmentInfo.Answers, res.Attachments); var result = this.AttachmentProvider.PutAttachmentAsync(attachments); if (result.IsSuccess) { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Db/AttachmentsDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Db/AttachmentsDbContext.cs index 561377d..b4e8d0c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Db/AttachmentsDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Db/AttachmentsDbContext.cs @@ -8,5 +8,12 @@ namespace DamageAssesment.Api.Attachments.Db { } public DbSet Attachments { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity() + .Property(item => item.Id) + .ValueGeneratedOnAdd(); + } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Interfaces/IAttachmentsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Interfaces/IAttachmentsProvider.cs index 81bc749..5af5c60 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Interfaces/IAttachmentsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Interfaces/IAttachmentsProvider.cs @@ -6,8 +6,8 @@ namespace DamageAssesment.Api.Attachments.Interfaces { Task<(bool IsSuccess, IEnumerable Attachments, string ErrorMessage)> GetAttachmentsAsync(); Task<(bool IsSuccess, Models.Attachment Attachment, string ErrorMessage)> GetAttachmentByIdAsync(int Id); - (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PostAttachmentAsync(List Attachments); - (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PutAttachmentAsync(List Attachments); + (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PostAttachmentAsync(List Attachments); + (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PutAttachmentAsync(List Attachments); Task<(bool IsSuccess, Models.Attachment Attachment, string Path)> DeleteAttachmentAsync(int Id); Task<(bool IsSuccess, int counter, string Path)> DeleteAttachmentsAsync(int responseId, int answerId); Task<(bool IsSuccess, int counter, string Path)> DeleteBulkAttachmentsAsync(int responseId, List answerIds); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Interfaces/IUploadService.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Interfaces/IUploadService.cs index 121d411..f645593 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Interfaces/IUploadService.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Interfaces/IUploadService.cs @@ -4,9 +4,9 @@ namespace DamageAssesment.Api.Attachments.Interfaces { public interface IUploadService { - List UploadAttachment(int responseId,int answerId, int counter, List postedFile); - List UploadAttachment(int responseId, int counter, List answers); - public List UpdateAttachments(int responseId, List answers, IEnumerable attachments); + List UploadAttachment(int responseId,int answerId, int counter, List postedFile); + List UploadAttachment(int responseId, int counter, List answers); + public List UpdateAttachments(int responseId, List answers, IEnumerable attachments); void Deletefile(string path); void Movefile(string path); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Models/Attachment.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Models/Attachment.cs index 8f2ba66..b364727 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Models/Attachment.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Models/Attachment.cs @@ -13,10 +13,10 @@ namespace DamageAssesment.Api.Attachments.Models public bool IsDeleted { get; set; } public string FileName { get; set; } - public Attachment(int answerId, string uri) - { - this.AnswerId = answerId; - this.URI = uri; - } + //public Attachment(int answerId, string uri) + //{ + // this.AnswerId = answerId; + // this.URI = uri; + //} } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Profiles/AttachmentsProfiles.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Profiles/AttachmentsProfiles.cs index 44ee69f..2dca91e 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Profiles/AttachmentsProfiles.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Profiles/AttachmentsProfiles.cs @@ -5,6 +5,7 @@ public AttachmentsProfiles() { CreateMap(); + CreateMap(); } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/AttachmentsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/AttachmentsProvider.cs index aa42d93..46356db 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/AttachmentsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/AttachmentsProvider.cs @@ -65,14 +65,15 @@ namespace DamageAssesment.Api.Attachments.Providers return (false, null, ex.Message); } } - public (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PostAttachmentAsync(List Attachments) + public (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PostAttachmentAsync(List Attachments) { try { logger?.LogInformation("Query Attachment"); - AttachmentDbContext.Attachments.AddRange(Attachments); + List attachments = mapper.Map, List>(Attachments); + AttachmentDbContext.Attachments.AddRange(attachments); AttachmentDbContext.SaveChanges(); - var result = mapper.Map, IEnumerable>(Attachments); + var result = mapper.Map, IEnumerable>(attachments); return (true, result, null); } catch (Exception ex) @@ -82,14 +83,15 @@ namespace DamageAssesment.Api.Attachments.Providers } } - public (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PutAttachmentAsync(List Attachments) + public (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PutAttachmentAsync(List Attachments) { try { logger?.LogInformation("Query Attachment"); - AttachmentDbContext.Attachments.UpdateRange(Attachments); + List attachments = mapper.Map, List>(Attachments); + AttachmentDbContext.Attachments.UpdateRange(attachments); AttachmentDbContext.SaveChanges(); - var result = mapper.Map, IEnumerable>(Attachments); + var result = mapper.Map, IEnumerable>(attachments); return (true, result, null); } catch (Exception ex) @@ -204,10 +206,11 @@ namespace DamageAssesment.Api.Attachments.Providers FileModel fileModel= new FileModel(){AttachmentId=0,FileName="Sample",FileContent= "c2FtcGxl",FileExtension=".txt"}; List answerInfos=new List(); answerInfos.Add(new AnswerInfo(){ AnswerId = 1,postedFiles=new List { fileModel }}); - List attachments = uploadservice.UploadAttachment(1, 0, answerInfos); + List attachments = uploadservice.UploadAttachment(1, 0, answerInfos); if (attachments.Count > 0) { - AttachmentDbContext.Attachments.AddRange(attachments); + List Attachments = mapper.Map, List>(attachments); + AttachmentDbContext.Attachments.AddRange(Attachments); AttachmentDbContext.SaveChanges(); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/UploadService.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/UploadService.cs index 3c0f21f..8c9045c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/UploadService.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/UploadService.cs @@ -24,7 +24,7 @@ namespace DamageAssesment.Api.Attachments.Providers uploadpath = configuration.GetValue("Fileupload:folderpath"); Deletepath = configuration.GetValue("Fileupload:Deletepath"); } - public List UploadAttachment(int responseId,int answerId,int counter, List postedFile) + public List UploadAttachment(int responseId,int answerId,int counter, List postedFile) { var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), uploadpath); String responseDirectory = "Response-" + responseId; @@ -42,7 +42,7 @@ namespace DamageAssesment.Api.Attachments.Providers Deletefile(searchFile); } } - List attachments = new List(); + List attachments = new List(); foreach (IFormFile item in postedFile) { @@ -55,15 +55,15 @@ namespace DamageAssesment.Api.Attachments.Providers { item.CopyTo(stream); } - attachments.Add(new Db.Attachment { AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath }); + attachments.Add(new Models.Attachment { AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath }); } return attachments; } - public List UploadAttachment(int responseId, int counter,List answers) + public List UploadAttachment(int responseId, int counter,List answers) { - List attachments = new List(); + List attachments = new List(); try { foreach (var item in answers) @@ -94,20 +94,20 @@ namespace DamageAssesment.Api.Attachments.Providers var dbPath = Path.Combine(fullDirectoryPath, fileName); File.WriteAllBytes(dbPath, Convert.FromBase64String(file.FileContent)); - attachments.Add(new Db.Attachment { AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath }); + attachments.Add(new Models.Attachment { AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath }); } } return attachments; } catch (Exception ex) { - return new List(); + return new List(); } } - public List UpdateAttachments(int responseId,List answers,IEnumerable attachments) + public List UpdateAttachments(int responseId,List answers,IEnumerable attachments) { - List Dbattachments = new List(); + List Dbattachments = new List(); foreach (Models.Attachment searchFile in attachments) { Deletefile(searchFile.URI); @@ -131,7 +131,7 @@ namespace DamageAssesment.Api.Attachments.Providers var dbPath = Path.Combine(fullDirectoryPath, fileName); File.WriteAllBytes(dbPath, Convert.FromBase64String(file.FileContent)); - Dbattachments.Add(new Db.Attachment { Id=attachment.Id, AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath }); + Dbattachments.Add(new Models.Attachment { Id=attachment.Id, AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath }); } } return Dbattachments; diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs index a784abf..a62485e 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs @@ -14,12 +14,21 @@ namespace DamageAssesment.Api.Questions.Db { } - //protected override void OnModelCreating(ModelBuilder modelBuilder) - //{ - // modelBuilder.Entity() - // .HasOne(a => a.QuestionType) - // .WithOne(b => b.Question) - // .HasForeignKey(b => b.QuestionTypeID); - //} + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity() + .Property(item => item.Id) + .ValueGeneratedOnAdd(); + modelBuilder.Entity() + .Property(item => item.Id) + .ValueGeneratedOnAdd(); + modelBuilder.Entity() + .Property(item => item.Id) + .ValueGeneratedOnAdd(); + modelBuilder.Entity() + .Property(item => item.Id) + .ValueGeneratedOnAdd(); + } } }