NonInventPurchasingSystem/CPRNIMS.Domain/Services/Account/Attachment.cs
2026-01-20 07:44:30 +08:00

143 lines
6.1 KiB
C#

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<Infrastructure.Entities.Account.Attachment> 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<Infrastructure.Entities.Account.Attachment> CreateAttachment(Infrastructure.Entities.Account.Attachment attachment)
{
throw new NotImplementedException();
}
public Task DeleteSignatureAsync(int id)
{
throw new NotImplementedException();
}
public async Task<List<Infrastructure.Entities.Account.Attachment>> 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<List<Infrastructure.Entities.Account.Attachment>> 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<List<Infrastructure.Entities.Account.Attachment>> 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;
}
}
}
}