public void UpdateBrand(DTO.Brand updatedBrand) { CheckHelper.ArgumentNotNull(updatedBrand, "updatedBrand"); CheckHelper.ArgumentWithinCondition(!updatedBrand.IsNew(), "Brand is new."); Container.Get <IValidateService>().CheckIsValid(updatedBrand); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition(SecurityService.IsCurrentUserSeller, "Only seller can change brand."); var persistentService = Container.Get <IPersistentService>(); var brand = persistentService.GetEntityById <DataAccess.Brand>(updatedBrand.Id); CheckHelper.NotNull(brand, "Brand does not exist."); if (brand.Name != updatedBrand.Name) { var doesAnotherBrandWithTheSameNameExist = persistentService .GetEntitySet <DataAccess.Brand>() .Any(b => b.Name == updatedBrand.Name); if (doesAnotherBrandWithTheSameNameExist) { throw new BrandServiceException("Бренд с заданным названием уже существует."); } } brand.Name = updatedBrand.Name; brand.Active = updatedBrand.Active; brand.UpdateTrackFields(Container); persistentService.SaveChanges(); }
public DTO.Product[] GetActiveProductsByBrand(DTO.Brand brand) { CheckHelper.ArgumentNotNull(brand, "brand"); CheckHelper.ArgumentWithinCondition(!brand.IsNew(), "!brand.IsNew()"); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); var persistentService = Container.Get<IPersistentService>(); var dtoService = Container.Get<IDtoService>(); return persistentService .GetActiveEntities<DataAccess.Product>() .Where(p => p.BrandId == brand.Id) .OrderByDescending(p => p.ChangeDate) .AsEnumerable() .Select(p => dtoService.CreateProduct(p)) .ToArray(); }
public void CreateBrand_Should_Create_Brand() { // Arrange var container = ContainerMockFactory.Create(); var securityService = container.Get <ISecurityService>(); securityService.LogIn(UserMockFactory.Diana.Login, EncryptServiceMockFactory.DianaPasswordData.Password); const string NEW_BRAND_NAME = "New Brand"; var createdBrand = new DTO.Brand { Name = NEW_BRAND_NAME, Active = true }; var brandService = container.Get <IBrandService>(); var persistentService = container.Get <IPersistentService>(); var timeService = container.Get <ITimeService>(); // Act brandService.CreateBrand(createdBrand); // Assert var actualBrand = persistentService .GetEntitySet <DataAccess.Brand>() .Single(b => b.Name == NEW_BRAND_NAME); Assert.IsTrue(actualBrand.Id > 0); Assert.AreEqual(NEW_BRAND_NAME, actualBrand.Name); Assert.IsTrue(actualBrand.Active); Assert.AreEqual(timeService.UtcNow, actualBrand.CreateDate); Assert.AreEqual(timeService.UtcNow, actualBrand.ChangeDate); Assert.AreEqual(UserMockFactory.Diana, actualBrand.CreatedBy); Assert.AreEqual(UserMockFactory.Diana, actualBrand.ChangedBy); Assert.AreEqual(createdBrand, container.Get <IDtoService>().CreateBrand(actualBrand)); }
public void CreateBrand(DTO.Brand createdBrand) { CheckHelper.ArgumentNotNull(createdBrand, "createdBrand"); CheckHelper.ArgumentWithinCondition(createdBrand.IsNew(), "Brand is not new."); Container.Get <IValidateService>().CheckIsValid(createdBrand); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition(SecurityService.IsCurrentUserSeller, "Only seller can create brand."); var persistentService = Container.Get <IPersistentService>(); var doesAnotherBrandWithTheSameNameExist = persistentService .GetEntitySet <DataAccess.Brand>() .Any(b => b.Name == createdBrand.Name); if (doesAnotherBrandWithTheSameNameExist) { throw new BrandServiceException("Бренд с заданным названием уже существует."); } var brand = new DataAccess.Brand { Name = createdBrand.Name, Active = createdBrand.Active }; brand.UpdateTrackFields(Container); persistentService.Add(brand); persistentService.SaveChanges(); createdBrand.Id = brand.Id; createdBrand.CreateDate = brand.CreateDate; createdBrand.CreateUser = brand.CreatedBy.GetFullName(); createdBrand.ChangeDate = brand.ChangeDate; createdBrand.ChangeUser = brand.ChangedBy.GetFullName(); }
public void UpdateBrand_Should_Update_Brand_When_Name_Is_Not_Changed() { // Arrange var container = ContainerMockFactory.Create(); var securityService = container.Get <ISecurityService>(); securityService.LogIn(UserMockFactory.Diana.Login, EncryptServiceMockFactory.DianaPasswordData.Password); var createDate = BrandMockFactory.Crazy8.CreateDate; var createdBy = BrandMockFactory.Crazy8.CreatedBy; var name = BrandMockFactory.Crazy8.Name; var updatedBrand = new DTO.Brand { Id = BrandMockFactory.Crazy8.Id, Name = BrandMockFactory.Crazy8.Name, Active = true }; var brandService = container.Get <IBrandService>(); var persistentService = container.Get <IPersistentService>(); var timeService = container.Get <ITimeService>(); // Act brandService.UpdateBrand(updatedBrand); // Assert var actualBrand = persistentService.GetEntityById <DataAccess.Brand>(BrandMockFactory.Crazy8.Id); Assert.AreEqual(name, actualBrand.Name); Assert.IsTrue(actualBrand.Active); Assert.AreEqual(createDate, actualBrand.CreateDate); Assert.AreEqual(timeService.UtcNow, actualBrand.ChangeDate); Assert.AreEqual(createdBy, actualBrand.CreatedBy); Assert.AreEqual(UserMockFactory.Diana, actualBrand.ChangedBy); Assert.AreEqual(updatedBrand, container.Get <IDtoService>().CreateBrand(actualBrand)); }
public DTO.Size[] GetActiveSizes(DTO.SubCategory subCategory, DTO.Brand brand) { CheckHelper.ArgumentNotNull(subCategory, "subCategory"); CheckHelper.ArgumentWithinCondition(!subCategory.IsNew(), "!subCategory.IsNew()"); CheckHelper.ArgumentNotNull(brand, "brand"); CheckHelper.ArgumentWithinCondition(!brand.IsNew(), "!brand.IsNew()"); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); var persistentService = Container.Get <IPersistentService>(); var dtoService = Container.Get <IDtoService>(); return (persistentService .GetActiveEntities <DataAccess.Size>() .Where(s => s.SubCategoryId == subCategory.Id && s.BrandId == brand.Id) .OrderBy(s => s.Name) .AsEnumerable() .Select(s => dtoService.CreateSize(s)) .ToArray()); }
public DTO.Brand CreateBrand(DataAccess.Brand brand, bool includeOnlyActive = true) { CheckHelper.ArgumentNotNull(brand, "brand"); CheckHelper.ArgumentWithinCondition(!brand.IsNew(), "!brand.IsNew()"); return (_dtoCache.Get( brand, b => { var result = new DTO.Brand { Id = b.Id, Name = b.Name, Active = b.Active }; CopyTrackableFields(result, b); return result; })); }
public void UpdateBrand_Should_Throw_Exception_When_Brand_With_The_Same_Name_Already_Exists() { var container = ContainerMockFactory.Create(); var securityService = container.Get <ISecurityService>(); securityService.LogIn(UserMockFactory.Diana.Login, EncryptServiceMockFactory.DianaPasswordData.Password); var updatedBrand = new DTO.Brand { Id = BrandMockFactory.Crazy8.Id, Name = BrandMockFactory.Carters.Name, Active = true }; var brandService = container.Get <IBrandService>(); // Act // Assert ExceptionAssert.Throw <BrandServiceException>( () => brandService.UpdateBrand(updatedBrand), "Бренд с заданным названием уже существует."); }
public void CreateBrand_Should_Throw_Exception_When_Current_User_Is_Not_Seller() { // Arrange var container = ContainerMockFactory.Create(); var securityService = container.Get <ISecurityService>(); securityService.LogIn(UserMockFactory.Olesya.Login, EncryptServiceMockFactory.OlesyaPasswordData.Password); var createdBrand = new DTO.Brand { Name = "New Brand", Active = true }; var brandService = container.Get <IBrandService>(); // Act // Assert ExceptionAssert.Throw <InvalidOperationException>( () => brandService.CreateBrand(createdBrand), "Only seller can create brand."); }
public DTO.Size[] GetSizes(DTO.SubCategory subCategory, DTO.Brand brand, string filter) { CheckHelper.ArgumentNotNull(subCategory, "subCategory"); CheckHelper.ArgumentWithinCondition(!subCategory.IsNew(), "!subCategory.IsNew()"); CheckHelper.ArgumentNotNull(brand, "brand"); CheckHelper.ArgumentWithinCondition(!brand.IsNew(), "!brand.IsNew()"); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition(SecurityService.IsCurrentUserSeller, "Only seller can get all sizes."); var query = Container .Get <IPersistentService>() .GetEntitySet <DataAccess.Size>() .Where(s => s.SubCategoryId == subCategory.Id && s.BrandId == brand.Id) .AsQueryable(); if (!string.IsNullOrWhiteSpace(filter)) { query = query .Where(s => s.Name.Contains(filter) || s.Height.Contains(filter) || s.Weight.Contains(filter)); } var dtoService = Container.Get <IDtoService>(); return (query .OrderBy(s => s.Id) .ToArray() .Select(s => dtoService.CreateSize(s, false)) .ToArray()); }
/// <summary> /// 编辑一个品牌 /// </summary> /// <param name="model"></param> public static void UpdateBrand(DTO.Brand model) { _brandService.UpdateBrand(model.Map <Model.BrandInfo>()); }
/// <summary> /// 添加一个品牌 /// </summary> /// <param name="model"></param> public static void AddBrand(DTO.Brand model) { _brandService.AddBrand(model.Map <Model.BrandInfo>()); }
/// <summary> /// 编辑一个品牌 /// </summary> /// <param name="model"></param> public static void UpdateBrand(DTO.Brand model) { Service.UpdateBrand(model.Map <Entities.BrandInfo>()); }
/// <summary> /// 添加一个品牌 /// </summary> /// <param name="model"></param> public static void AddBrand(DTO.Brand model) { Service.AddBrand(model.Map <Entities.BrandInfo>()); }