/// <summary> /// Gets the existing connection if any for DB /// Used for updating an existing medicine /// </summary> /// <param name="medicineId">Medicine to be retrieved from DB</param> public MedicineViewModel(int medicineId) { db = App.ViewModel.db; db.Refresh(RefreshMode.OverwriteCurrentValues, db.Medicines); // Clear any unsaved changes Medicine = db.Medicines.SingleOrDefault(m => m.Id == medicineId); }
/// <summary> /// Create a new medicine with current instance /// Sets the created_at and updated_at columns also /// </summary> internal void Create() { db = App.ViewModel.db; db.Medicines.InsertOnSubmit(this.medicine); db.SubmitChanges(); }
public GroupViewModel(string groupName) { db = App.ViewModel.db; Group = db.Groups.Single(g => g.Name == groupName); }
/// <summary> /// Gets the existing connection if any for DB /// Creates an empty Medicine instance /// Used for creating a new medicine as we do not have any ID for it yet /// </summary> public MedicineViewModel() { db = App.ViewModel.db; db.Refresh(RefreshMode.OverwriteCurrentValues, db.Medicines); // Clear any unsaved changes Medicine = new Medicine(); }
/// <summary> /// Shows the error message that the group name already exists /// Disposes the Connection and creates a new connection as there is no way to clear changes but not submitted /// </summary> private void HandleDuplicateGroup() { MessageBox.Show(String.Format("Group \"{0}\" already exists.\r\nUse the existing one or choose a different name", this.group.Name), "OOPS!", MessageBoxButton.OK); db.Dispose(); db = App.ViewModel.GetNewConnection(); }
/// <summary> /// Sets the DB Context /// Loads all groups if asked for /// </summary> /// <param name="loadAllGroups">Flag to indicate whether to load groups</param> public GroupViewModel() { db = App.ViewModel.db; Group = new Group(); LoadAllGroups(); }
public MainViewModel() { db = new MedicineContext(App.DbConnection); }
/// <summary> /// Instantiates new DB connection. Used when we dispose the old connection /// </summary> /// <returns>New connection to DB</returns> public MedicineContext GetNewConnection() { db = new MedicineContext(App.DbConnection); return db; }
/// <summary> /// :: Important :: /// Aim: Update DB to latest version using currentDBVersion variable. /// /// Working: /// Just like migrations in Rails. /// Each case in the switch has changes in that specific DB version /// default case implies latest version and so no changes /// At the end of each case we add goto to next case to fall through until we reach the latest version /// /// Notes: /// Target version will always be 'one' version higher than the current one. Increments in DB versions is always by 1 /// All the changes to schema from the targetVersion will be run /// </summary> private void UpdateDB() { using (var db = new MedicineContext(App.DbConnection)) { var adapter = db.CreateDatabaseSchemaUpdater(); switch (GetNextDBVersion(adapter.DatabaseSchemaVersion)) { case 0: // Version 0. Initial DB goto case 1; case 1: // Version 1 changes // New Columns adapter.AddColumn<Medicine>("IsUsing"); adapter.AddColumn<Medicine>("Quantity"); adapter.AddColumn<Medicine>("QuantityType"); adapter.AddColumn<Medicine>("ReminderIds"); adapter.AddColumn<Medicine>("DosageType"); // New Tables adapter.AddTable<Group>(); adapter.AddTable<Medicine_Group>(); goto default; default: // DB upto date break; } adapter.DatabaseSchemaVersion = currentDBVersion; adapter.Execute(); } }
/// <summary> /// Inserts sample data into the database /// </summary> /// <param name="db">Database context to use</param> private void InsertSampleData(MedicineContext db) { // Sample Medicines Medicine m1 = new Medicine() { Name = "saridon (sample)", MfgDate = DateTime.Now, ExpDate = DateTime.Now.AddMonths(3), CureFor = "Headache", Dosage = "4", DosageType = "cups", AgeGroup = "18+", Price = 20.50F, BoughtAddress = "Near Canara bank, Kormangala, Bangalore. Ph: 080-23399239", Quantity = 2, QuantityType = "pills", CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; db.Medicines.InsertOnSubmit(m1); Medicine m2 = new Medicine() { Name = "nice (sample)", MfgDate = DateTime.Now, ExpDate = DateTime.Now.AddMonths(3), IsUsing = true, CureFor = "Fever", Dosage = "3", DosageType = "spoons", AgeGroup = "20+", Price = 25.50F, BoughtAddress = "Indiranagar, Bangalore. Ph: 080-23399239", Quantity = 4, QuantityType = "pills", CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; db.Medicines.InsertOnSubmit(m2); db.SubmitChanges(); // Sample Groups Group g1 = new Group() { Name = "Is Using (sample)", CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; db.Groups.InsertOnSubmit(g1); Group g2 = new Group() { Name = "Not Using (sample)", CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; db.Groups.InsertOnSubmit(g2); db.SubmitChanges(); // Sample Associations db.MedicinesGroups.InsertOnSubmit(new Medicine_Group() { Medicine = m1, Group = g1 }); db.MedicinesGroups.InsertOnSubmit(new Medicine_Group() { Medicine = m2, Group = g2 }); db.SubmitChanges(); }
/// <summary> /// Creates the Medicines Catalogue DB if it doesn't exist /// Adds a sample medicine /// </summary> private void CreateDB() { using (var db = new MedicineContext(DbConnection)) { if (!db.DatabaseExists()) { db.CreateDatabase(); InsertSampleData(db); db.SubmitChanges(); var adapter = db.CreateDatabaseSchemaUpdater(); adapter.DatabaseSchemaVersion = currentDBVersion; adapter.Execute(); } } }