Пример #1
0
 public Band GetBandById(Guid bandId)
 {
     using (var context = new MusicArchiveContext())
     {
         return(context.Bands
                .Include("Albums")
                .Include("Members").Single(band => band.Id == bandId));
     }
 }
Пример #2
0
        public AlbumDetailDto GetAlbum(Guid id)
        {
            using (var context = new MusicArchiveContext())
            {
                var matchingAlbum = context.Albums
                                    .Include("Reviews")
                                    .Include("Tracks")
                                    .Single(album => album.Id == id);

                var albumDto = Mapper.Map <Album, AlbumDetailDto>(matchingAlbum);
                albumDto.ReviewAverage = albumDto.Reviews.Count > 0 ? (int)albumDto.Reviews.Average(dto => dto.Rating) : 0;

                return(albumDto);
            }
        }
Пример #3
0
        public void Review(ReviewDto review)
        {
            using (var context = new MusicArchiveContext())
            {
                var album = context.Albums.Single(a => a.Id == review.Id);

                var newReview = new Review
                {
                    Rating     = review.Rating,
                    ReviewText = review.ReviewText
                };

                album.Reviews.Add(newReview);
                context.SaveChanges();
            }
        }
Пример #4
0
        public HttpResponseMessage GetStats()
        {
            using (var context = new MusicArchiveContext())
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                var bandCount  = context.Bands.Count();
                var albumCount = context.Albums.Count();
                stopwatch.Stop();

                var statsMessage = string.Format("{0} bands, {1} albums on site. {2} milliseconds to retrive stats",
                                                 bandCount, albumCount, stopwatch.ElapsedMilliseconds);

                return(Request.CreateResponse(HttpStatusCode.OK, new { content = statsMessage }));
            }
        }
Пример #5
0
        public void AddBand(NewBandBindingModel newBand)
        {
            using (var context = new MusicArchiveContext())
            {
                var band = new Band
                {
                    Name           = newBand.Name,
                    CountryOfOrgin = newBand.CountryOfOrigin,
                    Genre          = newBand.Genre,
                    FormedIn       = newBand.YearOfFormation,
                    LyricalThemes  = JoinTags(newBand.LyricalThemes)
                };

                context.Bands.Add(band);
                context.SaveChanges();
            }
        }
Пример #6
0
        public IEnumerable <Band> Search(BandSearchInformation bandSearchInformation, out int totalMatches)
        {
            using (var context = new MusicArchiveContext())
            {
                IQueryable <Band> matches = new EnumerableQuery <Band>(context.Bands);

                if (!string.IsNullOrWhiteSpace(bandSearchInformation.BandName))
                {
                    matches = matches.Where(b => b.Name.Contains(bandSearchInformation.BandName));
                }

                if (!string.IsNullOrWhiteSpace(bandSearchInformation.CountryOfOrigin))
                {
                    matches = matches.Where(band1 => band1.CountryOfOrgin == bandSearchInformation.CountryOfOrigin);
                }

                if (!string.IsNullOrWhiteSpace(bandSearchInformation.Genre))
                {
                    matches = matches.Where(band1 => band1.Genre == bandSearchInformation.Genre);
                }

                if (!string.IsNullOrWhiteSpace(bandSearchInformation.Label))
                {
                    //matches = matches.Where(band1 => band1.CurrentLabel == bandSearchInformation.Label);
                }

                if (!string.IsNullOrWhiteSpace(bandSearchInformation.YearOfFormation))
                {
                    matches = matches.Where(band1 => band1.FormedIn == bandSearchInformation.YearOfFormation); //TODO make this an actual date...
                }

                if (!string.IsNullOrWhiteSpace(bandSearchInformation.LyricalThemes))
                {
                    matches = matches.Where(band1 => band1.LyricalThemes == bandSearchInformation.LyricalThemes);
                }

                totalMatches = matches.Count();

                return(matches.OrderBy(band => band.Name).Skip(bandSearchInformation.StartingRecordNumber).Take(bandSearchInformation.PageSize).ToList());
            }
        }
Пример #7
0
        private BandStatAggregation GetBandsBy(Func <MusicArchiveContext, IQueryable <IGrouping <string, Band> > > source)
        {
            using (var context = new MusicArchiveContext())
            {
                var genreStats = new BandStatAggregation();

                Dictionary <string, int> genreCounts = new Dictionary <string, int>();

                foreach (IGrouping <string, Band> genre in source(context))
                {
                    if (genre.Key == null)
                    {
                        if (genreCounts.ContainsKey(string.Empty))
                        {
                            genreCounts[string.Empty] += genre.Count();
                        }
                        else
                        {
                            genreCounts.Add(string.Empty, genre.Count());
                        }
                    }
                    else if (genreCounts.ContainsKey(genre.Key))
                    {
                        genreCounts[genre.Key] += genre.Count();
                    }
                    else
                    {
                        genreCounts.Add(genre.Key, genre.Count());
                    }

                    genreStats.Genres = genreCounts.Keys.ToList();
                    genreStats.Counts = genreCounts.Values.ToList();
                }

                return(genreStats);
            }
        }