예제 #1
0
 //function to add song to DB or XML, depending on where album is present. Calls AddSongInXML if album is not present in DB
 public bool AddSong(Song song, out string errMsg)
 {
     //Flag to check if album is presen in DB.
     bool isAlbumExist = false;
     errMsg = string.Empty;
     try
     {
         using (var dbConn = _dbConnectionFactory.OpenDbConnection())
         {
             using (var trans = dbConn.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
             {
                 if (dbConn.Select<Album>(x => x.Id == song.AlbumId).Count() > 0)
                 {
                     //Marked as true as album is present
                     isAlbumExist = true;
                     song.DateModified = DateTime.Now;
                     song.DateAdded = DateTime.Now;
                     dbConn.Insert<Song>(song);
                     trans.Commit();
                 }
             }
         }
         if (!isAlbumExist)
         {
             //Add song to XML as album is not present in DB.
             errMsg = AddSongInXML(song) == false ? "No album exist" : string.Empty;
         }
         return true;
     }
     catch (Exception ex)
     {
         _logger.Error("AlbumRepository.cs - GetAlbumSongs", ex);
         throw ex;
     }
 }
예제 #2
0
 public bool AddSongInXML(Song songToAdd)
 {
     try
     {
         string appdatafolder = HttpContext.Current.Server.MapPath("~/App_Data/Songs.xml");
         XmlDocument doc = new XmlDocument();
         doc.Load(appdatafolder);
         XmlNode album = doc.SelectSingleNode("music/artist/album [Id='" + songToAdd.AlbumId + "']");
         if (album != null)
         {
             XmlElement song = doc.CreateElement("song");
             song.SetAttribute("title", songToAdd.Title);
             song.SetAttribute("length", songToAdd.Length + ":" + (songToAdd.Length - (int)songToAdd.Length) * 60);
             XmlNode lastSong = album.LastChild;
             song.SetAttribute("SongId", (Convert.ToInt32(lastSong.Attributes["SongId"]) + 1).ToString());
             lock (this)
             {
                 doc.Save(appdatafolder);
             }
             return true;
         }
         else
         {
             return false;
         }
     }
     catch (Exception ex)
     {
         _logger.Error("AlbumRepository.cs - GetAlbumSongs", ex);
         throw ex;
     }
 }
예제 #3
0
        //Gets album and song info from XML. This will be called from GetAlbumSongs
        public Album GetAlbumSongsFromXML(string albumName, ref List<Song> songs)
        {
            Album album = null;
            string appdatafolder = HttpContext.Current.Server.MapPath("~/App_Data/Songs.xml");
            XmlReader xmlReader = XmlReader.Create(appdatafolder);
            string artist = string.Empty;
            while (xmlReader.Read())
            {
                if (xmlReader.MoveToContent() == XmlNodeType.Element &&
                    xmlReader.Name == "artist")
                {
                    artist = xmlReader.GetAttribute("name");
                }
                if (xmlReader.MoveToContent() == XmlNodeType.Element &&
                    xmlReader.Name == "album" &&
                    String.Equals(albumName, xmlReader.GetAttribute("title"), StringComparison.InvariantCultureIgnoreCase))
                {

                    album = new Album()
                    {
                        Id = Convert.ToInt32(xmlReader.GetAttribute("Id")),
                        Title = xmlReader.GetAttribute("title"),
                        ArtistName = artist
                    };
                    if (xmlReader.ReadToDescendant("song"))
                    {
                        do
                        {
                            Song s = new Song()
                            {
                                AlbumId = album.Id.Value,
                                Id = Convert.ToInt32(xmlReader.GetAttribute("SongId")),
                                Title = xmlReader.GetAttribute("title"),
                                Length = Convert.ToDouble(xmlReader.GetAttribute("length").Split(':')[0]) + (Convert.ToDouble(xmlReader.GetAttribute("length").Split(':')[1]) / 60)
                            };
                            songs.Add(s);
                        } while (xmlReader.ReadToNextSibling("song"));
                    }
                }
            }
            return album;
        }