public List<Product> getList() { using (NpgsqlConnection connection = getConnection()) { using (NpgsqlCommand cmd = new NpgsqlCommand(SELECT_CMD, connection)) { connection.Open(); List<Product> result = new List<Product>(); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { Product temp = new Product(); temp.id = reader.GetGuid(0); temp.description = reader.GetString(1); temp.productGroupID = reader.GetGuid(2); temp.Unit = Units.FromName(reader.GetString(3)); if (!reader.IsDBNull(4)) { temp.weight = (Decimal)reader.GetFloat(4); } temp.money = reader.GetDecimal(5); temp.quantity = reader.GetInt32(6); result.Add(temp); } } return result; } } }
public bool add(Product product) { using (NpgsqlConnection connection = getConnection()) { connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(INSERT_CMD, connection)) { var p = cmd.Parameters.Add(new NpgsqlParameter(":id", NpgsqlDbType.Uuid)); p.Value = product.id; p = cmd.Parameters.Add(new NpgsqlParameter(":desc", NpgsqlDbType.Text)); p.Value = product.description; p = cmd.Parameters.Add(new NpgsqlParameter(":groupid", NpgsqlDbType.Uuid)); p.Value = product.productGroupID; p = cmd.Parameters.Add(new NpgsqlParameter(":unitid", NpgsqlDbType.Text)); p.Value = product.Unit.getDBName(); p = cmd.Parameters.Add(new NpgsqlParameter(":weight", NpgsqlDbType.Real)); p.Value = product.weight; p = cmd.Parameters.Add(new NpgsqlParameter(":cost", NpgsqlDbType.Money)); p.Value = product.money; p = cmd.Parameters.Add(new NpgsqlParameter(":quantity", NpgsqlDbType.Integer)); p.Value = product.quantity; return cmd.ExecuteNonQuery() == 1; } } }
public bool update(Product product) { using (NpgsqlConnection connection = getConnection()) { connection.Open(); using (NpgsqlTransaction tran = connection.BeginTransaction()) { using (NpgsqlCommand cmd = new NpgsqlCommand(UPDATE_CMD, connection)) { var p = cmd.Parameters.Add(new NpgsqlParameter(":id", NpgsqlDbType.Uuid)); p.Value = product.id; p = cmd.Parameters.Add(new NpgsqlParameter(":desc", NpgsqlDbType.Text)); p.Value = product.description; p = cmd.Parameters.Add(new NpgsqlParameter(":groupid", NpgsqlDbType.Uuid)); p.Value = product.productGroupID; p = cmd.Parameters.Add(new NpgsqlParameter(":unitid", NpgsqlDbType.Text)); p.Value = product.Unit.getDBName(); p = cmd.Parameters.Add(new NpgsqlParameter(":weight", NpgsqlDbType.Real)); p.Value = product.weight; p = cmd.Parameters.Add(new NpgsqlParameter(":cost", NpgsqlDbType.Money)); p.Value = product.money; p = cmd.Parameters.Add(new NpgsqlParameter(":quantity", NpgsqlDbType.Integer)); p.Value = product.quantity; bool result = cmd.ExecuteNonQuery() == 1; tran.Commit(); return result; } } } }
public bool delete(Product product) { using (NpgsqlConnection connection = getConnection()) { connection.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(DELETE_CMD, connection)) { var p = cmd.Parameters.Add(new NpgsqlParameter(":id", NpgsqlDbType.Uuid)); p.Value = product.id; return cmd.ExecuteNonQuery() == 1; } } }
public void save( Product selected, Decimal money, string description, Decimal weight, int quantity, Guid productGroupID, Units Unit ) { if (state == FormState.ADD_NEW) { Product current = new Product(); //current = new Product(); current.id = Guid.NewGuid(); current.money = money; current.description = description; current.weight = weight; current.quantity = quantity; current.productGroupID = productGroupID; current.Unit = Unit; if (current.Unit.Equals(Units.EMPTY)) { view.notify ("Заполните Единицу измерения"); state = FormState.UPDATE_OLD; return; } if (current.money < 0) { view.notify("Цена не может быть меньше 0"); state = FormState.UPDATE_OLD; return; } if (current.weight <= 0) { view.notify("Введите вес больше 0"); state = FormState.UPDATE_OLD; return; } if (productDAO.add(current)) { view.notify("Успешно добавлено"); } state = FormState.UPDATE_OLD; } else { if (selected == null) { view.notify("Выберите товар для изменения"); return; } selected.money = money; selected.description = description; selected.weight = weight; selected.quantity = quantity; selected.productGroupID = productGroupID; selected.Unit = Unit; if (selected.Unit.Equals(Units.EMPTY)) { view.notify("Заполните Единицу измерения"); return; } if (selected.money < 0) { view.notify("Цена не может быть меньше 0"); return; } if (selected.weight <= 0) { view.notify("Введите вес больше 0"); return; } if (productDAO.update(selected)) { view.notify("Успешно изменено"); } } }
public void delete(Product selected) { if (selected == null) { view.notify("Выберите товар для изменения"); return; } if (productDAO.delete(selected)) { view.notify("Успешно изменено"); } }