Ejemplo n.º 1
0
        public void CreateSize(DTO.Size createdSize)
        {
            CheckHelper.ArgumentNotNull(createdSize, "createdSize");
            CheckHelper.ArgumentWithinCondition(createdSize.IsNew(), "Size is not new.");
            Container.Get <IValidateService>().CheckIsValid(createdSize);
            CheckHelper.ArgumentWithinCondition(!createdSize.SubCategory.IsNew(), "SubCategory of Size is new.");
            CheckHelper.ArgumentWithinCondition(!createdSize.Brand.IsNew(), "Brand of Size is new.");

            CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in.");
            CheckHelper.WithinCondition(SecurityService.IsCurrentUserSeller, "Only seller can create size.");

            var persistentService = Container.Get <IPersistentService>();

            var doesAnotherSizeWithTheSameNameSubCategoryBrandExist =
                persistentService
                .GetEntitySet <DataAccess.Size>()
                .Any(s =>
                     s.Name == createdSize.Name &&
                     s.SubCategoryId == createdSize.SubCategory.Id &&
                     s.BrandId == createdSize.Brand.Id);

            if (doesAnotherSizeWithTheSameNameSubCategoryBrandExist)
            {
                throw new SizeServiceException("Размер с заданным названием уже существует для данной подкатегории и данного бренда.");
            }

            var subCategory = persistentService.GetEntityById <SubCategory>(createdSize.SubCategory.Id);

            CheckHelper.NotNull(subCategory, "SubCategory does not exist.");
            var brand = persistentService.GetEntityById <DataAccess.Brand>(createdSize.Brand.Id);

            CheckHelper.NotNull(brand, "Brand does not exist.");

            var size =
                new DataAccess.Size
            {
                Name          = createdSize.Name,
                SubCategoryId = subCategory.Id,
                SubCategory   = subCategory,
                BrandId       = brand.Id,
                Brand         = brand,
                Active        = createdSize.Active,
                Weight        = createdSize.Weight,
                Height        = createdSize.Height
            };

            size.UpdateTrackFields(Container);
            persistentService.Add(size);

            persistentService.SaveChanges();

            createdSize.Id         = size.Id;
            createdSize.CreateDate = size.CreateDate;
            createdSize.CreateUser = size.CreatedBy.GetFullName();
            createdSize.ChangeDate = size.ChangeDate;
            createdSize.ChangeUser = size.ChangedBy.GetFullName();
        }
Ejemplo n.º 2
0
        public void UpdateSize(DTO.Size updatedSize)
        {
            CheckHelper.ArgumentNotNull(updatedSize, "updatedSize");
            CheckHelper.ArgumentWithinCondition(!updatedSize.IsNew(), "Size is new.");
            Container.Get <IValidateService>().CheckIsValid(updatedSize);
            CheckHelper.ArgumentWithinCondition(!updatedSize.SubCategory.IsNew(), "SubCategory of Size is new.");
            CheckHelper.ArgumentWithinCondition(!updatedSize.Brand.IsNew(), "SubCategory of Brand is new.");

            CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in.");
            CheckHelper.WithinCondition(SecurityService.IsCurrentUserSeller, "Only seller can change size.");

            var persistentService = Container.Get <IPersistentService>();
            var size = persistentService.GetEntityById <DataAccess.Size>(updatedSize.Id);

            CheckHelper.NotNull(size, "Size does not exist.");

            if (size.Name != updatedSize.Name || size.SubCategoryId != updatedSize.SubCategory.Id || size.BrandId != updatedSize.Brand.Id)
            {
                var doesAnotherSizeWithTheSameNameSubCategoryBrandExist =
                    persistentService
                    .GetEntitySet <DataAccess.Size>()
                    .Any(s =>
                         s.Name == updatedSize.Name &&
                         s.SubCategoryId == updatedSize.SubCategory.Id &&
                         s.BrandId == updatedSize.Brand.Id);

                if (doesAnotherSizeWithTheSameNameSubCategoryBrandExist)
                {
                    throw new SizeServiceException("Размер с заданным названием уже существует для данной подкатегории и данного бренда.");
                }
            }

            var subCategory = persistentService.GetEntityById <SubCategory>(updatedSize.SubCategory.Id);

            CheckHelper.NotNull(subCategory, "SubCategory does not exist.");
            var brand = persistentService.GetEntityById <DataAccess.Brand>(updatedSize.Brand.Id);

            CheckHelper.NotNull(brand, "Brand does not exist.");

            size.Name          = updatedSize.Name;
            size.Active        = updatedSize.Active;
            size.SubCategoryId = subCategory.Id;
            size.SubCategory   = subCategory;
            size.BrandId       = brand.Id;
            size.Brand         = brand;
            size.Weight        = updatedSize.Weight;
            size.Height        = updatedSize.Height;
            size.UpdateTrackFields(Container);

            persistentService.SaveChanges();
        }