Files
Backend-Api/CompanyManagment.Application/AuthorizedPersonApplication.cs

120 lines
4.9 KiB
C#

using System.Collections.Generic;
using System.Linq;
using _0_Framework.Application;
using Company.Domain.AuthorizedPersonAgg;
using CompanyManagment.App.Contracts.AuthorizedPerson;
namespace CompanyManagment.Application;
public class AuthorizedPersonApplication : IAuthorizedPersonApplication
{
private readonly IAuthorizedPersonRepository _authorizedPersonRepository;
public AuthorizedPersonApplication(IAuthorizedPersonRepository authorizedPersonRepository)
{
_authorizedPersonRepository = authorizedPersonRepository;
}
public OperationResult Create(CreateAuthorizedPerson command)
{
var operation = new OperationResult();
if (_authorizedPersonRepository.ExistsByNationalCode(command.NationalCode))
return operation.Failed("شخص با این کد ملی قبلاً ثبت شده است");
var authorizedPerson = new AuthorizedPerson(
command.NationalCode, command.FirstName, command.LastName,
command.FatherName, command.BirthDate, command.Gender,
command.DeathStatus, command.ShenasnameSeri,
command.ShenasnameSerial, command.ShenasnamehNumber);
_authorizedPersonRepository.Create(authorizedPerson);
_authorizedPersonRepository.SaveChanges();
return operation.Succcedded();
}
public OperationResult CreateFromUidResponse(CreateAuthorizedPerson command)
{
var operation = new OperationResult();
var existingPerson = _authorizedPersonRepository.GetByNationalCode(command.NationalCode);
if (existingPerson != null)
{
existingPerson.UpdatePersonalInfo(command.FirstName, command.LastName,
command.FatherName, command.Gender, command.DeathStatus);
_authorizedPersonRepository.SaveChanges();
return operation.Succcedded();
}
var authorizedPerson = new AuthorizedPerson(
command.NationalCode, command.FirstName, command.LastName,
command.FatherName, command.BirthDate, command.Gender,
command.DeathStatus, command.ShenasnameSeri,
command.ShenasnameSerial, command.ShenasnamehNumber);
_authorizedPersonRepository.Create(authorizedPerson);
_authorizedPersonRepository.SaveChanges();
return operation.Succcedded();
}
public AuthorizedPersonViewModel GetByNationalCode(string nationalCode)
{
var authorizedPerson = _authorizedPersonRepository.GetByNationalCode(nationalCode);
if (authorizedPerson == null) return null;
return new AuthorizedPersonViewModel
{
Id = authorizedPerson.id,
NationalCode = authorizedPerson.NationalCode,
FirstName = authorizedPerson.FirstName,
LastName = authorizedPerson.LastName,
FatherName = authorizedPerson.FatherName,
BirthDate = authorizedPerson.BirthDate,
Gender = authorizedPerson.Gender,
DeathStatus = authorizedPerson.DeathStatus,
ShenasnameSeri = authorizedPerson.ShenasnameSeri,
ShenasnameSerial = authorizedPerson.ShenasnameSerial,
ShenasnamehNumber = authorizedPerson.ShenasnamehNumber,
IsVerified = authorizedPerson.IsVerified,
VerificationDate = authorizedPerson.VerificationDate?.ToString("yyyy/MM/dd HH:mm"),
CreationDate = authorizedPerson.CreationDate.ToString("yyyy/MM/dd HH:mm")
};
}
public List<AuthorizedPersonViewModel> Search(string nationalCode = null, string firstName = null, string lastName = null)
{
var allPersons = _authorizedPersonRepository.Get();
var filteredPersons = allPersons.Where(x =>
(string.IsNullOrEmpty(nationalCode) || x.NationalCode.Contains(nationalCode)) &&
(string.IsNullOrEmpty(firstName) || x.FirstName.Contains(firstName)) &&
(string.IsNullOrEmpty(lastName) || x.LastName.Contains(lastName)))
.Select(x => new AuthorizedPersonViewModel
{
Id = x.id,
NationalCode = x.NationalCode,
FirstName = x.FirstName,
LastName = x.LastName,
FatherName = x.FatherName,
BirthDate = x.BirthDate,
Gender = x.Gender,
DeathStatus = x.DeathStatus,
ShenasnameSeri = x.ShenasnameSeri,
ShenasnameSerial = x.ShenasnameSerial,
ShenasnamehNumber = x.ShenasnamehNumber,
IsVerified = x.IsVerified,
VerificationDate = x.VerificationDate?.ToString("yyyy/MM/dd HH:mm"),
CreationDate = x.CreationDate.ToString("yyyy/MM/dd HH:mm")
}).ToList();
return filteredPersons;
}
public bool ExistsByNationalCode(string nationalCode)
{
return _authorizedPersonRepository.ExistsByNationalCode(nationalCode);
}
}