using CPRNIMS.Domain.Contracts.Account; using CPRNIMS.Infrastructure.Database; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CPRNIMS.Domain.Services.Account { public class Attachment : IAttachment { private readonly NonInventoryDbContext _NonInventoryDbContext; public Attachment(NonInventoryDbContext NonInventoryDbContext) { _NonInventoryDbContext = NonInventoryDbContext; } public async Task CreateUpdateAttachment(Infrastructure.Entities.Account.Attachment attachment, string userId) { try { var updateProfile = _NonInventoryDbContext.Attachments. SingleOrDefault(a => a.AttachmentId == userId); if (updateProfile != null) { updateProfile.URL = attachment.URL; updateProfile.FileName = attachment.FileName; updateProfile.ExtensionId = attachment.ExtensionId; updateProfile.UpdatedDate = attachment.UpdatedDate; updateProfile.UpdatedBy = attachment.UpdatedBy; await _NonInventoryDbContext.SaveChangesAsync(); return updateProfile; } else { await _NonInventoryDbContext.AddAsync(attachment); await _NonInventoryDbContext.SaveChangesAsync(); return updateProfile; } } catch (Exception ex) { Console.WriteLine(ex.InnerException); throw; } } public Task CreateAttachment(Infrastructure.Entities.Account.Attachment attachment) { throw new NotImplementedException(); } public Task DeleteSignatureAsync(int id) { throw new NotImplementedException(); } public async Task> GetAttachmentById(string userId) { var updateProfile = await _NonInventoryDbContext.Attachments .Where(s => s.AttachmentId == userId) .ToListAsync(); if (updateProfile != null || updateProfile.Count == 1) { return updateProfile; } else { return null; } } public async Task> GetAllAttachment() { var allAttachments = await (from a in _NonInventoryDbContext.Attachments join aft in _NonInventoryDbContext.AttachmentFileTypes on a.AttachmentTypeId equals aft.AttachmentTypeId into aftGroup from aft in aftGroup.DefaultIfEmpty() join ae in _NonInventoryDbContext.AttachmentExtensions on a.ExtensionId equals ae.ExtensionId into aeGroup from ae in aeGroup.DefaultIfEmpty() select new Infrastructure.Entities.Account.Attachment { FileName = a.FileName, AttachmentId = a.AttachmentId, URL = a.URL, AttachmentFileType = aft, AttachmentExtention = ae, CreatedBy = a.CreatedBy, CreatedDate = a.CreatedDate, UpdatedBy = a.UpdatedBy, UpdatedDate = a.UpdatedDate }).ToListAsync(); if (allAttachments != null || allAttachments.Count == 1) { return allAttachments; } else { return null; } } public async Task> GetAttachmentByType(string userId, int fileType) { var allAttachments = await (from a in _NonInventoryDbContext.Attachments join aft in _NonInventoryDbContext.AttachmentFileTypes on a.AttachmentTypeId equals aft.AttachmentTypeId into aftGroup from aft in aftGroup.DefaultIfEmpty() join ae in _NonInventoryDbContext.AttachmentExtensions on a.ExtensionId equals ae.ExtensionId into aeGroup from ae in aeGroup.DefaultIfEmpty() where a.AttachmentId == userId && aft.AttachmentTypeId == fileType select new Infrastructure.Entities.Account.Attachment { FileName = a.FileName, AttachmentId = a.AttachmentId, URL = a.URL, AttachmentFileType = aft, AttachmentExtention = ae, CreatedBy = a.CreatedBy, CreatedDate = a.CreatedDate, UpdatedBy = a.UpdatedBy, UpdatedDate = a.UpdatedDate }).ToListAsync(); if (allAttachments != null || allAttachments.Count == 1) { return allAttachments; } else { return null; } } } }