// Function returns the Online_Order with the specified order_id from the database public async Task <Online_Order> GetById(int order_id) { using (NpgsqlConnection sql = new NpgsqlConnection(_connectionString)) // Specifying the database context { using (NpgsqlCommand cmd = new NpgsqlCommand("\"spOnline_Order_GetById\"", sql)) // Specifying stored procedure { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new NpgsqlParameter("order_id", NpgsqlDbType.Integer)); cmd.Parameters[0].Value = order_id; Online_Order 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); } } }
// Function modifies an Online_Orders record in the database public async Task ModifyById(Online_Order online_order) { using (NpgsqlConnection sql = new NpgsqlConnection(_connectionString)) // Specifying the database context { using (NpgsqlCommand cmd = new NpgsqlCommand("\"spOnline_Order_ModifyById\"", sql)) // Specifying stored procedure { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new NpgsqlParameter("order_id", NpgsqlDbType.Integer)); cmd.Parameters.Add(new NpgsqlParameter("application", NpgsqlDbType.Varchar)); cmd.Parameters[0].Value = online_order.Order_ID; cmd.Parameters[1].Value = online_order.Application; await sql.OpenAsync(); await cmd.ExecuteNonQueryAsync(); return; } } }
public async Task <ActionResult> Put(int order_id, [FromBody] Online_Order online_order) { if (order_id != online_order.Order_ID) { // If id from body and id from URL don't match return(BadRequest("id in URL has to match the id of the record to be updated\n")); } try { // Searching for reacoord var response = await _repository.GetById(order_id); if (response == null) { // If record does not exist return(NotFound("Online_Order record was not found\n")); } else { // Recornd exists, then modify it await _repository.ModifyById(online_order); string format = "Online_Order record with key={0} was updated succesfully\n"; return(Ok(String.Format(format, order_id))); } } catch (Npgsql.PostgresException ex) { // Postgres threw some exception return(BadRequest(ex.Message.ToString())); } catch { // Unknown error return(BadRequest("Error: Online_Order record could not be updated\n")); } }