Reginald Cherenfant Jasmin 2023-08-25 18:49:07 -04:00
commit 1e16a23a1a
16 changed files with 83 additions and 54 deletions

View File

@ -43,9 +43,9 @@ namespace DamageAssesment.Api.Answers.Test
return (false, list, null);
}
public static async Task<Answers.Db.Answer> getInputAnswerData()
public static async Task<Answers.Models.Answer> 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 };
}

View File

@ -78,7 +78,7 @@ namespace DamageAssesment.Api.Answers.Controllers
/// </summary>
[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
/// </summary>
[HttpPost("Answers")]
public IActionResult CreateAnswer(Db.Answer answer)
public IActionResult CreateAnswer(Models.Answer answer)
{
if (answer != null)
{

View File

@ -10,7 +10,13 @@ namespace DamageAssesment.Api.Answers.Db
}
public DbSet<Db.Answer> Answers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Answer>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
}
}
}

View File

@ -6,8 +6,8 @@
Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersByQuestionAsync(int questionId);
Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> GetAnswerByIdAsync(int Id);
Task<(bool IsSuccess, IEnumerable<Models.Answer> 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);
}
}

View File

@ -7,6 +7,7 @@ namespace DamageAssesment.Api.Answers.Profiles
public AnswersProfile()
{
CreateMap<Db.Answer,Models.Answer>();
CreateMap<Models.Answer, Db.Answer>();
}
}
}

View File

@ -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<Models.Answer, Db.Answer>(Answer);
answerDbContext.Answers.Add(answer);
answerDbContext.SaveChanges();
var result = mapper.Map<Db.Answer, Models.Answer>(Answer);
var result = mapper.Map<Db.Answer, Models.Answer>(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<Models.Answer, Db.Answer>(Answer);
answerDbContext.Answers.Update(answer);
answerDbContext.SaveChanges();
return (true, mapper.Map<Db.Answer, Models.Answer>(Answer), "Successful");
return (true, mapper.Map<Db.Answer, Models.Answer>(answer), "Successful");
}
else
{

View File

@ -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<List<Attachments.Db.Attachment>> getInputAttachmentData()
public static async Task<List<Attachments.Models.Attachment>> getInputAttachmentData()
{
List<Attachments.Db.Attachment> Attachments=new List<Db.Attachment>();
Attachments.Add(new Db.Attachment{ Id = 0, AnswerId = 10, ResponseId = 10, URI = "sample", IsDeleted = false,FileName="sample1" }) ;
List<Attachments.Models.Attachment> Attachments=new List<Models.Attachment>();
Attachments.Add(new Models.Attachment{ Id = 0, AnswerId = 10, ResponseId = 10, URI = "sample", IsDeleted = false,FileName="sample1" }) ;
return Attachments;
}

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;

View File

@ -14,12 +14,21 @@ namespace DamageAssesment.Api.Questions.Db
{
}
//protected override void OnModelCreating(ModelBuilder modelBuilder)
//{
// modelBuilder.Entity<Question>()
// .HasOne(a => a.QuestionType)
// .WithOne(b => b.Question)
// .HasForeignKey<QuestionType>(b => b.QuestionTypeID);
//}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Question>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<QuestionType>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<QuestionsTranslation>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<QuestionCategory>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
}
}
}