MongoDbNoSql

using System; using System.Collections.Generic; using System.Windows.Forms; using ToDoDesktop.Model; using ToDoDesktop.DataAccess; using System.Text.Json; using MongoDB.Bson; using System.Globalization; namespace ToDoDesktop { public partial class Form1 : Form { Listtodos = new List (); List ids = new List (); public Form1() { InitializeComponent(); } private void btnNew_Click(object sender, EventArgs e) { if (validateInput()) { todo td = new todo(); td.Description = txtDescription.Text; td.Status = cboStatus.Text; td.Priority = cboPriority.Text; td.Created_Date = txtCreated.Text; td.Due_Date = txtDue.Text; td.Done = chkDone.Checked; string strTodo = JsonSerializer.Serialize(td); //MessageBox.Show(strTodo); Write dw = new Write(); if (!dw.InsertData(strTodo)) { MessageBox.Show("Error in writing data"); } RefreshData(); } } private bool validateInput() { try { bool vStatus = true; DateTime vDate; if (txtDescription.Text == "") { MessageBox.Show("Description is a mandatory field"); vStatus = false; } if (!cboStatus.Items.Contains(cboStatus.Text)) { MessageBox.Show("Invalid Status"); vStatus = false; } if (!cboPriority.Items.Contains(cboPriority.Text)) { MessageBox.Show("Invalid Priority"); vStatus = false; } if (txtCreated.Text != "") { txtCreated.Text = DateTime.Parse(txtCreated.Text).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); if (txtCreated.Text == "") { MessageBox.Show("Invalid Created Date"); vStatus = false; } } else { txtCreated.Text = DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); } if (txtDue.Text != "") { txtDue.Text = DateTime.Parse(txtDue.Text).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); if (txtDue.Text == "") { MessageBox.Show("Invalid Due Date"); vStatus = false; } } else { MessageBox.Show("Missing Due Date"); vStatus = false; } return vStatus; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return false; } } private void Form1_Load(object sender, EventArgs e) { SelectData(); } private void btnUpdate_Click(object sender, EventArgs e) { if (validateInput()) { todo td = new todo(); td._id = txtId.Text; td.Description = txtDescription.Text; td.Status = cboStatus.Text; td.Priority = cboPriority.Text; td.Created_Date = txtCreated.Text; td.Due_Date = txtDue.Text; td.Done = chkDone.Checked; string strTodo = JsonSerializer.Serialize(td); //MessageBox.Show(strTodo); Update du = new Update(); if (!du.UpdateData(td)) { MessageBox.Show("Error in updating data"); } RefreshData(); } } private void btnDelete_Click(object sender, EventArgs e) { if (txtId.Text != "") { Delete dd = new Delete(); dd.DeleteData(txtId.Text); RefreshData(); } } private void btnSelect_Click(object sender, EventArgs e) { SelectData(); } private void lstTodo_SelectedIndexChanged(object sender, EventArgs e) { //MessageBox.Show(lstTodo.SelectedItem.ToString()); Read dr = new Read(); //string id = ids<> int intItem = lstTodo.SelectedIndex; if (intItem != -1) { todo rdb = new todo(); BsonDocument bd = new BsonDocument(); bd = dr.ReadData(ids[intItem]); if (bd != null) { txtId.Text = bd.GetValue("_id").ToString(); txtDescription.Text = bd.GetValue("Description").ToString(); cboStatus.Text = bd.GetValue("Status").ToString(); cboPriority.Text = bd.GetValue("Priority").ToString(); txtCreated.Text = bd.GetValue("Created_Date").ToString(); txtDue.Text = bd.GetValue("Due_Date").ToString(); chkDone.Checked = bd.GetValue("Done").ToBoolean(); Application.DoEvents(); } //string[] arr = lstTodo.SelectedItem.ToString().Split('\t'); //txtId.Text = arr[0]; //txtDescription.Text = arr[1]; //Application.DoEvents(); btnNew.Visible = false; btnUpdate.Visible = true; btnDelete.Visible = true; } else { MessageBox.Show("Unable to find record for selection"); } } private void lstTodo_Click(object sender, EventArgs e) { //MessageBox.Show(sender.) } private void btnClear_Click(object sender, EventArgs e) { ClearData(); } private void ClearData() { txtId.Text = ""; txtDescription.Text = ""; cboStatus.Text = ""; cboPriority.Text = ""; txtCreated.Text = ""; txtDue.Text = ""; chkDone.Checked = false; Application.DoEvents(); btnNew.Visible = true; btnUpdate.Visible = false; btnDelete.Visible = false; } private void SelectData() { Read dr = new Read(); List docs = new List (); string strSort; if (radStatus.Checked) { strSort = "Status"; } else { if (radDue.Checked) { strSort = "Due_Date"; } else { strSort = "Priority"; } } docs = dr.SelectData(chkDisplayDone.Checked, strSort); lstTodo.Items.Clear(); ids.Clear(); todo td = new todo(); string SortData; foreach (BsonDocument doc in docs) { ids.Add(doc.GetValue("_id").ToString()); //if (strSort == "Status") //{ // SortData = doc.GetValue("Status").ToString(); //} //else if (strSort == "Due_Date") //{ // SortData = doc.GetValue("Due_Date").ToString(); //} //else //{ // SortData = doc.GetValue("Priority").ToString(); //} SortData = doc.GetValue(strSort).ToString(); lstTodo.Items.Add(SortData + '\t' + doc.GetValue("Description").ToString()); //MessageBox.Show("Also clear the data from the text fields below. Create functions for this and others."); } } private void RefreshData() { ClearData(); SelectData(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ToDoDesktop.Model { public class todo { public string? _id { get; set; } public string? Description { get; set; } public string? Status { get; set; } public string? Priority { get; set; } public string? Created_Date { get; set; } public string? Due_Date { get; set; } public bool? Done { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MongoDB.Driver; using MongoDB.Bson; namespace ToDoDesktop.DataAccess { internal class Delete { public bool DeleteData(string jsonData) { var settings = MongoClientSettings.FromConnectionString( "mongodb+srv://theSkinner:NortholtMartin14113609@cluster0.mtpjo.mongodb.net/" + "myDatabase?retryWrites=true&w=majority"); var client = new MongoClient(settings); var database = client.GetDatabase("myDatabase"); var collection = database.GetCollection ("myCollection"); ObjectId o_id = new ObjectId(jsonData); var deleteFilter = Builders .Filter.Eq("_id", o_id); collection.DeleteOne(deleteFilter); return true; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ToDoDesktop.Model; using MongoDB.Driver; using MongoDB.Bson; namespace ToDoDesktop.DataAccess { internal class Read { public List SelectData(bool Done, string SortBy) { var settings = MongoClientSettings.FromConnectionString( "mongodb+srv://theSkinner:NortholtMartin14113609@cluster0.mtpjo.mongodb.net/" + "myDatabase?retryWrites=true&w=majority"); var client = new MongoClient(settings); var database = client.GetDatabase("myDatabase"); var collection = database.GetCollection ("myCollection"); var sort = Builders .Sort.Ascending(SortBy); List documents = new List (); if (!Done) // Select only those not Done { var selectFilter = Builders .Filter.Eq("Done", false); documents = collection.Find(selectFilter).Sort(sort).ToList(); } else // Select all { documents = collection.Find(new BsonDocument()).Sort(sort).ToList(); } return documents; } public BsonDocument ReadData(string id) { var settings = MongoClientSettings.FromConnectionString( "mongodb+srv://theSkinner:NortholtMartin14113609@cluster0.mtpjo.mongodb.net/" + "myDtabase?retryWrites=true&w=majority"); var client = new MongoClient(settings); var database = client.GetDatabase("myDatabase"); var collection = database.GetCollection ("myCollection"); //var sort = Builders .Sort.Ascending(SortBy); ObjectId o_id = new ObjectId(id); var filter = Builders .Filter.Eq("_id", o_id); var document = collection.Find(filter).FirstOrDefault(); return document; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MongoDB.Driver; using MongoDB.Bson; using ToDoDesktop.Model; namespace ToDoDesktop.DataAccess { internal class Update { public bool UpdateData(todo td) { var settings = MongoClientSettings.FromConnectionString( "mongodb+srv://theSkinner:NortholtMartin14113609@cluster0.mtpjo.mongodb.net/" + "myDatabase?retryWrites=true&w=majority"); var client = new MongoClient(settings); var database = client.GetDatabase("myDatabase"); var collection = database.GetCollection ("myCollection"); // Get the document ObjectId o_id = new ObjectId(td._id); var updateFilter = Builders .Filter.Eq("_id", o_id); // Mark what has changed var update = Builders .Update.Set("Description", td.Description) .Set("Status", td.Status) .Set("Priority", td.Priority) .Set("Created_Date", td.Created_Date) .Set("Due_Date", td.Due_Date) .Set("Done", td.Done); // Update collection.UpdateOne(updateFilter, update); return true; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MongoDB.Driver; using MongoDB.Bson; using System.Text.Json; using ToDoDesktop.Model; using MongoDB.Bson.Serialization; namespace ToDoDesktop.DataAccess { internal class Write { public bool InsertData(string jsonData) { try { BsonDocument bd = BsonDocument.Parse(jsonData); var settings = MongoClientSettings.FromConnectionString( "mongodb+srv://theSkinner:NortholtMartin14113609@cluster0.mtpjo.mongodb.net/" + "myDatabase?retryWrites=true&w=majority"); var client = new MongoClient(settings); var database = client.GetDatabase("myDatabase"); var collection = database.GetCollection ("myCollection"); bd.Remove("_id"); collection.InsertOne(bd); return true; } catch (Exception ex) { return false; } } } }