private OVault convert(Vault model) { OVault vault = new OVault(); vault.amount = model.vAmount; vault.id = model.vId; vault.name = model.vName; return(vault); }
/// <summary> /// Save vault /// </summary> public OVault Save(OVault vault) { bool isNew = !vault.id.HasValue; // Validations if (vault.amount.HasValue && vault.amount.Value < 0) { throw new Exception("Invalid vault amount"); } if (!vault.userID.HasValue) { throw new Exception("Vault must have an user id"); } if (isNew && existsVaultWithSameUserAndName(vault.userID.Value, vault.name)) { throw new Exception("There is a vault with the same name"); } // Create or edit Vault model; if (isNew) { model = new Vault(); } else { model = context.Vaults.FirstOrDefault(v => v.vId == vault.id.Value); if (model == null) { throw new Exception($"Vault {vault.id.Value} not found"); } } model.vName = vault.name; model.vUserId = vault.userID.Value; model.vAmount = vault.amount.Value; // Save changes if (isNew) { context.Vaults.Add(model); } else { context.Vaults.Update(model); } context.SaveChanges(); return(convert(model)); }
public IActionResult SaveVault([FromBody] OVault vault) { VaultResponse response = new VaultResponse(); response.status = new Status(); response.status.success = false; try { using (var transaction = context.Database.BeginTransaction()) { VaultManager manager = new VaultManager(context); response.vault = manager.Save(vault); response.status.success = true; transaction.Commit(); } } catch (Exception exception) { response.status.errorMessage = exception.Message; } return(Json(response)); }