public bool Save(Order order, IConfigurationsHandler configurations) { SqlConnection connection = new SqlConnection(); try { connection.ConnectionString = configurations.GetConnectionString(); connection.Open(); SqlCommand command = new SqlCommand("INSERT INTO Orders VALUES (@CustomerId, @OrderId, @Amount, @VAT)", connection); command.Parameters.AddWithValue("@CustomerId", order.CustomerId); command.Parameters.AddWithValue("@OrderId", order.OrderId); command.Parameters.AddWithValue("@Amount", order.Amount); command.Parameters.AddWithValue("@VAT", order.VAT); command.ExecuteNonQuery(); return(true); } catch (Exception ex) { //log error here return(false); } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } }
public OrderService(IOrderAmountFactory orderAmountFactory, IOrderAmountValidator orderAmountValidator, IVatService vatService, IConfigurationsHandler configurations, IOrderRepository orderRepository) { this.orderAmountFactory = orderAmountFactory; this.orderAmountValidator = orderAmountValidator; this.vatService = vatService; this.configurations = configurations; this.orderRepository = orderRepository; }
public IEnumerable <Order> GetOrders(int customerId, IConfigurationsHandler configurations) { SqlConnection connection = new SqlConnection(); try { connection.ConnectionString = configurations.GetConnectionString(); connection.Open(); SqlCommand command = new SqlCommand("SELECT * FROM Orders WHERE CustomerId = " + customerId, connection); var reader = command.ExecuteReader(); var orders = new List <Order>(); while (reader.Read()) { orders.Add( new Order { OrderId = Convert.ToInt32(reader["OrderId"]), CustomerId = Convert.ToInt32(reader["CustomerId"]), Amount = Convert.ToDouble(reader["Amount"]), VAT = Convert.ToInt32(reader["VAT"]), }); } return(orders); } catch (Exception ex) { //log error here return(null); } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } }
public static Customer Load(int customerId, IConfigurationsHandler configurations) { SqlConnection connection = new SqlConnection(); try { connection.ConnectionString = configurations.GetConnectionString(); connection.Open(); SqlCommand command = new SqlCommand("SELECT * FROM Customer WHERE CustomerId = " + customerId, connection); var reader = command.ExecuteReader(); var customer = new Customer(); while (reader.Read()) { customer.Name = reader["Name"].ToString(); customer.DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString()); customer.Country = reader["Country"].ToString(); } return(customer); } catch (Exception ex) { // log error here return(null); } finally { if (connection.State == System.Data.ConnectionState.Open) { connection.Close(); } } }