示例#1
0
        public async Task <GetUserAdvertisements.Response> GetUserAdvertisements(GetUserAdvertisements.Request request, CancellationToken cancellationToken)
        {
            var total = await _repository.Count(cancellationToken);

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

            string userId;

            if (string.IsNullOrEmpty(request.Id))
            {
                var userIdFromClaims = await _identityService.GetCurrentUserId(cancellationToken);

                if (string.IsNullOrEmpty(userIdFromClaims))
                {
                    throw new NoUserFound("Пользователь не найден");
                }
                userId = userIdFromClaims;
            }
            else
            {
                userId = request.Id;
            }

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

            if (result == null)
            {
                throw new NoUserAdFoundException($"Объявления пользователя с Id = {userId} не найдены");
            }

            return(new GetUserAdvertisements.Response
            {
                Items = result.Select(a => new GetUserAdvertisements.Response.Item
                {
                    Id = a.Id,
                    Title = a.Title,
                    Description = a.Description,
                    Cover = a.Cover,
                    CreatedDate = a.CreatedDate,
                    Price = a.Price,
                    Location = a.Location,

                    Images = a.Images.Select(i => new GetUserAdvertisements.Response.ImageResponse
                    {
                        Id = i.Id,
                        ImageUrl = S3Url + i.Name
                    }),
                    Category = new GetUserAdvertisements.Response.CategoryResponse
                    {
                        Id = a.Category.Id,
                        Name = a.Category.Name
                    },
                    Status = a.Status.ToString()
                }),
                Total = result.Total,
                Offset = request.Offset,
                Limit = request.Limit
            });
        }