Exemple #1
0
        /// <summary>
        /// View the song history of the client.
        /// </summary>
        /// <param name="start">The index of the first result.</param>
        /// <param name="count">The number of results to return.</param>
        /// <param name="userKey">client mobile key.</param>
        /// <returns>The outcome of the opearation.</returns>
        public List<SongHistory> MobileViewSongHistory(int start, int count, long userKey)
        {
            int mobileID = -1;
            List<SongHistory> songHistory = new List<SongHistory>();
            using (DatabaseConnectivity db = new DatabaseConnectivity())
            {
                // Try to establish a database connection
                Response r = db.OpenConnection();
                if (r.error)
                    return (List<SongHistory>)Common.LogError(r.message, Environment.StackTrace, null, 0);

                // Convert the userKey to MobileID
                r = MobileKeyToID(userKey, out mobileID, db);
                if (r.error)
                    return (List<SongHistory>)Common.LogError(r.message, Environment.StackTrace, null, 0);

                // Make sure the client isn't already logged out.
                r = MobileCheckStatus(mobileID, "!0", db);
                if (r.error)
                    return (List<SongHistory>)Common.LogError(r.message, Environment.StackTrace, null, 0);

                r = db.MobileGetSongHistory(mobileID, start, count);
                if (r.error)
                    return (List<SongHistory>)Common.LogError(r.message, Environment.StackTrace, null, 0);

                // Case of empty playlists.
                if (r.message.Trim().Length == 0)
                    return songHistory;

                //"ID", "VenueID", "MobileID", "SongID", "DateSung"
                try
                {
                    string[] songHistoryLines = r.message.Trim().Split('\n');
                    foreach (string songHistoryLine in songHistoryLines)
                    {
                        string[] songHistoryParts = Common.splitByDel(songHistoryLine);
                        SongHistory sh = new SongHistory();

                        int vid;
                        if (!int.TryParse(songHistoryParts[1], out vid))
                            return (List<SongHistory>)Common.LogError("MobileGetSongHistory venueID parse fail from MobileSongHistory.", Environment.StackTrace, null, 0);
                        sh.venue = new Venue();
                        sh.venue.venueID = vid;

                        r = db.GetVenueName(vid);
                        if (r.error)
                            return (List<SongHistory>)Common.LogError(r.message, Environment.StackTrace, null, 0);
                        sh.venue.venueName = r.message.Trim();

                        r = db.GetVenueAddress(vid);
                        if (r.error)
                            return (List<SongHistory>)Common.LogError(r.message, Environment.StackTrace, null, 0);
                        sh.venue.venueAddress = r.message.Trim();

                        int songID;
                        if(!int.TryParse(songHistoryParts[3], out songID))
                            return (List<SongHistory>)Common.LogError("MobileGetSongHistory songID parse fail from MobileSongHistory.", Environment.StackTrace, null, 0);

                        Song song;
                        r = Common.GetSongInformation(songID, vid, mobileID,out song, db, false);
                        if(r.error)
                            return (List<SongHistory>)Common.LogError(r.message, Environment.StackTrace, null, 0);
                        sh.song = song;
                        sh.date = Convert.ToDateTime(songHistoryParts[4]);
                        songHistory.Add(sh);
                    }
                    return songHistory;
                }
                catch (Exception e)
                {
                    return (List<SongHistory>)Common.LogError(e.Message, e.StackTrace, null, 0);
                }
            }
        }
        /// Get the song history for a mobile user. ONLY sets the SONGID, VENUEID, and DATESUNG in each songhistory object.
        /// </summary>
        /// <param name="userID">The mobile user ID</param>
        /// <param name="start">The starting index</param>
        /// <param name="count">The number of results.</param>
        /// <param name="history">Out stores the song history.</param>
        /// <returns>The outcome of the operation.</returns>
        internal ExpResponse MobileGetSongHistory(int userID, int start, int count, out List<SongHistory> history)
        {
            history = new List<SongHistory>();
            ExpResponse r = new ExpResponse();
            if (count == 0)
                return r;
            SqlCommand cmd = new SqlCommand("select VenueID, SongID, DateSung from MobileSongHistory where MobileID = @userID order by DateSung desc offset @start rows fetch next @count rows only;", con);
            cmd.Parameters.AddWithValue("@userID", userID);
            cmd.Parameters.AddWithValue("@start", start);
            cmd.Parameters.AddWithValue("@count", count);

            try
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        SongHistory sh = new SongHistory();
                        sh.venue = new Venue();
                        sh.song = new Song();
                        sh.venue.venueID = reader.GetInt32(0);
                        sh.song.ID = reader.GetInt32(1);
                        sh.date = reader.GetDateTime(2);
                        history.Add(sh);
                    }
                }
                return r;
            }
            catch (Exception e)
            {
                r.setErMsgStk(true, "Exception in MobileGetSongHistory: " + e.Message, e.StackTrace);
                return r;
            }
        }