public async Task <ActionResult> Post([FromBody] Customer_Transaction customer_transaction) { try { // Making sure that the customer being referenced exists await _customerRepository.GetById(customer_transaction.User_ID); await _transactionRepository.GetById(customer_transaction.Transaction_ID); // Inserting record in the Customer_Transaction table await _repository.Insert(customer_transaction); return(Ok("Customer_Transaction Record inserted successfully\n")); } catch (Npgsql.PostgresException ex) { // Postgres threw an exception return(BadRequest(ex.Message.ToString())); } catch { // Unknown error return(BadRequest("Error: Customer_Transaction record was not inserted. Make sure the transactioon and customer exist.\n")); } }
// Function returns the Customer_Transaction with the specified user_id and transaction_id from the database public async Task <Customer_Transaction> GetById(int user_id, int tran_id) { using (NpgsqlConnection sql = new NpgsqlConnection(_connectionString)) // Specifying database context { using (NpgsqlCommand cmd = new NpgsqlCommand("\"spCustomer_Transaction_GetById\"", sql)) // Specifying stored procedure { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new NpgsqlParameter("user_id", NpgsqlDbType.Integer)); cmd.Parameters.Add(new NpgsqlParameter("tran_id", NpgsqlDbType.Integer)); cmd.Parameters[0].Value = user_id; cmd.Parameters[1].Value = tran_id; Customer_Transaction response = null; await sql.OpenAsync(); // Parsing the data retrieved from the database using (var reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { response = MapToValue(reader); } } return(response); } } }
/// <summary> /// Edit rows /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void dataGrid_RowValidatedForCustomerTransactions(object sender, RowValidatedEventArgs args) { CustomerDatabaseEntities context = new CustomerDatabaseEntities(); Customer_Transaction newRecord = args.RowData as Customer_Transaction; if (sender == null) { return; } Customer_Transaction order = context.Customer_Transaction.First(i => i.transactionId == newRecord.transactionId); if (newRecord != null) { order.transactionId = newRecord.transactionId; order.email = newRecord.email; order.eventAddress = newRecord.eventAddress; order.fullname = newRecord.fullname; order.eventName = newRecord.eventName; order.totalPrice = newRecord.totalPrice; order.address = newRecord.address; } context.Entry(order).State = EntityState.Modified; context.SaveChanges(); context.Dispose(); }
// Function inserts a Customer_Transaction record in the database public async Task Insert(Customer_Transaction customer_transaction) { using (NpgsqlConnection sql = new NpgsqlConnection(_connectionString)) // Specifying database context { using (NpgsqlCommand cmd = new NpgsqlCommand("\"spCustomer_Transaction_InsertValue\"", sql)) // Specifying stored procedure { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new NpgsqlParameter("user_id", NpgsqlDbType.Integer)); cmd.Parameters.Add(new NpgsqlParameter("tran_id", NpgsqlDbType.Integer)); cmd.Parameters[0].Value = customer_transaction.User_ID; cmd.Parameters[1].Value = customer_transaction.Transaction_ID; await sql.OpenAsync(); await cmd.ExecuteNonQueryAsync(); return; } } }