Exemple #1
0
        public void RemoveProductFromCategory(Guid categoryId, IList <Guid> productIdsToRemove)
        {
            using (var context = new InstantStoreDataContext())
            {
                if (productIdsToRemove != null && productIdsToRemove.Any())
                {
                    foreach (var productId in productIdsToRemove)
                    {
                        var productMappings = context.ProductToCategories.Where(x => x.CategoryId == categoryId && productId == x.ProductId && x.GroupId == null);
                        context.ProductToCategories.DeleteAllOnSubmit(productMappings);
                    }

                    context.SubmitChanges();

                    CategoryTreeBuilder.RebuidCategoryTreeGroups(context, categoryId);
                }
            }
        }
Exemple #2
0
        public void AssignProductsToCategory(IList <Guid> products, Guid contentPageId)
        {
            using (var context = new InstantStoreDataContext())
            {
                context.ProductToCategories.InsertAllOnSubmit(products.Select(x => new ProductToCategory
                {
                    Id         = Guid.NewGuid(),
                    ProductId  = x,
                    CategoryId = contentPageId,
                    UpdateTime = DateTime.Now
                }));

                // assign to all parents

                context.SubmitChanges();

                CategoryTreeBuilder.RebuidCategoryTreeGroups(context, contentPageId);
            }
        }
        public void DeletePage(Guid id)
        {
            using (var context = new InstantStoreDataContext())
            {
                var page = context.ContentPages.FirstOrDefault(x => x.Id == id);
                if (page != null)
                {
                    var attachmentLinks = context.ContentPageAttachments.Where(x => x.PageId == id);
                    context.ContentPageAttachments.DeleteAllOnSubmit(attachmentLinks);
                    context.Attachments.DeleteAllOnSubmit(attachmentLinks.Select(x => x.Attachment));
                    context.ContentPages.DeleteOnSubmit(page);
                    context.SubmitChanges();

                    if (page.ParentId != null)
                    {
                        CategoryTreeBuilder.RebuidCategoryTreeGroups(context, page.ParentId.Value);
                    }
                }
            }
        }
        public void UpdateContentPage(ContentPage contentPage, IList <Guid> attachmentIds)
        {
            using (var context = new InstantStoreDataContext())
            {
                var contentPageOriginal = context.ContentPages.FirstOrDefault(x => x.Id == contentPage.Id);
                if (contentPageOriginal == null)
                {
                    throw new ModelValidationException("UpdateContentPage.OriginalPageDoesNotExists");
                }

                Guid?oldParentId = contentPageOriginal.ParentId;

                contentPageOriginal.Name       = contentPage.Name;
                contentPageOriginal.Text       = contentPage.Text;
                contentPageOriginal.ParentId   = contentPage.ParentId;
                contentPageOriginal.ShowInMenu = contentPage.ShowInMenu;
                UpdateAttachmentName(contentPageOriginal, attachmentIds, context);

                context.SubmitChanges();

                if (contentPageOriginal.IsCategory())
                {
                    if (oldParentId != contentPageOriginal.ParentId && oldParentId != null)
                    {
                        // Update the old parent category
                        CategoryTreeBuilder.UpdateCategoryGropus(oldParentId.Value, context);

                        // Update both tree nodes & up for old and new parents.
                        CategoryTreeBuilder.RebuidCategoryTreeGroups(context, oldParentId.Value);
                    }

                    if (contentPageOriginal.ParentId != null)
                    {
                        CategoryTreeBuilder.RebuidCategoryTreeGroups(context, contentPageOriginal.Id);
                    }
                }
            }
        }
Exemple #5
0
        public Guid CloneProduct(Guid productId, Guid parentId)
        {
            if (productId == Guid.Empty)
            {
                return(Guid.Empty);
            }

            using (var context = new InstantStoreDataContext())
            {
                var product = context.Products.FirstOrDefault(x => x.VersionId == productId);
                if (product == null)
                {
                    throw new ModelValidationException("Model.Invalid:ProductDoesNotExists");
                }

                var pageCategory = context.ContentPages.FirstOrDefault(x => x.Id == parentId);
                if (pageCategory == null)
                {
                    throw new ModelValidationException("Model.Invalid:ContentPageDoesNotExists");
                }

                if (!pageCategory.IsCategory())
                {
                    throw new ModelValidationException("Model.Invalid:PageIsNotCategory");
                }

                // Build the clone

                var clone = new Product()
                {
                    Id           = Guid.NewGuid(),
                    CashAccepted = product.CashAccepted,
                    Currency     = product.Currency,
                    CustomAttributesTemplateId = product.CustomAttributesTemplateId,
                    Description        = product.Description,
                    Image              = product.Image,
                    IsAvailable        = product.IsAvailable,
                    MainImageId        = product.MainImageId,
                    Name               = product.Name,
                    PriceCurrencyId    = product.PriceCurrencyId,
                    PriceValueCash     = product.PriceValueCash,
                    PriceValueCashless = product.PriceValueCashless,
                    PropertyTemplate   = product.PropertyTemplate,
                    Version            = 1,
                    VersionId          = Guid.NewGuid()
                };

                const string endingStirng     = " - Копия ";
                int          cloneEndingIndex = clone.Name.IndexOf(endingStirng);
                if (cloneEndingIndex > -1)
                {
                    int    indexValue  = 0;
                    string indexString = clone.Name.Substring(cloneEndingIndex + endingStirng.Length);
                    if (!string.IsNullOrEmpty(indexString) && int.TryParse(indexString, out indexValue))
                    {
                        clone.Name = clone.Name.Substring(0, cloneEndingIndex) + endingStirng + indexValue.ToString();
                    }
                    else
                    {
                        clone.Name = clone.Name + endingStirng + "1";
                    }
                }
                else
                {
                    clone.Name = clone.Name + endingStirng + "1";
                }

                // Updating the tables

                context.Products.InsertOnSubmit(clone);

                // Clone images

                var images = context.Images.Where(x => x.ProductId == product.Id);
                if (images.Any())
                {
                    foreach (var image in images)
                    {
                        var clonedImage = new Image {
                            Id               = Guid.NewGuid(),
                            ProductId        = clone.Id,
                            ImageContentType = image.ImageContentType,
                            Image1           = image.Image1
                        };

                        context.Images.InsertOnSubmit(clonedImage);

                        var thumbnail = context.ImageThumbnails.FirstOrDefault(x => x.Id == image.Id);
                        if (thumbnail != null)
                        {
                            var clonedThumbnail = new ImageThumbnail {
                                Id             = clonedImage.Id,
                                Image          = clonedImage,
                                LargeThumbnail = thumbnail.LargeThumbnail,
                                SmallThumbnail = thumbnail.SmallThumbnail
                            };

                            context.ImageThumbnails.InsertOnSubmit(clonedThumbnail);
                        }
                    }
                }

                // Place product in to the same category as original.

                context.ProductToCategories.InsertOnSubmit(new ProductToCategory
                {
                    Id         = Guid.NewGuid(),
                    CategoryId = parentId,
                    ProductId  = clone.VersionId,
                    UpdateTime = DateTime.Now
                });

                CategoryTreeBuilder.RebuidCategoryTreeGroups(context, parentId);
                context.SubmitChanges();

                return(clone.VersionId);
            }
        }
Exemple #6
0
        public void UpdateOrCreateNewProduct(Product productToUpdate, Guid parentId, IList <Guid> images, Guid?prototypeTemplateId, IList <CustomProperty> attributes, int position)
        {
            using (var context = new InstantStoreDataContext())
            {
                var product = productToUpdate.Id != Guid.Empty ? context.Products.FirstOrDefault(x => x.VersionId == productToUpdate.Id) : null;
                if (product == null)
                {
                    productToUpdate.Id          = Guid.NewGuid();
                    productToUpdate.VersionId   = Guid.NewGuid();
                    productToUpdate.MainImageId = images != null && images.Any() ? images.First() : (Guid?)null;

                    context.Products.InsertOnSubmit(productToUpdate);

                    context.SubmitChanges();

                    product = productToUpdate;

                    while (parentId != Guid.Empty)
                    {
                        var parent = context.ContentPages.First(x => x.Id == parentId);
                        if (parent.CategoryId != null)
                        {
                            context.ProductToCategories.InsertOnSubmit(new ProductToCategory
                            {
                                Id         = Guid.NewGuid(),
                                CategoryId = parentId,
                                ProductId  = productToUpdate.VersionId,
                                UpdateTime = DateTime.Now
                            });

                            context.SubmitChanges();

                            CategoryTreeBuilder.RebuidCategoryTreeGroups(context, parentId);
                            break;
                        }

                        parentId = parent.ParentId ?? Guid.Empty;
                    }

                    if (images != null)
                    {
                        foreach (var imageId in images)
                        {
                            var image = context.Images.FirstOrDefault(x => x.Id == imageId);
                            if (image != null)
                            {
                                image.ProductId = productToUpdate.Id;
                            }
                        }
                    }

                    AddAttributes(productToUpdate, prototypeTemplateId, attributes, context);
                }
                else
                {
                    product.Name               = productToUpdate.Name;
                    product.Description        = productToUpdate.Description;
                    product.IsAvailable        = productToUpdate.IsAvailable;
                    product.PriceCurrencyId    = productToUpdate.PriceCurrencyId;
                    product.PriceValueCash     = productToUpdate.PriceValueCash;
                    product.PriceValueCashless = productToUpdate.PriceValueCashless;

                    var productImages     = context.Images.Where(x => x.ProductId == product.Id).ToList();
                    var productImageIds   = productImages.Select(x => x.Id);
                    var productThumbnails = context.ImageThumbnails.Where(x => productImageIds.Contains(x.Id)).ToList();

                    if (images != null)
                    {
                        var imageIdsToDelete   = productImageIds.Except(images);
                        var imagesToDelete     = productImages.Where(x => imageIdsToDelete.Contains(x.Id));
                        var thumbnailsToDelete = productThumbnails.Where(x => imageIdsToDelete.Contains(x.Id));

                        context.Images.DeleteAllOnSubmit(imagesToDelete);
                        context.ImageThumbnails.DeleteAllOnSubmit(thumbnailsToDelete);

                        var imageIdsToInsert = images.Except(productImageIds);
                        var imagesToInsert   = context.Images.Where(x => imageIdsToInsert.Contains(x.Id));
                        foreach (var imageToUpdate in imagesToInsert)
                        {
                            imageToUpdate.ProductId = product.Id;
                        }

                        if (product.MainImageId == null || product.MainImageId == Guid.Empty || imageIdsToDelete.Contains(product.MainImageId.Value))
                        {
                            product.MainImageId = imageIdsToInsert.Any() ? imageIdsToInsert.First() : (Guid?)null;
                        }
                    }
                    else
                    {
                        var imageIdsToDelete   = productImageIds;
                        var imagesToDelete     = productImages.Where(x => imageIdsToDelete.Contains(x.Id));
                        var thumbnailsToDelete = productThumbnails.Where(x => imageIdsToDelete.Contains(x.Id));

                        context.Images.DeleteAllOnSubmit(imagesToDelete);
                        context.ImageThumbnails.DeleteAllOnSubmit(thumbnailsToDelete);

                        product.MainImageId = null;
                    }

                    if (product.CustomAttributesTemplateId != null && (prototypeTemplateId == null || prototypeTemplateId == Guid.Empty))
                    {
                        product.CustomAttributesTemplateId = null;
                    }

                    AddAttributes(product, prototypeTemplateId, attributes, context);
                }

                var productPrimaryCategories = context.ProductToCategories.Where(x => x.ProductId == product.Id);
                foreach (var productPrimaryCategory in productPrimaryCategories)
                {
                    productPrimaryCategory.Index = position;
                }

                context.SubmitChanges();
            }
        }