Beispiel #1
0
        public async Task <ActionResult <IEnumerable <Event> > > GetEvents(GeoPoint location, double?radius, int?genre, int?host, int?artist)
        {
            var authorizedUser = await Authentication.GetAuthenticatedUserAsync(_context, Request);

            if (authorizedUser.Result is UnauthorizedResult)
            {
                return(Unauthorized());
            }

            if (authorizedUser.Value == null)
            {
                return(Unauthorized());
            }

            using (var db = new GigFinderContext())
            {
                if (location == null && !genre.HasValue && !host.HasValue && !artist.HasValue)
                {
                    return(await db.Events.Include(h => h.EventGenres).Where(e => e.HostId == authorizedUser.Value.Id || e.Participations.Any(p => p.ArtistId == authorizedUser.Value.Id)).ToListAsync());
                }

                var query = (IQueryable <Event>)db.Events.Include(h => h.EventGenres);
                if (location != null && radius.HasValue)
                {
                    query = query.Where(e => GeoPoint.CalculateDistance(location, new GeoPoint()
                    {
                        Longitude = e.Longitude, Latitude = e.Latitude
                    }) <= radius.Value);
                }
                if (genre.HasValue)
                {
                    query = query.Where(e => e.EventGenres.Any(eg => eg.GenreId == genre));
                }
                if (host.HasValue)
                {
                    query = query.Where(e => e.HostId == host);
                }
                if (artist.HasValue)
                {
                    query = query.Where(e => e.Participations.Any(p => p.ArtistId == artist));
                }

                return(await query.ToListAsync());
            }
        }