birbmap/Birdmap.API/MapperProfiles/BirdmapProfile.cs

40 lines
1.2 KiB
C#
Raw Normal View History

using AutoMapper;
2020-11-01 12:25:45 +01:00
using Birdmap.API.DTOs;
using Birdmap.DAL.Entities;
2020-11-01 12:25:45 +01:00
using System;
namespace Birdmap.API.MapperProfiles
{
public class BirdmapProfile : Profile
{
public BirdmapProfile()
{
2020-11-01 12:25:45 +01:00
CreateMap<Uri, string>().ConvertUsing<UriToStringConverter>();
CreateMap<string, Uri>().ConvertUsing<UriToStringConverter>();
CreateMap<User, AuthenticateResponse>()
.ForMember(m => m.Username, opt => opt.MapFrom(m => m.Name))
.ForMember(m => m.UserRole, opt => opt.MapFrom(m => m.Role))
.ReverseMap();
2020-11-01 12:25:45 +01:00
CreateMap<Service, ServiceRequest>()
.ReverseMap();
}
private class UriToStringConverter : ITypeConverter<Uri, string>, ITypeConverter<string, Uri>
{
public string Convert(Uri source, string destination, ResolutionContext context)
{
destination = source.ToString();
return destination;
}
public Uri Convert(string source, Uri destination, ResolutionContext context)
{
Uri.TryCreate(source, UriKind.Absolute, out destination);
return destination;
}
}
}
}