private ProductType ModelToType(ProductTypeModel model) { return new ProductType { Id = (int)model.TypeId, Name = model.Name }; }
public async Task<ProductTypeModel> LoadProductType(long typeId) { ProductTypeModel type = null; try { using (var db = dbManager.GetConnection()) using (var command = db.Connection.CreateCommand()) { command.CommandText = string.Format("SELECT * FROM PRODUCTTYPE WHERE Id={0}", typeId); var dataReader = await command.ExecuteReaderAsync().ConfigureAwait(false); if (dataReader.Read()) { type = new ProductTypeModel { TypeId = dataReader.GetInt64(0), Name = dataReader.GetString(1) }; } } } catch (Exception ex) { logger.Error("Exception during execution method \"LoadProductType\": {0}", ex.Message); } return type; }
public async Task<bool> DeleteProductType(ProductTypeModel type) { bool result = false; try { using (var db = dbManager.GetConnection()) using (var command = db.Connection.CreateCommand()) { command.CommandText = string.Format("DELETE FROM PRODUCTTYPE WHERE Id = {0}", type.TypeId); await command.ExecuteNonQueryAsync().ConfigureAwait(false); result = true; } } catch (Exception ex) { result = false; logger.Error("Exception during execution method \"DeleteProductType\": {0}", ex.Message); } return result; }
public async Task<long> SaveProductType(ProductTypeModel productType) { long typeId = productType.TypeId; try { using (var db = dbManager.GetConnection()) using (var command = db.Connection.CreateCommand()) { if (typeId == 0) { //is this part really needed? command.CommandText = string.Format("SELECT TypeId FROM PRODUCTTYPE WHERE Name='{0}'", productType.Name); typeId = (long)(await command.ExecuteScalarAsync().ConfigureAwait(false) ?? 0L); } if (typeId == 0) { command.CommandText = string.Format("INSERT INTO PRODUCTTYPE (Name) VALUES ('{0}'); SELECT last_insert_rowid() FROM PRODUCTTYPE", productType.Name); typeId = (long)(await command.ExecuteScalarAsync().ConfigureAwait(false)); logger.Debug("Saving new product type with name={0} and id={1}", productType.Name, typeId); } else { command.CommandText = string.Format("UPDATE PRODUCTTYPE SET Name = '{0}' WHERE TypeId = {1}", productType.Name, productType.TypeId); await command.ExecuteNonQueryAsync().ConfigureAwait(false); logger.Debug("Updating existed product type with name={0} and id={1}", productType.Name, typeId); } } } catch (Exception ex) { typeId = -1; logger.Error("Exception during execution method \"SaveProductType\": {0}", ex.Message); } return typeId; }