private async void CreateButton_Click(object sender, EventArgs e) { using var createForm = new RecipeCreateDialogForm(_clientsService, _diseasesService, _recipeCreator); var dialogResult = createForm.ShowDialog(this); if (dialogResult == DialogResult.OK) { var createModel = createForm.DialogResultEntity; var recipeMedicaments = createModel.RecipeMedicaments.ToList(); createModel.RecipeMedicaments = null; var recipe = await _recipesService.CreateAsync(createModel); foreach (var recipeMedicament in recipeMedicaments) { recipeMedicament.RecipeId = recipe.Id; await _recipesMedicamentsService.CreateRelationAsync(recipeMedicament); var medicament = await _medicamentsService.ReadByIdAsync(recipeMedicament.MedicamentId); medicament.StockQuantity -= recipeMedicament.Dose; await _medicamentsService.UpdateAsync(medicament); } await RefreshRecipesDataGridViewAsync(); } createForm.Close(); }
private async void OriginalSubstitutesButton_Click(object sender, EventArgs e) { if (!_originalDataGridViewService.TryGetSelectedCellIndices(out _, out var rowIndex, true) || !_originalDataGridViewService.TryParseIntCell(OriginalColumnName.MedicamentId, rowIndex, out var originalMedicamentId)) { return; } var originalMedicament = await _medicamentsService.ReadByIdAsync(originalMedicamentId); var nextSubstitutesForm = new SubstitutesForm(originalMedicament, _substitutesService, _medicamentsService); Navigation.Push(nextSubstitutesForm); }
private async void EditButton_Click(object sender, EventArgs e) { if (!_dataGridViewService.TryGetSelectedCellIndices(out var columnIndex, out var rowIndex, true)) { return; } if (columnIndex == -1) { MessageBoxService.ShowIncorrectSelectionWarning("Select cell you want to edit."); return; } var columnName = _dataGridViewService.GetColumnName(columnIndex); if (columnName == ColumnName.Id) { MessageBoxService.ShowIncorrectSelectionWarning("You can't edit id."); return; } if (!_dataGridViewService.TryParseIntCell(ColumnName.Id, rowIndex, out var medicamentId)) { return; } var medicament = await _medicamentsService.ReadByIdAsync(medicamentId); switch (columnName) { case ColumnName.Name: { var oldValue = _dataGridViewService.GetCellValue(columnIndex, rowIndex); var editForm = new EditStringDialogForm(oldValue); editForm.ShowDialog(this); var editResult = editForm.DialogResult; var newValue = editForm.NewValue; editForm.Close(); if (editResult == DialogResult.Cancel) { return; } medicament.Name = newValue; break; } case ColumnName.StockQuantity: { if (!_dataGridViewService.TryParseDoubleCell(columnName, rowIndex, out var oldValue)) { return; } var editForm = new EditDoubleDialogForm(oldValue); editForm.ShowDialog(this); var editResult = editForm.DialogResult; var newValue = editForm.NewValue; editForm.Close(); if (editResult == DialogResult.Cancel) { return; } medicament.StockQuantity = newValue; break; } default: MessageBox.Show(@"Check database and debug code.", @"Unknown field.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } await _medicamentsService.UpdateAsync(medicament); await RefreshDataViewGridAsync(); }