klippa

example2

Sep 15th, 2020
747
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ClientReturnApiService : IClientReturnApiService
  2. {
  3.     private readonly IV2ReturnService _v2ReturnService;
  4.     private readonly IItemsGateway _itemsGateway;
  5.  
  6.     private readonly IClientReturnMappingService _mappingService;
  7.  
  8.     public ClientReturnApiService(
  9.         IV2ReturnService v2ReturnService,
  10.         IItemsGateway itemsGateway,
  11.         IClientReturnMappingService mappingService)
  12.     {
  13.         _mappingService = mappingService;
  14.         _v2ReturnService = v2ReturnService;
  15.         _itemsGateway = itemsGateway;
  16.     }
  17.  
  18.     public async Task<ClientReturnResponse> GetByNumber(string returnNumber)
  19.     {
  20.         var clientReturn = await _v2ReturnService.GetClientReturn(returnNumber, true, true);
  21.  
  22.         return clientReturn != null
  23.             ? _mappingService.ToClientReturnResponse(clientReturn)
  24.             : null;
  25.     }
  26.  
  27.     public async Task<ICollection<ReturnResponse>> GetReturns(long clientId)
  28.     {
  29.         var returns = await _v2ReturnService.GetClientReturns(clientId, true);
  30.  
  31.         return await Task.WhenAll(returns.Select(ToReturnResponse));
  32.     }
  33.  
  34.     public async Task<ICollection<ClientReturnResponse>> GetClientReturns(long clientId)
  35.     {
  36.         var returns = await _v2ReturnService.GetClientReturns(clientId, true, true);
  37.  
  38.         return returns.Select(_mappingService.ToClientReturnResponse).ToArray();
  39.     }
  40.  
  41.     private async Task<ReturnResponse> ToReturnResponse(ClientReturn clientReturn)
  42.     {
  43.         var items = await GetReturnItemResponses(clientReturn);
  44.  
  45.         var response = new ReturnResponse
  46.         {
  47.             Id = clientReturn.Id,
  48.             Number = clientReturn.ReturnNumber,
  49.             CreatedAt = clientReturn.Moment,
  50.             Address = clientReturn.Address,
  51.             TimeSlot = new ReturnTimeSlotResponse
  52.             {
  53.                 DateFrom = clientReturn.DateFrom,
  54.                 DateTo = clientReturn.DateTo
  55.             },
  56.             Items = items,
  57.             Type = clientReturn.Type,
  58.             ClientType = clientReturn.Type.ToNamed(),
  59.             IsDeletable = !_v2ReturnService.ValidateDelete(clientReturn).HasErrors,
  60.             State = clientReturn.State.ToString(),
  61.             ClientState = clientReturn.State.ToNamed(),
  62.             RejectionComment = clientReturn.RejectionComment
  63.         };
  64.  
  65.         return response;
  66.     }
  67.  
  68.     private async Task<ReturnItemResponse[]> GetReturnItemResponses(ClientReturn clientReturn)
  69.     {
  70.         var returnItems = clientReturn.Items;
  71.  
  72.         var itemIds = returnItems.Select(x => x.ItemId).Distinct();
  73.  
  74.         var photos = returnItems.GroupBy(x => x.ItemId).ToDictionary(x => x.Key, x => x.SelectMany(y => y.Images.Select(z => z.Url)));
  75.  
  76.         return (await _itemsGateway.GetItemsInfoAndMerchant(itemIds)).Select(x => new ReturnItemResponse
  77.         {
  78.             Id = (int)x.Id,
  79.             ImageUrl = x.Info?.CoverImage,
  80.             Quantity = returnItems.Count(y => y.ItemId == x.Id),
  81.             Photos = photos[x.Id].Distinct()
  82.         }).ToArray();
  83.     }
  84. }
RAW Paste Data