80 lines
2.9 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.Services;
using System.Runtime.Serialization;
using AAIntegration.SimmonsBank.API.Models.Transactions;
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{ // 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 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;
}
));
// 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))
.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;
}
));
}
}