Db context changes for auto insertion

This commit is contained in:
uppuv
2023-08-25 18:24:46 -04:00
parent 099055d088
commit 82f2ae265a
16 changed files with 83 additions and 54 deletions

View File

@ -90,7 +90,7 @@ namespace DamageAssesment.Api.Attachments.Controllers
if (attachmentInfo.Answers.Count > 0)
{
var Attachments = this.AttachmentProvider.GetAttachmentCounter();
List<Db.Attachment> attachments = UploadService.UploadAttachment(attachmentInfo.ResponseId, Attachments.counter, attachmentInfo.Answers);
List<Models.Attachment> 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<Db.Attachment> attachments = UploadService.UpdateAttachments(attachmentInfo.ResponseId, attachmentInfo.Answers, res.Attachments);
List<Models.Attachment> attachments = UploadService.UpdateAttachments(attachmentInfo.ResponseId, attachmentInfo.Answers, res.Attachments);
var result = this.AttachmentProvider.PutAttachmentAsync(attachments);
if (result.IsSuccess)
{

View File

@ -8,5 +8,12 @@ namespace DamageAssesment.Api.Attachments.Db
{
}
public DbSet<Db.Attachment> Attachments { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Attachment>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
}
}
}

View File

@ -6,8 +6,8 @@ namespace DamageAssesment.Api.Attachments.Interfaces
{
Task<(bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage)> GetAttachmentsAsync();
Task<(bool IsSuccess, Models.Attachment Attachment, string ErrorMessage)> GetAttachmentByIdAsync(int Id);
(bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage) PostAttachmentAsync(List<Db.Attachment> Attachments);
(bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage) PutAttachmentAsync(List<Db.Attachment> Attachments);
(bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage) PostAttachmentAsync(List<Models.Attachment> Attachments);
(bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage) PutAttachmentAsync(List<Models.Attachment> 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<int> answerIds);

View File

@ -4,9 +4,9 @@ namespace DamageAssesment.Api.Attachments.Interfaces
{
public interface IUploadService
{
List<Db.Attachment> UploadAttachment(int responseId,int answerId, int counter, List<IFormFile> postedFile);
List<Db.Attachment> UploadAttachment(int responseId, int counter, List<AnswerInfo> answers);
public List<Db.Attachment> UpdateAttachments(int responseId, List<AnswerInfo> answers, IEnumerable<Models.Attachment> attachments);
List<Models.Attachment> UploadAttachment(int responseId,int answerId, int counter, List<IFormFile> postedFile);
List<Models.Attachment> UploadAttachment(int responseId, int counter, List<AnswerInfo> answers);
public List<Models.Attachment> UpdateAttachments(int responseId, List<AnswerInfo> answers, IEnumerable<Models.Attachment> attachments);
void Deletefile(string path);
void Movefile(string path);
}

View File

@ -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;
//}
}
}

View File

@ -5,6 +5,7 @@
public AttachmentsProfiles()
{
CreateMap<Db.Attachment, Models.Attachment>();
CreateMap<Models.Attachment, Db.Attachment>();
}
}
}

View File

@ -65,14 +65,15 @@ namespace DamageAssesment.Api.Attachments.Providers
return (false, null, ex.Message);
}
}
public (bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage) PostAttachmentAsync(List<Db.Attachment> Attachments)
public (bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage) PostAttachmentAsync(List<Models.Attachment> Attachments)
{
try
{
logger?.LogInformation("Query Attachment");
AttachmentDbContext.Attachments.AddRange(Attachments);
List<Db.Attachment> attachments = mapper.Map<List<Models.Attachment>, List<Db.Attachment>>(Attachments);
AttachmentDbContext.Attachments.AddRange(attachments);
AttachmentDbContext.SaveChanges();
var result = mapper.Map<IEnumerable<Db.Attachment>, IEnumerable<Models.Attachment>>(Attachments);
var result = mapper.Map<IEnumerable<Db.Attachment>, IEnumerable<Models.Attachment>>(attachments);
return (true, result, null);
}
catch (Exception ex)
@ -82,14 +83,15 @@ namespace DamageAssesment.Api.Attachments.Providers
}
}
public (bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage) PutAttachmentAsync(List<Db.Attachment> Attachments)
public (bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage) PutAttachmentAsync(List<Models.Attachment> Attachments)
{
try
{
logger?.LogInformation("Query Attachment");
AttachmentDbContext.Attachments.UpdateRange(Attachments);
List<Db.Attachment> attachments = mapper.Map<List<Models.Attachment>, List<Db.Attachment>>(Attachments);
AttachmentDbContext.Attachments.UpdateRange(attachments);
AttachmentDbContext.SaveChanges();
var result = mapper.Map<IEnumerable<Db.Attachment>, IEnumerable<Models.Attachment>>(Attachments);
var result = mapper.Map<IEnumerable<Db.Attachment>, IEnumerable<Models.Attachment>>(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<AnswerInfo> answerInfos=new List<AnswerInfo>();
answerInfos.Add(new AnswerInfo(){ AnswerId = 1,postedFiles=new List<FileModel> { fileModel }});
List<Db.Attachment> attachments = uploadservice.UploadAttachment(1, 0, answerInfos);
List<Models.Attachment> attachments = uploadservice.UploadAttachment(1, 0, answerInfos);
if (attachments.Count > 0)
{
AttachmentDbContext.Attachments.AddRange(attachments);
List<Db.Attachment> Attachments = mapper.Map<List<Models.Attachment>, List<Db.Attachment>>(attachments);
AttachmentDbContext.Attachments.AddRange(Attachments);
AttachmentDbContext.SaveChanges();
}
}

View File

@ -24,7 +24,7 @@ namespace DamageAssesment.Api.Attachments.Providers
uploadpath = configuration.GetValue<string>("Fileupload:folderpath");
Deletepath = configuration.GetValue<string>("Fileupload:Deletepath");
}
public List<Db.Attachment> UploadAttachment(int responseId,int answerId,int counter, List<IFormFile> postedFile)
public List<Models.Attachment> UploadAttachment(int responseId,int answerId,int counter, List<IFormFile> postedFile)
{
var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), uploadpath);
String responseDirectory = "Response-" + responseId;
@ -42,7 +42,7 @@ namespace DamageAssesment.Api.Attachments.Providers
Deletefile(searchFile);
}
}
List<Db.Attachment> attachments = new List<Db.Attachment>();
List<Models.Attachment> attachments = new List<Models.Attachment>();
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<Db.Attachment> UploadAttachment(int responseId, int counter,List<AnswerInfo> answers)
public List<Models.Attachment> UploadAttachment(int responseId, int counter,List<AnswerInfo> answers)
{
List<Db.Attachment> attachments = new List<Db.Attachment>();
List<Models.Attachment> attachments = new List<Models.Attachment>();
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<Db.Attachment>();
return new List<Models.Attachment>();
}
}
public List<Db.Attachment> UpdateAttachments(int responseId,List<AnswerInfo> answers,IEnumerable<Models.Attachment> attachments)
public List<Models.Attachment> UpdateAttachments(int responseId,List<AnswerInfo> answers,IEnumerable<Models.Attachment> attachments)
{
List<Db.Attachment> Dbattachments = new List<Db.Attachment>();
List<Models.Attachment> Dbattachments = new List<Models.Attachment>();
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;