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