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);
        }
Ejemplo n.º 3
0
 private ProductSubType ModelToSubType(ProductSubTypeModel model)
 {
     return(new ProductSubType {
         Id = (int)model.Id, Name = StringUtilities.EscapeStringForDatabase(model.Name), TypeId = model.TypeId
     });
 }