Ejemplo n.º 1
0
        public object AddArtist(string name)
        {
            Artist artist = new Artist();
            artist.Name = name.Trim();
            artist.DateAdded = DateTime.Today;

            DB.Artists.Add(artist);
            DB.SaveChanges();

            return name + " added.";
        }
Ejemplo n.º 2
0
        public object SaveSong(int id, string artistName, string name, string lyrics, string notes)
        {
            /// 1. Find song by id, or create new song
            /// 2. Set song properties
            /// 3. Find the artist, by name
            /// 4. If the artist does not exist, create it
            /// 5. Set artist
            /// 6. Save changes and return the updated song

            /// 1
            Song song = DB.Songs.Find(id);
            if (song == null)
            {
                song = new Song();        // create a new one
                DB.Songs.Add(song);         // add to set
                song.DateAdded = DateTime.Today;
            }

            /// 2
            song.Name = name.Trim();
            song.Lyrics = lyrics.Trim();
            song.Notes = notes.Trim();

            /// 3
            Artist artist = DB.Artists.FirstOrDefault(x => x.Name == artistName);

            /// 4
            if (artist == null)
            {
                artist = new Artist() { Name = artistName, DateAdded = DateTime.Now };
                DB.Artists.Add(artist);
            }

            /// 5
            song.Artist = artist;

            /// 6
            DB.SaveChanges();
            return GetSong(song.Id);
        }