diff --git a/OF DL.Core/Models/Entities/Purchased/ListItem.cs b/OF DL.Core/Models/Entities/Purchased/ListItem.cs index 45561be..9e21aca 100644 --- a/OF DL.Core/Models/Entities/Purchased/ListItem.cs +++ b/OF DL.Core/Models/Entities/Purchased/ListItem.cs @@ -17,9 +17,9 @@ public class ListItem public List? 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; } diff --git a/OF DL.Core/Models/Mappers/ArchivedMapper.cs b/OF DL.Core/Models/Mappers/ArchivedMapper.cs index 2ef0d09..ab722c6 100644 --- a/OF DL.Core/Models/Mappers/ArchivedMapper.cs +++ b/OF DL.Core/Models/Mappers/ArchivedMapper.cs @@ -28,7 +28,7 @@ public static class ArchivedMapper { Id = dto.Id, PostedAt = dto.PostedAt, - Author = MapAuthor(dto.Author), + Author = CommonMapper.MapAuthor(dto.Author), Text = dto.Text, Price = dto.Price, IsOpened = dto.IsOpened, @@ -37,25 +37,8 @@ public static class ArchivedMapper Preview = dto.Preview }; - private static Entities.Common.Author? MapAuthor(AuthorDto? dto) - { - if (dto == null) - { - return null; - } - - return new Entities.Common.Author { Id = dto.Id }; - } - - private static List? MapMedia(List? media) - { - if (media == null) - { - return null; - } - - return media.Select(MapMedium).ToList(); - } + private static List? MapMedia(List? media) => + media == null ? null : media.Select(MapMedium).ToList(); private static Medium MapMedium(MediumDto dto) => new() @@ -67,50 +50,7 @@ public static class ArchivedMapper Preview = dto.Preview }; - private static Entities.Common.Files? MapFiles(FilesDto? dto) - { - if (dto == null) - { - 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 - }; - } + private static Entities.Common.Files? MapFiles(FilesDto? dto) => dto == null + ? null + : new Entities.Common.Files { Full = CommonMapper.MapFull(dto.Full), Drm = CommonMapper.MapDrm(dto.Drm) }; } diff --git a/OF DL.Core/Models/Mappers/CommonMapper.cs b/OF DL.Core/Models/Mappers/CommonMapper.cs new file mode 100644 index 0000000..3312a2f --- /dev/null +++ b/OF DL.Core/Models/Mappers/CommonMapper.cs @@ -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 }; + } +} diff --git a/OF DL.Core/Models/Mappers/HighlightsMapper.cs b/OF DL.Core/Models/Mappers/HighlightsMapper.cs index 450ea00..60efa17 100644 --- a/OF DL.Core/Models/Mappers/HighlightsMapper.cs +++ b/OF DL.Core/Models/Mappers/HighlightsMapper.cs @@ -59,8 +59,6 @@ public static class HighlightsMapper Files = MapFiles(dto.Files) }; - private static Files? MapFiles(FilesDto? dto) => dto == null ? null : new Files { Full = MapFull(dto.Full) }; - - private static Full? MapFull(FullDto? dto) => - dto == null ? null : new Full { Url = dto.Url }; + private static Files? MapFiles(FilesDto? dto) => + dto == null ? null : new Files { Full = CommonMapper.MapFull(dto.Full) }; } diff --git a/OF DL.Core/Models/Mappers/MessagesMapper.cs b/OF DL.Core/Models/Mappers/MessagesMapper.cs index 52b9a8e..c416ab1 100644 --- a/OF DL.Core/Models/Mappers/MessagesMapper.cs +++ b/OF DL.Core/Models/Mappers/MessagesMapper.cs @@ -63,35 +63,17 @@ public static class MessagesMapper private static List? MapMedia(List? media) => media?.Select(MapMedium).ToList(); - 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) + private static FromUser? MapFromUser(FromUserDto? dto) { - if (dto == null) + if (dto?.Id == null || dto.Id == 0) { return null; } - return new Dash - { - CloudFrontPolicy = dto.CloudFrontPolicy, - CloudFrontSignature = dto.CloudFrontSignature, - CloudFrontKeyPairId = dto.CloudFrontKeyPairId - }; + return new FromUser { Id = dto.Id.Value }; } + + private static Files? MapFiles(FilesDto? dto) => dto == null + ? null + : new Files { Full = CommonMapper.MapFull(dto.Full), Drm = CommonMapper.MapDrm(dto.Drm) }; } diff --git a/OF DL.Core/Models/Mappers/PostMapper.cs b/OF DL.Core/Models/Mappers/PostMapper.cs index 00c1b8f..1634ce8 100644 --- a/OF DL.Core/Models/Mappers/PostMapper.cs +++ b/OF DL.Core/Models/Mappers/PostMapper.cs @@ -36,7 +36,7 @@ public static class PostMapper mapped.Id = dto.Id; mapped.PostedAt = dto.PostedAt; - mapped.Author = MapSingleAuthor(dto.Author); + mapped.Author = CommonMapper.MapAuthor(dto.Author); mapped.Text = dto.Text; mapped.RawText = dto.RawText; mapped.IsOpened = dto.IsOpened; @@ -53,7 +53,7 @@ public static class PostMapper { Id = dto.Id, PostedAt = dto.PostedAt, - Author = MapAuthor(dto.Author), + Author = CommonMapper.MapAuthor(dto.Author), Text = dto.Text, RawText = dto.RawText, IsOpened = dto.IsOpened, @@ -63,12 +63,6 @@ public static class PostMapper 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? MapMedia(List? media) => media?.Select(MapMedium).ToList(); @@ -84,39 +78,12 @@ public static class PostMapper private static Files? MapFiles(FilesDto? dto) => dto == null ? null - : new Files { Full = MapFull(dto.Full), Preview = MapPreview(dto.Preview), Drm = MapDrm(dto.Drm) }; - - 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) + : new Files { - return null; - } - - return new Dash - { - CloudFrontPolicy = dto.CloudFrontPolicy, - CloudFrontSignature = dto.CloudFrontSignature, - CloudFrontKeyPairId = dto.CloudFrontKeyPairId + Full = CommonMapper.MapFull(dto.Full), + Preview = CommonMapper.MapPreview(dto.Preview), + Drm = CommonMapper.MapDrm(dto.Drm) }; - } private static List? MapSingleMedia(List? media) => media?.Select(MapSingleMedium).ToList(); @@ -129,7 +96,7 @@ public static class PostMapper CanView = dto.CanView, Preview = dto.Preview, Files = MapSingleFiles(dto.Files), - VideoSources = MapVideoSources(dto.VideoSources) + VideoSources = CommonMapper.MapVideoSources(dto.VideoSources) }; private static Files? MapSingleFiles(FilesDto? dto) @@ -141,42 +108,9 @@ public static class PostMapper 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 }; } diff --git a/OF DL.Core/Models/Mappers/PurchasedMapper.cs b/OF DL.Core/Models/Mappers/PurchasedMapper.cs index 43b5830..9ace9e5 100644 --- a/OF DL.Core/Models/Mappers/PurchasedMapper.cs +++ b/OF DL.Core/Models/Mappers/PurchasedMapper.cs @@ -1,5 +1,4 @@ using OF_DL.Models.Dtos.Purchased; -using OF_DL.Models.Dtos.Common; using MessageDtos = OF_DL.Models.Dtos.Messages; using CommonEntities = OF_DL.Models.Entities.Common; using MessageEntities = OF_DL.Models.Entities.Messages; @@ -42,14 +41,18 @@ public static class PurchasedMapper Previews = dto.Previews, Preview = dto.Preview, FromUser = MapFromUser(dto.FromUser), - Author = MapAuthor(dto.Author) + Author = CommonMapper.MapAuthor(dto.Author) }; - private static CommonEntities.FromUser MapFromUser(FromUserDto? dto) => - dto == null ? new CommonEntities.FromUser() : new CommonEntities.FromUser { Id = dto.Id }; + private static CommonEntities.FromUser? MapFromUser(FromUserDto? dto) + { + if (dto == null || dto.Id == 0) + { + return null; + } - private static CommonEntities.Author MapAuthor(AuthorDto? dto) => - dto == null ? new CommonEntities.Author() : new CommonEntities.Author { Id = dto.Id }; + return new CommonEntities.FromUser { Id = dto.Id }; + } private static List? MapMedia(List? media) => media?.Select(MessagesMapper.MapMedium).ToList(); diff --git a/OF DL.Core/Models/Mappers/StoriesMapper.cs b/OF DL.Core/Models/Mappers/StoriesMapper.cs index 4ccdd03..70ef4aa 100644 --- a/OF DL.Core/Models/Mappers/StoriesMapper.cs +++ b/OF DL.Core/Models/Mappers/StoriesMapper.cs @@ -26,7 +26,6 @@ public static class StoriesMapper Files = MapFiles(dto.Files) }; - private static Files MapFiles(FilesDto? dto) => new() { Full = MapFull(dto?.Full) }; - - private static Full MapFull(FullDto? dto) => new() { Url = dto?.Url }; + private static Files MapFiles(FilesDto? dto) => + new() { Full = CommonMapper.MapFull(dto?.Full) }; } diff --git a/OF DL.Core/Models/Mappers/StreamsMapper.cs b/OF DL.Core/Models/Mappers/StreamsMapper.cs index 3b3e30d..caa61ee 100644 --- a/OF DL.Core/Models/Mappers/StreamsMapper.cs +++ b/OF DL.Core/Models/Mappers/StreamsMapper.cs @@ -29,7 +29,7 @@ public static class StreamsMapper { Id = dto.Id, PostedAt = dto.PostedAt, - Author = MapAuthor(dto.Author), + Author = CommonMapper.MapAuthor(dto.Author), Text = dto.Text, RawText = dto.RawText, Price = dto.Price, @@ -39,9 +39,6 @@ public static class StreamsMapper Preview = dto.Preview }; - private static Author? MapAuthor(AuthorDto? dto) => - dto == null ? null : new Author { Id = dto.Id }; - private static List? MapMedia(List? media) => media?.Select(MapMedium).ToList(); @@ -55,7 +52,7 @@ public static class StreamsMapper return null; } - Full? full = MapFull(dto.Full); + Full? full = CommonMapper.MapFull(dto.Full); Drm? drm = MapDrm(dto.Drm); if (full == null && drm == null) @@ -66,43 +63,15 @@ public static class StreamsMapper 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) { - if (dto?.Manifest == null || string.IsNullOrEmpty(dto.Manifest.Dash)) + Manifest? manifest = CommonMapper.MapManifest(dto?.Manifest); + if (manifest == null) { return null; } - Dash? dash = MapDash(dto.Signature.Dash); - if (dash == null) - { - 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 - }; + Signature? signature = CommonMapper.MapSignature(dto?.Signature); + return signature == null ? null : new Drm { Manifest = manifest, Signature = signature }; } } diff --git a/OF DL.Core/Models/Mappers/UserListsMapper.cs b/OF DL.Core/Models/Mappers/UserListsMapper.cs index 1ff4e32..7f0557a 100644 --- a/OF DL.Core/Models/Mappers/UserListsMapper.cs +++ b/OF DL.Core/Models/Mappers/UserListsMapper.cs @@ -22,15 +22,8 @@ public static class UserListsMapper return mapped; } - public static List FromDto(List? dto) - { - if (dto == null) - { - return []; - } - - return dto.Select(MapUsersList).ToList(); - } + public static List FromDto(List? dto) => + dto == null ? [] : dto.Select(MapUsersList).ToList(); private static UserListItem MapListItem(UserListItemDto dto) => new() { Id = dto.Id, Name = dto.Name };