Ejemplo n.º 1
0
        public async Task <GetFavorites.Response> GetFavorites(GetFavorites.Request request, CancellationToken cancellationToken)
        {
            var userId = await _identityService.GetCurrentUserId(cancellationToken);

            if (string.IsNullOrEmpty(userId))
            {
                throw new UserNotFoundException("Пользователь не найден");
            }

            var total = await _repository.Count(a => a.UserId == userId, cancellationToken);

            if (total == 0)
            {
                return(new GetFavorites.Response
                {
                    Total = 0,
                    Offset = request.Offset,
                    Limit = request.Limit
                });
            }

            var items = await _repository.FindFavorites(userId, request.Offset, request.Limit, request.SortBy, request.SortDirection, cancellationToken);

            return(new GetFavorites.Response
            {
                Items = items.Select(a => new GetFavorites.Item
                {
                    Id = a.AdvertisementId,
                    Title = a.Advertisement.Title,
                    Description = a.Advertisement.Description,
                    CreatedDate = a.Advertisement.CreatedDate,
                    Price = a.Advertisement.Price,
                    Status = a.Advertisement.Status.ToString(),
                    Cover = a.Advertisement.Cover,
                    Images = a.Advertisement.Images.Select(i => new GetFavorites.ImageResponse
                    {
                        Id = i.Id,
                    }),
                    Category = new GetFavorites.CategoryResponse
                    {
                        Id = a.Advertisement.Category.Id,
                        Name = a.Advertisement.Category.Name
                    },
                    Location = a.Advertisement.Location
                }),
                Total = items.Total,
                Limit = request.Limit,
                Offset = request.Offset
            });
        }