public void UpdateCategory(DTO.Category updatedCategory) { CheckHelper.ArgumentNotNull(updatedCategory, "updatedCategory"); CheckHelper.ArgumentWithinCondition(!updatedCategory.IsNew(), "Category is new."); Container.Get <IValidateService>().CheckIsValid(updatedCategory); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition(SecurityService.IsCurrentUserSeller, "Only seller can change category."); var persistentService = Container.Get <IPersistentService>(); var category = persistentService.GetEntityById <DataAccess.Category>(updatedCategory.Id); CheckHelper.NotNull(category, "Category does not exist."); if (category.Name != updatedCategory.Name) { var doesAnotherCategoryWithTheSameNameExist = persistentService .GetEntitySet <DataAccess.Category>() .Any(c => c.Name == updatedCategory.Name); if (doesAnotherCategoryWithTheSameNameExist) { throw new CategoryServiceException("Категория с заданным названием уже существует."); } } category.Name = updatedCategory.Name; category.Active = updatedCategory.Active; category.UpdateTrackFields(Container); persistentService.SaveChanges(); }
public DTO.SubCategory[] GetSubCategories(DTO.Category category, string filter) { CheckHelper.ArgumentNotNull(category, "category"); CheckHelper.ArgumentWithinCondition(!category.IsNew(), "!category.IsNew()"); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition(SecurityService.IsCurrentUserSeller, "Only seller can get all subcategories."); var query = Container .Get <IPersistentService>() .GetEntitySet <SubCategory>() .Where(sc => sc.CategoryId == category.Id) .AsQueryable(); if (!string.IsNullOrWhiteSpace(filter)) { query = query.Where(sc => sc.Name.Contains(filter)); } var dtoService = Container.Get <IDtoService>(); return (query .OrderBy(sc => sc.Id) .ToArray() .Select(sc => dtoService.CreateSubCategory(sc, false)) .ToArray()); }
public void CreateCategory(DTO.Category createdCategory) { CheckHelper.ArgumentNotNull(createdCategory, "createdCategory"); CheckHelper.ArgumentWithinCondition(createdCategory.IsNew(), "Category is not new."); Container.Get <IValidateService>().CheckIsValid(createdCategory); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition(SecurityService.IsCurrentUserSeller, "Only seller can create category."); var persistentService = Container.Get <IPersistentService>(); var doesAnotherCategoryWithTheSameNameExist = persistentService .GetEntitySet <DataAccess.Category>() .Any(c => c.Name == createdCategory.Name); if (doesAnotherCategoryWithTheSameNameExist) { throw new CategoryServiceException("Категория с заданным названием уже существует."); } var category = new DataAccess.Category { Name = createdCategory.Name, Active = createdCategory.Active }; category.UpdateTrackFields(Container); persistentService.Add(category); persistentService.SaveChanges(); createdCategory.Id = category.Id; createdCategory.CreateDate = category.CreateDate; createdCategory.CreateUser = category.CreatedBy.GetFullName(); createdCategory.ChangeDate = category.ChangeDate; createdCategory.ChangeUser = category.ChangedBy.GetFullName(); }