public void AddPlayList(string userId, Playlist playListModel)
        {
            using (var db = new ApplicationDbContext())
            {
                var user = db.Users.FirstOrDefault(m => m.Id == userId);
                playListModel.AddDate = DateTime.Now;
                playListModel.PlaylistId = Guid.NewGuid().ToString();
                db.Playlist.Add(playListModel);
                if (user != null)
                {
                    user.Playlist.Add(playListModel);
                }

                db.SaveChanges();
            }
        }
        public void AddToVkPlayList(string userId, Song song)
        {
            using (var db = new ApplicationDbContext())
            {
                var user = db.Users.FirstOrDefault(m => m.Id == userId);
                var myPlaylist = db.Playlist.FirstOrDefault(m => m.PlaylistId == userId + "VK");
                song.AddDate = DateTime.Now;
                // song.SongId = Guid.NewGuid().ToString();

                var songExists = db.Songs.FirstOrDefault(m => m.SongId == song.SongId);

                if (songExists == null)
                {
                    db.Songs.Add(song);
                }
                if (myPlaylist == null)
                {
                    if (user != null)
                    {
                        var playlist = new Playlist
                        {
                            AddDate = DateTime.Now,
                            Note = "Vk плэйлист",
                            PlayListName = "Добавлено из вк",
                            PlaylistId = userId + "VK",

                        };

                        db.Playlist.Add(playlist);
                        user.Playlist.Add(playlist);

                        AddSongItemToPlayList(song, playlist, db);
                    }

                }
                else
                {
                    AddSongItemToPlayList(song, myPlaylist, db);
                }

                db.SaveChanges();
            }
        }
 public static PlaylistViewModel ToPlaylistViewModel(Playlist song)
 {
     Mapper.CreateMap<Playlist, PlaylistViewModel>();
     return Mapper.Map<Playlist, PlaylistViewModel>(song);
 }
        private static void CreateUpdateDefaultPlaylist(string songId, string userId, ApplicationDbContext db)
        {
            var user = (from entity in db.Users
                        where entity.Id == userId
                        select entity).FirstOrDefault();

            if (user != null && user.Playlist.Any(m => m.PlaylistId == userId) == false)
            {
                var defaultPlaylist = new Playlist
                {
                    AddDate = DateTime.Now,
                    PlaylistId = userId,
                    PlayListName = "Мои аудиозаписи",
                    DefaultPlaylist = true
                };

                db.Playlist.Add(defaultPlaylist);
                user.Playlist.Add(defaultPlaylist);

                var newRecord = new PlaylistItem
                {
                    SongId = songId,
                    AddDate = DateTime.Now,
                    PlaylistItemId = Guid.NewGuid().ToString(),
                    PlaylistId = defaultPlaylist.PlaylistId,
                    TrackPos = 1
                };

                db.PlaylistItem.Add(newRecord);
            }
            else
            {
                var defaultPlaylist = db.Playlist.FirstOrDefault(m => m.PlaylistId == userId);

                if (defaultPlaylist != null)
                {
                    var trackPos = db.PlaylistItem.Where(m => m.PlaylistId == defaultPlaylist.PlaylistId).Max(m => m.TrackPos);
                    var newRecord = new PlaylistItem
                    {
                        SongId = songId,
                        AddDate = DateTime.Now,
                        PlaylistItemId = Guid.NewGuid().ToString(),
                        PlaylistId = defaultPlaylist.PlaylistId,
                        TrackPos = trackPos + 1
                    };

                    db.PlaylistItem.Add(newRecord);
                }
            }
            db.SaveChanges();
        }
 private static void AddSongItemToPlayList(Song song, Playlist playlist, ApplicationDbContext db)
 {
     var newRecord = new PlaylistItem
     {
         SongId = song.SongId,
         AddDate = DateTime.Now,
         PlaylistItemId = Guid.NewGuid().ToString(),
         PlaylistId = playlist.PlaylistId,
     };
     db.PlaylistItem.Add(newRecord);
 }