Extract repeated mapper methods into a common mapper class

This commit is contained in:
whimsical-c4lic0 2026-02-09 23:14:52 -06:00
parent ff431a377d
commit 9766636d04
10 changed files with 161 additions and 228 deletions

View File

@ -17,9 +17,9 @@ public class ListItem
public List<object>? Preview { get; set; } public List<object>? Preview { get; set; }
public FromUser FromUser { get; set; } = new(); public FromUser? FromUser { get; set; }
public Author Author { get; set; } = new(); public Author? Author { get; set; }
public long Id { get; set; } public long Id { get; set; }

View File

@ -28,7 +28,7 @@ public static class ArchivedMapper
{ {
Id = dto.Id, Id = dto.Id,
PostedAt = dto.PostedAt, PostedAt = dto.PostedAt,
Author = MapAuthor(dto.Author), Author = CommonMapper.MapAuthor(dto.Author),
Text = dto.Text, Text = dto.Text,
Price = dto.Price, Price = dto.Price,
IsOpened = dto.IsOpened, IsOpened = dto.IsOpened,
@ -37,25 +37,8 @@ public static class ArchivedMapper
Preview = dto.Preview Preview = dto.Preview
}; };
private static Entities.Common.Author? MapAuthor(AuthorDto? dto) private static List<Medium>? MapMedia(List<MediumDto>? media) =>
{ media == null ? null : media.Select(MapMedium).ToList();
if (dto == null)
{
return null;
}
return new Entities.Common.Author { Id = dto.Id };
}
private static List<Medium>? MapMedia(List<MediumDto>? media)
{
if (media == null)
{
return null;
}
return media.Select(MapMedium).ToList();
}
private static Medium MapMedium(MediumDto dto) => private static Medium MapMedium(MediumDto dto) =>
new() new()
@ -67,50 +50,7 @@ public static class ArchivedMapper
Preview = dto.Preview Preview = dto.Preview
}; };
private static Entities.Common.Files? MapFiles(FilesDto? dto) private static Entities.Common.Files? MapFiles(FilesDto? dto) => dto == null
{ ? null
if (dto == null) : new Entities.Common.Files { Full = CommonMapper.MapFull(dto.Full), Drm = CommonMapper.MapDrm(dto.Drm) };
{
return null;
}
return new Entities.Common.Files { Full = MapFull(dto.Full), Drm = MapDrm(dto.Drm) };
}
private static Entities.Common.Full? MapFull(FullDto? dto) =>
dto == null ? null : new Entities.Common.Full { Url = dto.Url };
private static Entities.Common.Drm? MapDrm(DrmDto? dto)
{
if (dto == null)
{
return null;
}
return new Entities.Common.Drm
{
Manifest = MapManifest(dto.Manifest), Signature = MapSignature(dto.Signature)
};
}
private static Entities.Common.Manifest? MapManifest(ManifestDto? dto) =>
dto == null ? null : new Entities.Common.Manifest { Dash = dto.Dash };
private static Entities.Common.Signature? MapSignature(SignatureDto? dto) =>
dto == null ? null : new Entities.Common.Signature { Dash = MapDash(dto.Dash) };
private static Entities.Common.Dash? MapDash(DashDto? dto)
{
if (dto == null)
{
return null;
}
return new Entities.Common.Dash
{
CloudFrontPolicy = dto.CloudFrontPolicy,
CloudFrontSignature = dto.CloudFrontSignature,
CloudFrontKeyPairId = dto.CloudFrontKeyPairId
};
}
} }

View File

@ -0,0 +1,115 @@
using OF_DL.Models.Dtos.Common;
using OF_DL.Models.Entities.Common;
namespace OF_DL.Models.Mappers;
public static class CommonMapper
{
public static Author? MapAuthor(AuthorDto? dto)
{
if (dto == null || dto.Id == 0)
{
return null;
}
return new Author { Id = dto.Id };
}
private static Dash? MapDash(DashDto? dto)
{
if (dto == null || (
string.IsNullOrEmpty(dto.CloudFrontPolicy) &&
string.IsNullOrEmpty(dto.CloudFrontSignature) &&
string.IsNullOrEmpty(dto.CloudFrontKeyPairId)))
{
return null;
}
return new Dash
{
CloudFrontPolicy = dto.CloudFrontPolicy,
CloudFrontSignature = dto.CloudFrontSignature,
CloudFrontKeyPairId = dto.CloudFrontKeyPairId
};
}
public static Drm? MapDrm(DrmDto? dto)
{
if (dto == null || (dto.Manifest == new ManifestDto() && dto.Signature == new SignatureDto()))
{
return null;
}
return new Drm { Manifest = MapManifest(dto.Manifest), Signature = MapSignature(dto.Signature) };
}
public static Files? MapFiles(FilesDto? dto)
{
if (dto == null)
{
return null;
}
Full? full = MapFull(dto.Full);
Preview? preview = MapPreview(dto.Preview);
Drm? drm = MapDrm(dto.Drm);
if (full == null && preview == null && drm == null)
{
return null;
}
return new Files { Full = MapFull(dto.Full), Preview = MapPreview(dto.Preview), Drm = MapDrm(dto.Drm) };
}
public static Full? MapFull(FullDto? dto)
{
if (dto == null || string.IsNullOrEmpty(dto.Url))
{
return null;
}
return new Full { Url = dto.Url };
}
public static Manifest? MapManifest(ManifestDto? dto)
{
if (dto == null || string.IsNullOrEmpty(dto.Dash))
{
return null;
}
return new Manifest { Dash = dto.Dash };
}
public static Preview? MapPreview(PreviewDto? dto)
{
if (dto == null || string.IsNullOrEmpty(dto.Url))
{
return null;
}
return new Preview { Url = dto.Url };
}
public static Signature? MapSignature(SignatureDto? dto)
{
if (dto == null)
{
return null;
}
Dash? dash = MapDash(dto.Dash);
return dash == null ? null : new Signature { Dash = dash };
}
public static VideoSources? MapVideoSources(VideoSourcesDto? dto)
{
if (dto == null || (string.IsNullOrEmpty(dto._240) && string.IsNullOrEmpty(dto._720)))
{
return null;
}
return new VideoSources { _240 = dto._240, _720 = dto._720 };
}
}

View File

@ -59,8 +59,6 @@ public static class HighlightsMapper
Files = MapFiles(dto.Files) Files = MapFiles(dto.Files)
}; };
private static Files? MapFiles(FilesDto? dto) => dto == null ? null : new Files { Full = MapFull(dto.Full) }; private static Files? MapFiles(FilesDto? dto) =>
dto == null ? null : new Files { Full = CommonMapper.MapFull(dto.Full) };
private static Full? MapFull(FullDto? dto) =>
dto == null ? null : new Full { Url = dto.Url };
} }

View File

@ -63,35 +63,17 @@ public static class MessagesMapper
private static List<MessageEntities.Medium>? MapMedia(List<MediumDto>? media) => private static List<MessageEntities.Medium>? MapMedia(List<MediumDto>? media) =>
media?.Select(MapMedium).ToList(); media?.Select(MapMedium).ToList();
private static FromUser? MapFromUser(FromUserDto? dto) => private static FromUser? MapFromUser(FromUserDto? dto)
dto?.Id == null ? null : new FromUser { Id = (long)dto.Id };
private static Files? MapFiles(FilesDto? dto) =>
dto == null ? null : new Files { Full = MapFull(dto.Full), Drm = MapDrm(dto.Drm) };
private static Full? MapFull(FullDto? dto) => dto == null ? null : new Full { Url = dto.Url };
private static Drm? MapDrm(DrmDto? dto) => dto == null
? null
: new Drm { Manifest = MapManifest(dto.Manifest), Signature = MapSignature(dto.Signature) };
private static Manifest? MapManifest(ManifestDto? dto) => dto == null ? null : new Manifest { Dash = dto.Dash };
private static Signature? MapSignature(SignatureDto? dto) =>
dto == null ? null : new Signature { Dash = MapDash(dto.Dash) };
private static Dash? MapDash(DashDto? dto)
{ {
if (dto == null) if (dto?.Id == null || dto.Id == 0)
{ {
return null; return null;
} }
return new Dash return new FromUser { Id = dto.Id.Value };
{
CloudFrontPolicy = dto.CloudFrontPolicy,
CloudFrontSignature = dto.CloudFrontSignature,
CloudFrontKeyPairId = dto.CloudFrontKeyPairId
};
} }
private static Files? MapFiles(FilesDto? dto) => dto == null
? null
: new Files { Full = CommonMapper.MapFull(dto.Full), Drm = CommonMapper.MapDrm(dto.Drm) };
} }

View File

@ -36,7 +36,7 @@ public static class PostMapper
mapped.Id = dto.Id; mapped.Id = dto.Id;
mapped.PostedAt = dto.PostedAt; mapped.PostedAt = dto.PostedAt;
mapped.Author = MapSingleAuthor(dto.Author); mapped.Author = CommonMapper.MapAuthor(dto.Author);
mapped.Text = dto.Text; mapped.Text = dto.Text;
mapped.RawText = dto.RawText; mapped.RawText = dto.RawText;
mapped.IsOpened = dto.IsOpened; mapped.IsOpened = dto.IsOpened;
@ -53,7 +53,7 @@ public static class PostMapper
{ {
Id = dto.Id, Id = dto.Id,
PostedAt = dto.PostedAt, PostedAt = dto.PostedAt,
Author = MapAuthor(dto.Author), Author = CommonMapper.MapAuthor(dto.Author),
Text = dto.Text, Text = dto.Text,
RawText = dto.RawText, RawText = dto.RawText,
IsOpened = dto.IsOpened, IsOpened = dto.IsOpened,
@ -63,12 +63,6 @@ public static class PostMapper
Preview = dto.Preview Preview = dto.Preview
}; };
private static Author? MapAuthor(AuthorDto? dto) =>
dto == null ? null : new Author { Id = dto.Id };
private static Author? MapSingleAuthor(AuthorDto? dto) =>
dto == null ? null : new Author { Id = dto.Id };
private static List<Medium>? MapMedia(List<MediumDto>? media) => private static List<Medium>? MapMedia(List<MediumDto>? media) =>
media?.Select(MapMedium).ToList(); media?.Select(MapMedium).ToList();
@ -84,39 +78,12 @@ public static class PostMapper
private static Files? MapFiles(FilesDto? dto) => dto == null private static Files? MapFiles(FilesDto? dto) => dto == null
? null ? null
: new Files { Full = MapFull(dto.Full), Preview = MapPreview(dto.Preview), Drm = MapDrm(dto.Drm) }; : new Files
private static Full? MapFull(FullDto? dto) =>
dto == null || string.IsNullOrEmpty(dto.Url) ? null : new Full { Url = dto.Url };
private static Preview? MapPreview(PreviewDto? dto) =>
dto == null || string.IsNullOrEmpty(dto.Url) ? null : new Preview { Url = dto.Url };
private static Drm MapDrm(DrmDto? dto) => new()
{
Manifest = MapManifest(dto?.Manifest), Signature = MapSignature(dto?.Signature)
};
private static Manifest? MapManifest(ManifestDto? dto) =>
dto == null ? null : new Manifest { Dash = dto.Dash };
private static Signature? MapSignature(SignatureDto? dto) =>
dto == null ? null : new Signature { Dash = MapDash(dto.Dash) };
private static Dash? MapDash(DashDto? dto)
{
if (dto == null)
{ {
return null; Full = CommonMapper.MapFull(dto.Full),
} Preview = CommonMapper.MapPreview(dto.Preview),
Drm = CommonMapper.MapDrm(dto.Drm)
return new Dash
{
CloudFrontPolicy = dto.CloudFrontPolicy,
CloudFrontSignature = dto.CloudFrontSignature,
CloudFrontKeyPairId = dto.CloudFrontKeyPairId
}; };
}
private static List<Medium>? MapSingleMedia(List<MediumDto>? media) => private static List<Medium>? MapSingleMedia(List<MediumDto>? media) =>
media?.Select(MapSingleMedium).ToList(); media?.Select(MapSingleMedium).ToList();
@ -129,7 +96,7 @@ public static class PostMapper
CanView = dto.CanView, CanView = dto.CanView,
Preview = dto.Preview, Preview = dto.Preview,
Files = MapSingleFiles(dto.Files), Files = MapSingleFiles(dto.Files),
VideoSources = MapVideoSources(dto.VideoSources) VideoSources = CommonMapper.MapVideoSources(dto.VideoSources)
}; };
private static Files? MapSingleFiles(FilesDto? dto) private static Files? MapSingleFiles(FilesDto? dto)
@ -141,42 +108,9 @@ public static class PostMapper
return new Files return new Files
{ {
Full = MapSingleFull(dto.Full), Preview = MapSinglePreview(dto.Preview), Drm = MapSingleDrm(dto.Drm) Full = CommonMapper.MapFull(dto.Full),
Preview = CommonMapper.MapPreview(dto.Preview),
Drm = CommonMapper.MapDrm(dto.Drm)
}; };
} }
private static Full? MapSingleFull(FullDto? dto) =>
dto == null || string.IsNullOrEmpty(dto.Url) ? null : new Full { Url = dto.Url };
private static Preview? MapSinglePreview(PreviewDto? dto) =>
dto == null || string.IsNullOrEmpty(dto.Url) ? null : new Preview { Url = dto.Url };
private static Drm? MapSingleDrm(DrmDto? dto) => dto == null
? null
: new Drm { Manifest = MapSingleManifest(dto.Manifest), Signature = MapSingleSignature(dto.Signature) };
private static Manifest? MapSingleManifest(ManifestDto? dto) =>
dto == null ? null : new Manifest { Dash = dto.Dash };
private static Signature? MapSingleSignature(SignatureDto? dto) =>
dto == null ? null : new Signature { Dash = MapSingleDash(dto.Dash) };
private static Dash? MapSingleDash(DashDto? dto)
{
if (dto == null)
{
return null;
}
return new Dash
{
CloudFrontPolicy = dto.CloudFrontPolicy,
CloudFrontSignature = dto.CloudFrontSignature,
CloudFrontKeyPairId = dto.CloudFrontKeyPairId
};
}
private static VideoSources? MapVideoSources(VideoSourcesDto? dto) => dto == null
? null
: new VideoSources { _240 = dto._240, _720 = dto._720 };
} }

View File

@ -1,5 +1,4 @@
using OF_DL.Models.Dtos.Purchased; using OF_DL.Models.Dtos.Purchased;
using OF_DL.Models.Dtos.Common;
using MessageDtos = OF_DL.Models.Dtos.Messages; using MessageDtos = OF_DL.Models.Dtos.Messages;
using CommonEntities = OF_DL.Models.Entities.Common; using CommonEntities = OF_DL.Models.Entities.Common;
using MessageEntities = OF_DL.Models.Entities.Messages; using MessageEntities = OF_DL.Models.Entities.Messages;
@ -42,14 +41,18 @@ public static class PurchasedMapper
Previews = dto.Previews, Previews = dto.Previews,
Preview = dto.Preview, Preview = dto.Preview,
FromUser = MapFromUser(dto.FromUser), FromUser = MapFromUser(dto.FromUser),
Author = MapAuthor(dto.Author) Author = CommonMapper.MapAuthor(dto.Author)
}; };
private static CommonEntities.FromUser MapFromUser(FromUserDto? dto) => private static CommonEntities.FromUser? MapFromUser(FromUserDto? dto)
dto == null ? new CommonEntities.FromUser() : new CommonEntities.FromUser { Id = dto.Id }; {
if (dto == null || dto.Id == 0)
{
return null;
}
private static CommonEntities.Author MapAuthor(AuthorDto? dto) => return new CommonEntities.FromUser { Id = dto.Id };
dto == null ? new CommonEntities.Author() : new CommonEntities.Author { Id = dto.Id }; }
private static List<MessageEntities.Medium>? MapMedia(List<MessageDtos.MediumDto>? media) => private static List<MessageEntities.Medium>? MapMedia(List<MessageDtos.MediumDto>? media) =>
media?.Select(MessagesMapper.MapMedium).ToList(); media?.Select(MessagesMapper.MapMedium).ToList();

View File

@ -26,7 +26,6 @@ public static class StoriesMapper
Files = MapFiles(dto.Files) Files = MapFiles(dto.Files)
}; };
private static Files MapFiles(FilesDto? dto) => new() { Full = MapFull(dto?.Full) }; private static Files MapFiles(FilesDto? dto) =>
new() { Full = CommonMapper.MapFull(dto?.Full) };
private static Full MapFull(FullDto? dto) => new() { Url = dto?.Url };
} }

View File

@ -29,7 +29,7 @@ public static class StreamsMapper
{ {
Id = dto.Id, Id = dto.Id,
PostedAt = dto.PostedAt, PostedAt = dto.PostedAt,
Author = MapAuthor(dto.Author), Author = CommonMapper.MapAuthor(dto.Author),
Text = dto.Text, Text = dto.Text,
RawText = dto.RawText, RawText = dto.RawText,
Price = dto.Price, Price = dto.Price,
@ -39,9 +39,6 @@ public static class StreamsMapper
Preview = dto.Preview Preview = dto.Preview
}; };
private static Author? MapAuthor(AuthorDto? dto) =>
dto == null ? null : new Author { Id = dto.Id };
private static List<Medium>? MapMedia(List<MediumDto>? media) => private static List<Medium>? MapMedia(List<MediumDto>? media) =>
media?.Select(MapMedium).ToList(); media?.Select(MapMedium).ToList();
@ -55,7 +52,7 @@ public static class StreamsMapper
return null; return null;
} }
Full? full = MapFull(dto.Full); Full? full = CommonMapper.MapFull(dto.Full);
Drm? drm = MapDrm(dto.Drm); Drm? drm = MapDrm(dto.Drm);
if (full == null && drm == null) if (full == null && drm == null)
@ -66,43 +63,15 @@ public static class StreamsMapper
return new Files { Full = full, Drm = drm }; return new Files { Full = full, Drm = drm };
} }
private static Full? MapFull(FullDto? dto) =>
dto == null || string.IsNullOrEmpty(dto.Url) ? null : new Full { Url = dto.Url };
private static Drm? MapDrm(DrmDto? dto) private static Drm? MapDrm(DrmDto? dto)
{ {
if (dto?.Manifest == null || string.IsNullOrEmpty(dto.Manifest.Dash)) Manifest? manifest = CommonMapper.MapManifest(dto?.Manifest);
if (manifest == null)
{ {
return null; return null;
} }
Dash? dash = MapDash(dto.Signature.Dash); Signature? signature = CommonMapper.MapSignature(dto?.Signature);
if (dash == null) return signature == null ? null : new Drm { Manifest = manifest, Signature = signature };
{
return null;
}
return new Drm
{
Manifest = new Manifest { Dash = dto.Manifest.Dash }, Signature = new Signature { Dash = dash }
};
}
private static Dash? MapDash(DashDto? dto)
{
if (dto == null ||
string.IsNullOrEmpty(dto.CloudFrontPolicy) ||
string.IsNullOrEmpty(dto.CloudFrontSignature) ||
string.IsNullOrEmpty(dto.CloudFrontKeyPairId))
{
return null;
}
return new Dash
{
CloudFrontPolicy = dto.CloudFrontPolicy,
CloudFrontSignature = dto.CloudFrontSignature,
CloudFrontKeyPairId = dto.CloudFrontKeyPairId
};
} }
} }

View File

@ -22,15 +22,8 @@ public static class UserListsMapper
return mapped; return mapped;
} }
public static List<UsersList> FromDto(List<UsersListDto>? dto) public static List<UsersList> FromDto(List<UsersListDto>? dto) =>
{ dto == null ? [] : dto.Select(MapUsersList).ToList();
if (dto == null)
{
return [];
}
return dto.Select(MapUsersList).ToList();
}
private static UserListItem MapListItem(UserListItemDto dto) => private static UserListItem MapListItem(UserListItemDto dto) =>
new() { Id = dto.Id, Name = dto.Name }; new() { Id = dto.Id, Name = dto.Name };