Example #1
0
        public override async Task <Model.User> GetById(int id)
        {
            var entity = await _context.Set <Database.User>()
                         .Include(i => i.UserRoles)
                         .Where(i => i.ID == id)
                         .SingleOrDefaultAsync();

            return(_mapper.Map <Model.User>(entity));
        }
        public override async Task <Model.Genre> Insert(GenreUpsertRequest request)
        {
            if (await _context.Genres.AnyAsync(i => i.Name == request.Name))
            {
                throw new UserException("Genre already exists!");
            }
            var entity = _mapper.Map <Database.Genre>(request);

            _context.Set <Database.Genre>().Add(entity);
            await _context.SaveChangesAsync();

            return(_mapper.Map <Model.Genre>(entity));
        }
        public override async Task <Model.Playlist> Update(int id, PlaylistUpsertRequest request)
        {
            var entity = _context.Playlists.Find(id);

            _context.Playlists.Attach(entity);
            _context.Playlists.Update(entity);


            foreach (var TrackID in request.Tracks)
            {
                var playlistTrack = await _context.PlaylistTracks
                                    .Where(i => i.TrackID == TrackID && i.PlaylistID == id)
                                    .SingleOrDefaultAsync();

                if (playlistTrack == null)
                {
                    var newPlaylistTrack = new PlaylistTrack()
                    {
                        PlaylistID = id,
                        TrackID    = TrackID
                    };
                    await _context.Set <PlaylistTrack>().AddAsync(newPlaylistTrack);
                }
            }


            foreach (var TrackID in request.TracksToDelete)
            {
                var playlistTrack = await _context.PlaylistTracks
                                    .Where(i => i.TrackID == TrackID && i.PlaylistID == id)
                                    .SingleOrDefaultAsync();

                if (playlistTrack != null)
                {
                    _context.Set <PlaylistTrack>().Remove(playlistTrack);
                }
            }

            _mapper.Map(request, entity);
            await _context.SaveChangesAsync();

            return(_mapper.Map <Model.Playlist>(entity));
        }
        public override async Task <Model.Album> Update(int id, AlbumUpsertRequest request)
        {
            var entity = _context.Albums.Find(id);

            _context.Albums.Attach(entity);
            _context.Albums.Update(entity);


            foreach (var TrackID in request.Tracks)
            {
                var albumTrack = await _context.AlbumTracks
                                 .Where(i => i.TrackID == TrackID && i.AlbumID == id)
                                 .SingleOrDefaultAsync();

                if (albumTrack == null)
                {
                    var newAlbumTrack = new Database.AlbumTrack()
                    {
                        AlbumID = id,
                        TrackID = TrackID
                    };
                    await _context.Set <Database.AlbumTrack>().AddAsync(newAlbumTrack);
                }
            }


            foreach (var TrackID in request.TracksToDelete)
            {
                var albumTrack = await _context.AlbumTracks
                                 .Where(i => i.TrackID == TrackID && i.AlbumID == id)
                                 .SingleOrDefaultAsync();

                if (albumTrack != null)
                {
                    _context.Set <Database.AlbumTrack>().Remove(albumTrack);
                }
            }

            _mapper.Map(request, entity);
            await _context.SaveChangesAsync();

            return(_mapper.Map <Model.Album>(entity));
        }
        public virtual async Task <TModel> Insert(TInsert request)
        {
            var entity = _mapper.Map <TDatabase>(request);

            _context.Set <TDatabase>().Add(entity);
            await _context.SaveChangesAsync();

            return(_mapper.Map <TModel>(entity));
        }
Example #6
0
        public virtual async Task <List <TModel> > Get(TSearch search)
        {
            var list = await _context.Set <TDatabase>().ToListAsync();

            return(_mapper.Map <List <TModel> >(list));
        }
        public override async Task <Model.Track> Update(int id, TrackUpsertRequest request)
        {
            var entity = _context.Tracks.Find(id);

            _context.Tracks.Attach(entity);
            _context.Tracks.Update(entity);

            entity.MP3File = request.MP3File;

            foreach (var ArtistID in request.ArtistsToDelete)
            {
                var trackArtist = await _context.TrackArtists
                                  .Where(i => i.TrackID == id && i.ArtistID == ArtistID)
                                  .SingleOrDefaultAsync();

                if (trackArtist != null)
                {
                    _context.Set <Database.TrackArtist>().Remove(trackArtist);
                }
            }
            await _context.SaveChangesAsync();

            foreach (var ArtistID in request.FeaturedArtists)
            {
                var trackArtist = await _context.TrackArtists
                                  .Where(i => i.TrackID == id && i.ArtistID == ArtistID)
                                  .SingleOrDefaultAsync();

                if (trackArtist == null)
                {
                    var newTrackArtist = new Database.TrackArtist()
                    {
                        TrackID         = id,
                        ArtistID        = ArtistID,
                        TrackArtistRole = TrackArtistRole.Feature
                    };
                    await _context.Set <Database.TrackArtist>().AddAsync(newTrackArtist);
                }
            }
            await _context.SaveChangesAsync();


            var tracMainkArtist = await _context.TrackArtists
                                  .Where(i => i.TrackID == id && i.TrackArtistRole == TrackArtistRole.Main)
                                  .SingleOrDefaultAsync();

            if (tracMainkArtist == null)
            {
                var newTrackArtist = new Database.TrackArtist()
                {
                    TrackID         = id,
                    ArtistID        = request.MainArtist,
                    TrackArtistRole = TrackArtistRole.Main
                };
                await _context.Set <Database.TrackArtist>().AddAsync(newTrackArtist);
            }
            else
            {
                tracMainkArtist.ArtistID = request.MainArtist;
            }
            await _context.SaveChangesAsync();



            foreach (var GenreID in request.Genres)
            {
                var trackGenre = await _context.TrackGenres
                                 .Where(i => i.TrackID == id && i.GenreID == GenreID)
                                 .SingleOrDefaultAsync();

                if (trackGenre == null)
                {
                    var newTrackArtist = new Database.TrackGenre()
                    {
                        TrackID = id,
                        GenreID = GenreID,
                    };
                    await _context.Set <Database.TrackGenre>().AddAsync(newTrackArtist);
                }
            }

            foreach (var GenreID in request.GenresToDelete)
            {
                var trackGenre = await _context.TrackGenres
                                 .Where(i => i.TrackID == id && i.GenreID == GenreID)
                                 .SingleOrDefaultAsync();

                if (trackGenre != null)
                {
                    _context.Set <Database.TrackGenre>().Remove(trackGenre);
                }
            }

            return(_mapper.Map <Model.Track>(entity));
        }