211 lines
8.2 KiB
C#
Raw Normal View History

namespace AAIntegration.SimmonsBank.API.Config;
using AutoMapper;
using AAIntegration.SimmonsBank.API.Entities;
using AAIntegration.SimmonsBank.API.Models.Users;
using AAIntegration.SimmonsBank.API.Models.Accounts;
using AAIntegration.SimmonsBank.API.Models.Envelopes;
using AAIntegration.SimmonsBank.API.Models.CurrencyType;
using AAIntegration.SimmonsBank.API.Services;
using System.Runtime.Serialization;
using AAIntegration.SimmonsBank.API.Models.Transactions;
using AAIntegration.SimmonsBank.API.Models.Autoclass;
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
// User -> AuthenticateResponse
CreateMap<User, AuthenticateResponse>();
// RegisterRequest -> User
CreateMap<RegisterRequest, User>();
// UserUpdateRequest -> User
CreateMap<UserUpdateRequest, User>()
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
// ignore null role
if (x.DestinationMember.Name == "Role" && src.Role == null) return false;
// ignore null password
if (x.DestinationMember.Name == "Password" && src.Password == null) return false;
return true;
}
));
// AccountUpdateRequest -> Account
CreateMap<AccountUpdateRequest, Account>();
// AccountCreateRequest -> Account
CreateMap<AccountCreateRequest, Account>();
/*.ForMember(
dest => dest.OwnerId,
opt => opt.MapFrom(src => src.Owner)
);
/*.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
))*/
// Account -> AccountGet
CreateMap<Account, AccountDTO>()
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
));
// AccountHistorical -> AccountHistoricalDTO
CreateMap<AccountHistorical, AccountHistoricalDTO>();
// EnvelopeHistorical -> EnvelopeHistoricalDTO
CreateMap<EnvelopeHistorical, EnvelopeHistoricalDTO>();
// Envelope -> EnvelopeDTO
CreateMap<Envelope, EnvelopeDTO>()
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
));
// EnvelopeFundingMethod -> EnvelopeFundingMethodDTO
CreateMap<EnvelopeFundingMethod, EnvelopeFundingMethodDTO>()
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
));
// Transaction -> TransactionDto
CreateMap<Transaction, TransactionDto>()
.ForMember(dest => dest.DebitAccountId, opt => opt.MapFrom(src => src.DebitAccount.Id))
.ForMember(dest => dest.CreditAccountId, opt => opt.MapFrom(src => src.CreditAccount.Id))
.ForMember(dest => dest.DebitEnvelopeId, opt => opt.MapFrom(src => src.DebitEnvelope.Id))
.ForMember(dest => dest.CreditEnvelopeId, opt => opt.MapFrom(src => src.CreditEnvelope.Id))
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
));
// CurrencyTypeCreateRequest -> CurrencyType
CreateMap<CurrencyTypeCreateRequest, CurrencyType>()
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
));
// AutoclassExpression <-> AutoclassExpressionCreateRequest
CreateMap<AutoclassExpression, AutoclassExpressionCreateRequest>();
CreateMap<AutoclassExpressionCreateRequest, AutoclassExpression>()
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
));
CreateMap<AutoclassExpression, AutoclassExpressionDTO>();
CreateMap<AutoclassChange, AutoclassChangeDTO>();
// AutoclassChange <-> AutoclassChangeCreateRequest
CreateMap<AutoclassChange, AutoclassChangeCreateRequest>();
CreateMap<AutoclassChangeCreateRequest, AutoclassChange>()
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
));
// AutoclassRule <-> AutoclassRuleCreateRequest
CreateMap<AutoclassRule, AutoclassRuleUpdateRequest>()
//.ForMember(d => d.Account, opt => opt.MapFrom(src => src.Account.Id))
.ReverseMap();/*
CreateMap<AutoclassRuleCreateRequest, AutoclassRule>()
.ForMember(d => d.Account, opt => opt.MapFrom(src => src))
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
if (dest.GetType() == typeof(Account))
{
}
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
));
CreateMap<AutoclassRuleCreateRequest, Account>()
.ForMember(d => d.Id, opt => opt.MapFrom(src => src.Account))
.ForAllMembers(x => x.Condition(
(src, dest, prop) =>
{
src.UseDestinationValue();
// ignore both null & empty string properties
if (prop == null) return false;
if (prop.GetType() == typeof(string) && string.IsNullOrEmpty((string)prop)) return false;
return true;
}
));*/
// AutoclassRule -> AutoclassRuleDTO
CreateMap<AutoclassRule, AutoclassRuleDTO>()
.ForMember(dest => dest.AccountId, opt => opt.MapFrom(src => src.Account.Id));
}
}