Ejemplo n.º 1
0
        private void SetSongTagsOnUpload(Song currentSong, SongUploadEditViewModel song, ApplicationDbContext db)
        {
            if (song.Tags == null)
            {
                song.Tags = string.Empty;
            }

            var tagsStringSplit = song.Tags.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                                  .OrderBy(t => t)
                                  .Select(t => t.ToLower())
                                  .Distinct();

            currentSong.Tags.Clear();

            foreach (var tagString in tagsStringSplit)
            {
                Tag tag = db.Tags.FirstOrDefault(t => t.Name == tagString);

                if (tag == null)
                {
                    tag = new Tag()
                    {
                        Name = tagString
                    };
                    db.Tags.Add(tag);
                }

                currentSong.Tags.Add(tag);
            }
        }
Ejemplo n.º 2
0
        public ActionResult Edit(SongUploadEditViewModel song)
        {
            var currentSong = db.Songs.FirstOrDefault(s => s.Id == song.Id);

            if (currentSong == null)
            {
                this.AddNotification("Song does not exist", NotificationType.ERROR);
                return(RedirectToAction("MySongs"));
            }

            currentSong.Id          = song.Id;
            currentSong.Artist      = song.Artist;
            currentSong.Title       = song.Title;
            currentSong.Description = song.Description;
            currentSong.GenreId     = song.GenreId;
            this.SetSongTagsOnUpload(currentSong, song, db);

            if (currentSong.Artist == null || currentSong.Title == null)
            {
                this.AddNotification("Fill all requred fields, marked with \"*\"", NotificationType.ERROR);
                return(RedirectToAction("Edit"));
            }

            db.SaveChanges();

            this.AddNotification($"Song '{song.Artist} - {song.Title}' has been updated.", NotificationType.INFO);

            return(RedirectToAction($"Details/{currentSong.Id}"));
        }