Beispiel #1
0
        public ISpecification <Pub> GetSpecification(PubQuery query)
        {
            var specification = new PubSpecification();

            specification.SetPage(query.Page);
            specification.SetPageSize(query.PageSize);

            specification
            .AddInclude(bg => bg.PubBoardGames)
            .AddInclude($"{nameof(Pub.PubBoardGames)}.{nameof(PubBoardGame.BoardGame)}")
            .AddInclude(pub => pub.Address)
            .AddInclude(pub => pub.LikedPubs);

            if (!string.IsNullOrWhiteSpace(query.BoardGameId))
            {
                specification
                .AndAlso(pub => pub.PubBoardGames
                         .Any(game => game.BoardGameId == query.BoardGameId));
            }

            if (!string.IsNullOrWhiteSpace(query.City))
            {
                specification
                .AndAlso(pub => pub.Address.City == query.City);
            }

            if (!string.IsNullOrWhiteSpace(query.Search))
            {
                specification
                .AndAlso(pub => pub.Name.Contains(query.Search));
            }

            if (!string.IsNullOrWhiteSpace(query.OrderBy))
            {
                SetOrderBy(specification, query.OrderBy);
            }

            return(specification);
        }
Beispiel #2
0
        public async Task <IActionResult> Get([FromQuery] PubQuery query)
        {
            try
            {
                var specification = _pubSpeczilla.GetSpecification(query);
                var result        = await _pubService.GetPaginationResultAsync(specification);

                if (!result.HasValue)
                {
                    return(NotFound());
                }

                var responseResult = result.Value.ToResponse();

                if (!string.IsNullOrWhiteSpace(UserEmail))
                {
                    var likedPubs = _hexadoUserService.GetLikedPubs(UserEmail);
                    if (likedPubs.HasValue)
                    {
                        foreach (var likedPub in likedPubs.Value)
                        {
                            var pub = responseResult.Results
                                      .FirstOrDefault(rr => rr.Id == likedPub.Id);
                            if (pub != null)
                            {
                                pub.IsLikedByUser = true;
                            }
                        }
                    }
                }

                return(OkJson(responseResult));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error while retrieving pubs!");
                return(InternalServerErrorJson(ex));
            }
        }