Skip to content

Instantly share code, notes, and snippets.

@Sampath-Lokuge
Created June 19, 2018 11:40
Show Gist options
  • Save Sampath-Lokuge/50001c73e8dae3e7c59db5e12202e78e to your computer and use it in GitHub Desktop.
Save Sampath-Lokuge/50001c73e8dae3e7c59db5e12202e78e to your computer and use it in GitHub Desktop.
AgentAppService.cs
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Joshi.IP.Agents.Dtos;
using Joshi.IP.Authorization;
using Joshi.IP.EntityFramework;
namespace Joshi.IP.Agents
{
[AbpAuthorize]
public class AgentAppService : IPAppServiceBase, IAgentAppService
{
private readonly IRepository<Agent> _agentRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IpDbContext _context;
public AgentAppService(IRepository<Agent> agentRepository, IUnitOfWorkManager unitOfWorkManager, IpDbContext context)
{
_agentRepository = agentRepository;
_unitOfWorkManager = unitOfWorkManager;
_context = context;
}
/// <summary>
/// to Get All Attorneys details
/// </summary>
public async Task<ListResultDto<AgentListDto>> GetAllAgentsAsync()
{
var cache = RedisConnectorHelper.Connection.GetDatabase();
var values = RedisConnectorHelper.GetCache<AgentListDto>(AppConsts.Agents, cache, o => o.Name);//get cache
if (values != null) return values;
var agents = await _agentRepository.GetAllListAsync();
RedisConnectorHelper.SetCache<AgentListDto, Agent>(AppConsts.Agents, agents, cache, o => o.Name);//set cache
return new ListResultDto<AgentListDto>(agents.OrderBy(o => o.Name).MapTo<List<AgentListDto>>());
}
/// <summary>
/// to Get Agent For Editing
/// </summary>
public async Task<AgentEditDto> GetAgentForEditAsync(NullableIdDto<int> input)
{
var agent = await _agentRepository.FirstOrDefaultAsync(p => p.Id == input.Id);
var result = agent.MapTo<AgentEditDto>();
return result;
}
/// <summary>
/// to Create Or Edit Agent
/// </summary>
[ExcludeFromCodeCoverage]
[AbpAuthorize(AppPermissions.Pages_Tenant_IplContactsAndAssociates)]
public async Task<int?> CreateOrEditAgentAsync(CreateOrEditAgentInput input)
{
int? agentId;
if (input.AgentDetail.Id.HasValue)//edit
{
agentId = await EditAgentAsync(input);
}
else//create
{
agentId = await CreateAgentAsync(input);
}
var cacheKeys = new string[] { AppConsts.Agents };
AppHelper.DeleteCache(cacheKeys);//to delete cache key
return agentId;
}
/// <summary>
/// to Create Agent
/// </summary>
public async Task<int> CreateAgentAsync(CreateOrEditAgentInput input)
{
var agent = input.AgentDetail.MapTo<Agent>();
SetTenantId(agent);
var agentId = await _agentRepository.InsertAndGetIdAsync(agent);
return agentId;
}
/// <summary>
/// to Edit Agent
/// </summary>
public async Task<int?> EditAgentAsync(CreateOrEditAgentInput input)
{
var agent = await _context.Agents.FirstOrDefaultAsync(p => p.Id == input.AgentDetail.Id);
foreach (var be in agent.AgentEmployees.ToList())//to remove existing Agent Employees
{
_context.AgentEmployees.Remove(be);
}
await _unitOfWorkManager.Current.SaveChangesAsync();//intermediate save
input.AgentDetail.MapTo(agent);
foreach (var ae in agent.AgentEmployees.ToList())//to add new Agent Employees
{
_context.AgentEmployees.Add(ae);
}
SetTenantId(agent);
await _context.SaveChangesAsync();//final save
return input.AgentDetail.Id;
}
#region Private Methods
/// <summary>
/// to set TenantId
/// </summary>
private void SetTenantId(Agent agent)
{
if (AbpSession.TenantId == null) return;
agent.TenantId = AbpSession.TenantId.Value;
agent.AdditionalAddress.TenantId = AbpSession.TenantId.Value;
agent.ContactDetail.TenantId = AbpSession.TenantId.Value;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment