public ServiceResult<Category> AddCategory(string name, int? categoryGroupId = null)
		{
			var result = new ServiceResult<Category>();

			// does Category Group exist?
			if (categoryGroupId.HasValue)
			{
				var categoryGroupResult = GetCategoryGroup(categoryGroupId.Value);
				if (categoryGroupResult.HasErrors)
				{
					result.AddErrors(categoryGroupResult);
					return result;
				}
			}

			// create Category
			var category = new Category()
			{
				Name = name,
				CategoryGroupId = categoryGroupId,
			};

			_db.Insert<Category>(category);

			result.Result = category;
			return result;
		}
Beispiel #2
0
        public ServiceResult<Category> AddCategory(string name, int categoryGroupId)
        {
            var result = new ServiceResult<Category>();

            var existingCategoryGroup = _categoryGroupRepository.GetById(categoryGroupId);
            if (existingCategoryGroup == null)
            {
                result.AddError(ErrorType.NotFound, "Category Group {0} not found", categoryGroupId);
                return result;
            }

            var existingCategory = _categoryRepository.Get(x => x.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase));
            if (existingCategory != null)
            {
                result.Result = existingCategory;
                return result;
            }

            var newCategory = new Category() { Name = name, CategoryGroupId = categoryGroupId };
            _categoryRepository.Add(newCategory);
            _unitOfWork.Commit();

            result.Result = newCategory;
            return result;
        }