private bool AddLocation(out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsLocationNameValid(db, textBoxLocationName.Text, 0, out message)) { Location locationToAdd = new Location(); locationToAdd.Name = textBoxLocationName.Text; var trackedObjects = db.ChangeTracker.Entries(); db.Locations.Add(locationToAdd); db.SaveChanges(); selectedLocation = locationToAdd; result = true; } } } } catch (Exception ex) { message = $"Error adding Location '{textBoxLocationName.Text}': {ex.Message}"; } return(result); }
private void loadActionsFromPlant(MyGardenContext db) { Actions = db.Actions .Where(a => a.PlantId == SelectedPlant.Id) .OrderByDescending(a => a.ActionDate) .ToList(); }
private bool AddPlant(out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsPlantNameValid(db, textBoxPlantName.Text, 0, out message)) { Plant plantToAdd = new Plant(); plantToAdd.Name = textBoxPlantName.Text; PlantType selectedPlantType = comboBoxPlantTypes.SelectedItem as PlantType; plantToAdd.PlantTypeId = selectedPlantType.Id; var trackedObjects = db.ChangeTracker.Entries(); db.Plants.Add(plantToAdd); db.SaveChanges(); selectedPlant = plantToAdd; result = true; } } } } catch (Exception ex) { message = $"Error adding Plant '{textBoxPlantName.Text}': {ex.Message}"; } return(result); }
private void dataGridViewActions_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { if (dataGridViewActions.SelectedRows.Count > 0) { Action actionToDelete = dataGridViewActions.SelectedRows[0].Tag as Action; if (MessageBox.Show( $"Do you want to delete this action?", "Confirm Delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { db.Remove(actionToDelete); db.SaveChanges(); dataGridViewActions.ClearSelection(); } } } catch (Exception ex) { MessageBox.Show($"Error when deleting: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { e.Cancel = true; } } }
private bool UpdateActionType(ActionType actionType, out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsActionTypeNameValid(db, textBoxActionTypeName.Text, actionType.Id, out message)) { ActionType actionTypeToUpdate = db.ActionTypes.FirstOrDefault(p => p.Id == actionType.Id); if (actionTypeToUpdate != null) { actionTypeToUpdate.Name = textBoxActionTypeName.Text; } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); selectedActionType = actionTypeToUpdate; result = true; } } } } catch (Exception ex) { message = $"Error saving Action Type '{textBoxActionTypeName.Text}': {ex.Message}"; } return(result); }
private bool AddActionType(out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsActionTypeNameValid(db, textBoxActionTypeName.Text, 0, out message)) { ActionType actionTypeToAdd = new ActionType(); actionTypeToAdd.Name = textBoxActionTypeName.Text; var trackedObjects = db.ChangeTracker.Entries(); db.ActionTypes.Add(actionTypeToAdd); db.SaveChanges(); selectedActionType = actionTypeToAdd; result = true; } } } } catch (Exception ex) { message = $"Error adding Action Type '{textBoxActionTypeName.Text}': {ex.Message}"; } return(result); }
private void UserControlPlantTypesPerLocationOverview_Load(object sender, EventArgs e) { try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { Locations = db.Locations.ToList(); string query; if (OverviewType == OverviewTypes.PlantTypes) { query = "select * from PlantTypesPerLocation order by HarvestSeason desc;"; } else { query = "select * from PlantsPerLocation order by HarvestSeason desc;"; } PlantsPerLocationList = db.PlantsPerLocation.FromSqlRaw(query).ToList(); } } } catch (Exception ex) { MessageBox.Show($"Error getting data: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private bool UpdateLocation(Location location, out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsLocationNameValid(db, textBoxLocationName.Text, location.Id, out message)) { Location locationToUpdate = db.Locations.FirstOrDefault(p => p.Id == location.Id); if (locationToUpdate != null) { locationToUpdate.Name = textBoxLocationName.Text; } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); selectedLocation = locationToUpdate; result = true; } } } } catch (Exception ex) { message = $"Error saving Location '{textBoxLocationName.Text}': {ex.Message}"; } return(result); }
private void loadActionsFromLocation(MyGardenContext db) { Actions = db.Actions .Where(a => a.LocationId == SelectedLocation.Id) .OrderByDescending(a => a.ActionDate) .ToList(); }
private bool UpdatePlant(Plant plant, out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsPlantNameValid(db, textBoxPlantName.Text, plant.Id, out message)) { Plant plantToUpdate = db.Plants.FirstOrDefault(p => p.Id == plant.Id); if (plantToUpdate != null) { plantToUpdate.Name = textBoxPlantName.Text; PlantType selectedPlantType = comboBoxPlantTypes.SelectedItem as PlantType; plantToUpdate.PlantTypeId = selectedPlantType.Id; } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); selectedPlant = plantToUpdate; result = true; } } } } catch (Exception ex) { message = $"Error saving Plant '{textBoxPlantName.Text}': {ex.Message}"; } return(result); }
private bool IsActionTypeNameValid(MyGardenContext db, string name, int id, out string message) { message = null; if (name == "") { message = "Please enter a Action Type name."; return(false); } ActionType actionType = db.ActionTypes.FirstOrDefault(p => p.Name == name); if (actionType != null && actionType.Id != id) { message = $"There is already a Action Type called '{name}'. Please try another name."; return(false); } return(true); }
private bool IsLocationNameValid(MyGardenContext db, string name, int id, out string message) { message = null; if (name == "") { message = "Please enter a Location name."; return(false); } Location location = db.Locations.FirstOrDefault(p => p.Name == name); if (location != null && location.Id != id) { message = $"There is already a Location called '{name}'. Please, try another name."; return(false); } return(true); }
private bool IsPlantNameValid(MyGardenContext db, string name, int id, out string message) { message = null; if (name == "") { message = "Please enter a Plant name."; return(false); } Plant plant = db.Plants.FirstOrDefault(p => p.Name == name); if (plant != null && plant.Id != id) { message = $"There is already a Plant called '{name}'. Please, try another name."; return(false); } return(true); }
private void LoadData() { try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { var plantTypes = db.PlantTypes.ToList(); PlantTypes = db.PlantTypes .ToList(); } } } catch (Exception ex) { MessageBox.Show($"Error getting data: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void buttonDelete_Click(object sender, EventArgs e) { Plant plant = listBoxPlants.SelectedItem as Plant; if (MessageBox.Show( $"Do you want to delete {plant.Name}? \n" + $"(All Actions for this Plant will also be deleted)", "Confirm Delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { Plant plantToDelete = db.Plants.FirstOrDefault(p => p.Id == plant.Id); var trackedObjects = db.ChangeTracker.Entries(); db.Plants.Remove(plantToDelete); db.SaveChanges(); LoadData(); ClearSelected(); toolStripLabel.Text = $"'{plantToDelete.Name}' is deleted."; } } } catch (Exception ex) { MessageBox.Show($"Error deleting Plant '{plant.Name}': {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private bool UpdatePlantType(PlantType plantType, out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsPlantTypeNameValid(db, textBoxPlantTypeName.Text, plantType.Id, out message)) { PlantType plantTypeToUpdate = db.PlantTypes.FirstOrDefault(p => p.Id == plantType.Id); if (plantTypeToUpdate != null) { plantTypeToUpdate.Name = textBoxPlantTypeName.Text; plantTypeToUpdate.Perennial = radioButtonYes.Checked; plantTypeToUpdate.Soil = textBoxSoil.Text; plantTypeToUpdate.Nutrition = textBoxNutrition.Text; plantTypeToUpdate.Info = textBoxInfo.Text; } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); selectedPlantType = plantTypeToUpdate; result = true; } } } } catch (Exception ex) { message = $"Error saving Plant Type '{textBoxPlantTypeName.Text}': {ex.Message}"; } return(result); }
private bool AddPlantType(out string message) { bool result = false; message = null; try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (IsPlantTypeNameValid(db, textBoxPlantTypeName.Text, 0, out message)) { PlantType plantTypeToAdd = new PlantType(); plantTypeToAdd.Name = textBoxPlantTypeName.Text; plantTypeToAdd.Perennial = radioButtonYes.Checked; plantTypeToAdd.Soil = textBoxSoil.Text; plantTypeToAdd.Nutrition = textBoxNutrition.Text; plantTypeToAdd.Info = textBoxInfo.Text; var trackedObjects = db.ChangeTracker.Entries(); db.PlantTypes.Add(plantTypeToAdd); db.SaveChanges(); selectedPlantType = plantTypeToAdd; result = true; } } } } catch (Exception ex) { message = $"Error adding Plant Type '{textBoxPlantTypeName.Text}': {ex.Message}"; } return(result); }
private void dataGridViewActions_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } Action actionToUpdate; if (e.RowIndex == dataGridViewActions.NewRowIndex) { actionToUpdate = new Action(); } else { actionToUpdate = dataGridViewActions.Rows[e.RowIndex].Tag as Action; } var cell = dataGridViewActions.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (actionToUpdate.Id == 0) { if (dataGridViewActions.Rows[e.RowIndex].Cells["Location"].Value == null || dataGridViewActions.Rows[e.RowIndex].Cells["Plant"].Value == null || dataGridViewActions.Rows[e.RowIndex].Cells["ActionType"].Value == null) { return; } } if (!NeedsUpdating(actionToUpdate, cell)) { return; } try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { if (actionToUpdate.Id != 0) { actionToUpdate = db.Actions.FirstOrDefault(a => a.Id == actionToUpdate.Id); if (cell.OwningColumn.Name == "ActionType") { actionToUpdate.ActionType = cell.Value as ActionType; } if (cell.OwningColumn.Name == "ActionDate") { actionToUpdate.ActionDate = DateTime.Parse(cell.Value.ToString()); } if (cell.OwningColumn.Name == "HarvestSeason") { actionToUpdate.HarvestSeason = short.Parse(cell.Value.ToString()); } } else { if (selectedLocation == null) { var location = dataGridViewActions.Rows[e.RowIndex].Cells["Location"].Value as Location; actionToUpdate.LocationId = location.Id; } if (selectedPlant == null) { var plant = dataGridViewActions.Rows[e.RowIndex].Cells["Plant"].Value as Plant; actionToUpdate.PlantId = plant.Id; } var actionType = dataGridViewActions.Rows[e.RowIndex].Cells["ActionType"].Value as ActionType; actionToUpdate.ActionTypeId = actionType.Id; db.Add(actionToUpdate); } var trackedObjects = db.ChangeTracker.Entries(); db.SaveChanges(); } } } catch (Exception ex) { MessageBox.Show($"Error when saving: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void loadData() { try { using (var db = new MyGardenContext()) { if (db.Database.CanConnect()) { DataGridViewComboBoxColumn comboBoxColumn; Plants = db.Plants.ToList(); ActionTypes = db.ActionTypes.ToList(); Locations = db.Locations.ToList(); dataGridViewActions.Columns.Clear(); dataGridViewActions.Rows.Clear(); if (selectedLocation == null) { comboBoxColumn = new DataGridViewComboBoxColumn { HeaderText = "Location:", Name = "Location" }; dataGridViewActions.Columns.Add(comboBoxColumn); } else { loadActionsFromLocation(db); dataGridViewActions.Columns.Add("Location", "Location:"); dataGridViewActions.Columns["Location"].Visible = false; } if (selectedPlant == null) { comboBoxColumn = new DataGridViewComboBoxColumn { HeaderText = "Plant:", Name = "Plant" }; dataGridViewActions.Columns.Add(comboBoxColumn); } else { loadActionsFromPlant(db); dataGridViewActions.Columns.Add("Plant", "Plant:"); dataGridViewActions.Columns["Plant"].Visible = false; } comboBoxColumn = new DataGridViewComboBoxColumn { HeaderText = "Action Type:", Name = "ActionType" }; dataGridViewActions.Columns.Add(comboBoxColumn); dataGridViewActions.Columns.Add("ActionDate", "Action Date and Time:"); dataGridViewActions.Columns.Add("HarvestSeason", "Harvest Season:"); loadDataGridView(); } } } catch (Exception ex) { MessageBox.Show($"Error getting data: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }