Example #1
0
        public async Task <ActionResult> GetEvents(int?page, int?limit, decimal?lat, decimal?lng, string title)
        {
            try
            {
                IEnumerable <EventViewModel> _items = null;

                int _page  = page ?? 0;
                int _limit = limit ?? 0;

                int _totalpages = 0;

                _limit = _limit == 0 || _limit > 10 ? 10 : _limit;

                if (_page > 0 && _limit > 0)
                {
                    Expression <Func <Event, bool> > criteria = null;

                    if (string.IsNullOrEmpty(title) && lat == null && lng == null)
                    {
                        criteria = e => true;
                    }
                    else if (!string.IsNullOrEmpty(title))
                    {
                        criteria = e => e.Title == title;
                    }

                    IReadOnlyList <Event>             eventsTask = null;
                    EventFilterPaginatedSpecification filterPaginatedSpecification = null;

                    if (criteria != null)
                    {
                        filterPaginatedSpecification =
                            new EventFilterPaginatedSpecification(_limit * (_page - 1), _limit, criteria);

                        eventsTask = await _repository.List(filterPaginatedSpecification);
                    }
                    else if (lat != null && lng != null)
                    {
                        var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);

                        var myLocation =
                            geometryFactory.CreatePoint(new Coordinate(double.Parse(lng.ToString()), double.Parse(lat.ToString())));

                        criteria = e => e.Location.IsWithinDistance(myLocation, 3000);

                        var events = await _repository.GetWhere(criteria);

                        events = events.OrderBy(o => o.Location.Distance(myLocation));

                        int _skip = _limit * (_page - 1);

                        eventsTask = events.Skip(_skip).Take(_limit).ToList();
                    }

                    if (eventsTask != null)
                    {
                        _items = eventsTask.Select(s => new EventViewModel()
                        {
                            Id            = s.EventId,
                            Title         = s.Title,
                            Description   = s.Description,
                            Date          = s.Date,
                            Image         = s.Image,
                            Attendances   = s.Attendances,
                            WillYouAttend = s.WillYouAttend
                        });

                        int totalItems = await _repository.CountWhere(criteria);

                        _totalpages = int.Parse(Math.Ceiling(((decimal)totalItems / _limit)).ToString());
                    }
                }
                else
                {
                    var eventsTask = await _repository.GetAll();

                    if (eventsTask.Any())
                    {
                        _items = eventsTask.Select(s => new EventViewModel()
                        {
                            Id            = s.EventId,
                            Title         = s.Title,
                            Description   = s.Description,
                            Date          = s.Date,
                            Image         = s.Image,
                            Attendances   = s.Attendances,
                            WillYouAttend = s.WillYouAttend
                        });

                        int totalItems = await _repository.CountAll();

                        _items = _limit > 0 ? _items.Take(_limit) : _items;
                    }
                }

                if (_page == 0)
                {
                    _page = 1;
                }
                if (_totalpages == 0)
                {
                    _totalpages = 1;
                }

                return(Ok(new EventViewModelResponse()
                {
                    Page = _page,
                    Pages = _totalpages,
                    Items = _items
                }));
            }
            catch (Exception ex)
            {
                LogException(ex);
                return(StatusCode(500, "Error al obtener los eventos"));
            }
        }