112 lines
3.8 KiB
C#
112 lines
3.8 KiB
C#
using Marco.Pms.DataAccess.Data;
|
|
using Marco.Pms.Model.Directory;
|
|
using Marco.Pms.Model.Mapper;
|
|
using Marco.Pms.Model.Utilities;
|
|
using Marco.Pms.Model.ViewModels.Directory;
|
|
using Marco.Pms.Model.ViewModels.Master;
|
|
using MarcoBMS.Services.Helpers;
|
|
using MarcoBMS.Services.Service;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Marco.Pms.Services.Helpers
|
|
{
|
|
public class DirectoryHelper
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly ILoggingService _logger;
|
|
private readonly UserHelper _userHelper;
|
|
|
|
|
|
public DirectoryHelper(ApplicationDbContext context, ILoggingService logger, UserHelper userHelper)
|
|
{
|
|
_context = context;
|
|
_logger = logger;
|
|
_userHelper = userHelper;
|
|
}
|
|
|
|
|
|
|
|
public async Task<ApiResponse<object>> GetListOfContacts()
|
|
{
|
|
Guid tenantId = _userHelper.GetTenantId();
|
|
|
|
var contacts = await _context.Contacts.Where(c => c.TenantId == tenantId).ToListAsync();
|
|
|
|
List<Guid> contactIds = contacts.Select(c=>c.Id).ToList();
|
|
|
|
var phoneNo = await _context.ContactsPhones.Where(p => contactIds.Contains(p.ContactId)).ToListAsync();
|
|
|
|
var Emails = await _context.ContactsEmails.Where(E=> contactIds.Contains(E.ContactId)).ToListAsync();
|
|
|
|
var Tags = await _context.ContactTagMappings.Where(t=>contactIds.Contains(t.ContactId)).ToListAsync();
|
|
|
|
|
|
List<Guid> TagIds = Tags.Select(t=>t.ContactTagtId).ToList();
|
|
|
|
var TagList = await _context.ContactTagMasters.Where(t=> TagIds.Contains(t.Id)).ToListAsync();
|
|
|
|
List<ContactVM> list = new List<ContactVM>();
|
|
|
|
foreach (var contact in contacts)
|
|
{
|
|
|
|
ContactVM contactVM = new ContactVM();
|
|
List<ContactEmailVM> contactEmailVms = new List<ContactEmailVM>();
|
|
List<ContactPhoneVM> contactPhoneVms = new List<ContactPhoneVM>();
|
|
|
|
List<ContactTagVM> conatctTagVms = new List<ContactTagVM>();
|
|
var phones = phoneNo.Where(p => p.ContactId == contact.Id).ToList();
|
|
var emails = Emails.Where(e => e.ContactId == contact.Id).ToList();
|
|
var tagMappingss = Tags.Where(t => t.ContactId == contact.Id).ToList();
|
|
|
|
|
|
if(emails != null)
|
|
{
|
|
foreach (var email in emails)
|
|
{
|
|
ContactEmailVM emailVM = new ContactEmailVM();
|
|
emailVM = email.ToContactEmailVMFromContactEmail();
|
|
contactEmailVms.Add(emailVM);
|
|
}
|
|
}
|
|
|
|
if(phones != null)
|
|
{
|
|
foreach (var phone in phones)
|
|
{
|
|
ContactPhoneVM phoneVM = new ContactPhoneVM();
|
|
phoneVM = phone.ToContactPhoneVMFromContactPhone();
|
|
contactPhoneVms.Add(phoneVM);
|
|
}
|
|
|
|
}
|
|
|
|
if (tagMappingss != null) {
|
|
foreach (var tagMapping in tagMappingss)
|
|
{
|
|
ContactTagVM tagVM = new ContactTagVM(); ;
|
|
var tag = TagList.Find(t => t.Id == tagMapping.ContactTagtId);
|
|
|
|
tagVM = tag.ToContactTagVMFromContactTagMaster();
|
|
conatctTagVms.Add(tagVM);
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
contactVM = contact.ToContactVMFromContact();
|
|
|
|
contactVM.ContactEmails = contactEmailVms;
|
|
contactVM.ContactPhones = contactPhoneVms;
|
|
contactVM.Tags = conatctTagVms;
|
|
|
|
list.Add(contactVM);
|
|
}
|
|
|
|
return ApiResponse<object>.SuccessResponse(list, "Contact List !",200);
|
|
|
|
}
|
|
}
|
|
}
|