using AutoMapper; using DamageAssesment.Api.Attachments.Db; using DamageAssesment.Api.Attachments.Interfaces; using DamageAssesment.Api.Attachments.Models; using Microsoft.EntityFrameworkCore; using System.Drawing; namespace DamageAssesment.Api.Attachments.Providers { public class AttachmentsProvider : IAttachmentsProvider { private AttachmentsDbContext AttachmentDbContext; private ILogger logger; private IUploadService uploadservice; private IMapper mapper; public AttachmentsProvider(AttachmentsDbContext AttachmentDbContext, ILogger logger, IMapper mapper,IUploadService uploadservice) { this.AttachmentDbContext = AttachmentDbContext; this.logger = logger; this.mapper = mapper; this.uploadservice = uploadservice; SeedData(); } public async Task<(bool IsSuccess, IEnumerable Attachments, string ErrorMessage)> GetAttachmentsAsync() { try { logger?.LogInformation("Query Question"); var Attachment = await AttachmentDbContext.Attachments.AsNoTracking().Where(a => !a.IsDeleted).ToListAsync(); if (Attachment != null) { logger?.LogInformation($"{Attachment.Count} Attachments(s) found"); var result = mapper.Map, IEnumerable>(Attachment); return (true, result, null); } return (false, null, "Not found"); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } public async Task<(bool IsSuccess, Models.Attachment Attachment, string ErrorMessage)> GetAttachmentByIdAsync(int Id) { try { logger?.LogInformation("Query Attachment"); var Attachment = await AttachmentDbContext.Attachments.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id & !q.IsDeleted); if (Attachment != null) { logger?.LogInformation($"{Attachment} customer(s) found"); var result = mapper.Map(Attachment); return (true, result, null); } return (false, null, "Not found"); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } public (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PostAttachmentAsync(List Attachments) { try { logger?.LogInformation("Query Attachment"); AttachmentDbContext.Attachments.AddRange(Attachments); AttachmentDbContext.SaveChanges(); var result = mapper.Map, IEnumerable>(Attachments); return (true, result, null); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } public (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) PutAttachmentAsync(List Attachments) { try { logger?.LogInformation("Query Attachment"); AttachmentDbContext.Attachments.UpdateRange(Attachments); AttachmentDbContext.SaveChanges(); var result = mapper.Map, IEnumerable>(Attachments); return (true, result, null); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } public async Task<(bool IsSuccess, int counter, string Path)> DeleteBulkAttachmentsAsync(int responseId, List answerIds) { int AttachmentId = 0; try { AttachmentId = AttachmentDbContext.Attachments.Max(a => a.Id); List Attachments = AttachmentDbContext.Attachments.Where(a => a.ResponseId == responseId && answerIds.Contains(a.AnswerId ?? 0)).AsNoTracking().ToList(); if (Attachments.Count > 0) { AttachmentDbContext.Attachments.RemoveRange(Attachments); AttachmentDbContext.SaveChanges(); } return (true, AttachmentId, ""); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, AttachmentId, ""); } } public (bool IsSuccess,int counter,string message) GetAttachmentCounter() { try { int AttachmentId = AttachmentDbContext.Attachments.Max(a => a.Id); return (true, AttachmentId, ""); } catch(Exception ex) { return (false, 0, ex.Message); } } public async Task<(bool IsSuccess, int counter, string Path)> DeleteAttachmentsAsync(int responseId, int answerId) { int AttachmentId = 0; try { AttachmentId = AttachmentDbContext.Attachments.Max(a => a.Id); List Attachments = AttachmentDbContext.Attachments.Where(a => a.ResponseId == responseId && a.AnswerId == answerId).AsNoTracking().ToList(); if (Attachments.Count > 0) { AttachmentDbContext.Attachments.RemoveRange(Attachments); AttachmentDbContext.SaveChanges(); } return (true, AttachmentId, ""); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, AttachmentId, ""); } } public (bool IsSuccess, IEnumerable Attachments, string ErrorMessage) GetAttachmentInfo(List answers) { try { List attchmentIds = new List(); foreach (AnswerInfo item in answers) { attchmentIds.AddRange(item.postedFiles.Select(a => a.AttachmentId ?? 0).ToList()); } var attachments= AttachmentDbContext.Attachments.AsNoTracking().Where(a=>attchmentIds.Contains(a.Id)).ToList(); var result = mapper.Map, IEnumerable>(attachments); return (true, result, null); } catch (Exception ex) { return (false, null, ex.Message); } } public async Task<(bool IsSuccess, Models.Attachment Attachment, string Path)> DeleteAttachmentAsync(int Id) { try { Db.Attachment Attachment = AttachmentDbContext.Attachments.Where(a => a.Id == Id).AsNoTracking().FirstOrDefault(); if (Attachment == null) { return (false, null, "Not Found"); } Attachment.IsDeleted = true; AttachmentDbContext.Attachments.Update(Attachment); AttachmentDbContext.SaveChanges(); return (true, mapper.Map(Attachment), $"Attachment {Id} is deleted"); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } private bool AttachmentExists(int id) { return AttachmentDbContext.Attachments.AsNoTracking().Count(e => e.Id == id && !e.IsDeleted) > 0; } private void SeedData() { if (!AttachmentDbContext.Attachments.Any()) { // adding sample text file in respective folder based responseid and answer id 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); if (attachments.Count > 0) { AttachmentDbContext.Attachments.AddRange(attachments); AttachmentDbContext.SaveChanges(); } } } } }