Пример #1
0
 /// <summary>
 /// Copies the data from the given instance
 /// </summary>
 /// <param name="website"></param>
 public void Copy(Website website)
 {
     this.Name = website.Name;
     this.URL = website.URL;
     this.IsActive = website.IsActive;
     this.LastUpdatedOn = website.LastUpdatedOn;
 }
        /// <summary>
        /// Deletes the document from MongoDB
        /// </summary>
        /// <param name="strDataBaseName"></param>
        /// <param name="strTableName"></param>
        /// <param name="website"></param>
        /// <returns></returns>
        public Boolean Delete(String strDataBaseName, String strTableName, Website website)
        {
            //Declaratoins
            Boolean result = false;

            try
            {
                MongoCollection<Website> colData = GetServer().GetDatabase(strDataBaseName).GetCollection<Website>(strTableName);

                //Remove the document
                colData.Remove(Query.EQ("_id", website.Id));

                result = true;
            }
            catch (Exception ex)
            {
                Logger.Log("Delete:" + ex.Message);

            }

            return result;
        }
        /// <summary>
        /// Persists the website data to the database
        /// </summary>
        private void PersistData()
        {
            //Declaratoins
            DBUtility dbUtility = new DBUtility();
            String dbName = Configurations.DatabaseName;
            String tableName = Configurations.Websites;

            try
            {
                //Check if the data is valid
                if (!ValidateData())
                    return;

                Website website = new Website();

                website.Name = txtName.Text;
                website.URL = txtURL.Text;
                website.IsActive = true;
                website.LastUpdatedOn = DateTime.Now.ToShortDateString();

                //Update the data
                dbUtility.UpdateData(dbName, tableName, website);

                //Clear the textboxes
                txtName.Text = String.Empty;
                txtURL.Text = String.Empty;
            }
            catch (Exception ex)
            {
                DisplayMessage("Unable to update your data");
                Logger.Log("PersistData: " + ex.Message);
            }
        }
        /// <summary>
        /// Updates the existing data if present else inserts it as a new record
        /// </summary>
        /// <param name="strDataBaseName"></param>
        /// <param name="strTableName"></param>
        /// <param name="person"></param>
        /// <returns></returns>
        public Boolean UpdateData(String strDataBaseName, String strTableName, Website website)
        {
            //Declarations
            MongoCollection<Website> colData = GetServer().GetDatabase(strDataBaseName).GetCollection<Website>(strTableName);
            Boolean isExistingData = false;

            try
            {
                QueryDocument queryDocument = new QueryDocument("Name", website.Name);
                foreach (Website websiteToUpdate in colData.Find(queryDocument))
                {
                    if (websiteToUpdate.Name != String.Empty)
                    {
                        websiteToUpdate.Copy(website);
                        colData.Save(websiteToUpdate);
                        isExistingData = true;
                        break;
                    }
                }

                //If its not an existing data
                if (!isExistingData)
                    colData.Save(website);

            }
            catch (Exception ex)
            {
                Logger.Log("UpdateData: " + ex.Message);

            }

            return true;
        }