using Amazon.DynamoDBv2.Model; using System; using System.Collections.Generic; using System.Reflection; using System.Diagnostics; using BootCamp.DataAccess.Contract; using BootCamp.DataAccess.Common.AWS.DynamoDB; namespace BootCamp.DataAccess.Extension { public static class ProductDtoExtension { private static readonly Type albumDtoType = typeof(AlbumDto); private static readonly Type bookDtoType = typeof(BookDto); private static readonly Type movieDtoType = typeof(MovieDto); private static readonly Dictionary> productDtoPropertyCache = new Dictionary>(); /// /// Convert to product dto list. /// /// /// /// public static List ToProductDtoList(this List> queryResult, string productType) { if ((queryResult is null) || (queryResult.Count <= 0)) { return null; } var resultList = new List(); foreach (Dictionary item in queryResult) { var dto = item.ToProductDto(productType); resultList.Add(dto); } return resultList; } /// /// Converto to product dto. /// /// /// /// public static ProductDto ToProductDto(this Dictionary item, string productType) { if (item is null) { return null; } if (item.ContainsKey(nameof(AlbumDto.Artist))) { var productDto = new AlbumDto(); return productDtoPropertyCache.GetProductDto(item, albumDtoType, productDto); } else if (item.ContainsKey(nameof(BookDto.Author))) { var productDto = new BookDto(); return productDtoPropertyCache.GetProductDto(item, bookDtoType, productDto); } else if (item.ContainsKey(nameof(MovieDto.Director))) { var productDto = new MovieDto(); return productDtoPropertyCache.GetProductDto(item, movieDtoType, productDto); } return null; } /// /// Convert to delete item request details. /// /// /// public static Dictionary ToDeleteItemRequestDetail(this ProductDto dto) { var item = new Dictionary(); item.AddValue(nameof(dto.Title), dto.Title); if (dto is BookDto bookDto) { item.AddValue(nameof(bookDto.Author), bookDto.Author.ToAttributeValue()); } else if (dto is AlbumDto albumDto) { item.AddValue(nameof(albumDto.Artist), albumDto.Artist.ToAttributeValue()); } else if (dto is MovieDto movieDto) { item.AddValue(nameof(movieDto.Director), movieDto.Director.ToAttributeValue()); } return item; } } }