Exemple #1
0
        /// <summary>
        /// Fixes 'MainPictureId' property of a single product entity.
        /// </summary>
        /// <param name="db">Database context.</param>
        /// <param name="product">Product to fix.</param>
        /// <param name="entities">When <c>null</c>, <see cref="Product.ProductPictures"/> gets called.</param>
        /// <returns><c>true</c> when value was fixed.</returns>
        public static bool FixProductMainPictureId(SmartDbContext db, Product product, IEnumerable <ProductMediaFile> entities = null)
        {
            Guard.NotNull(product, nameof(product));

            // INFO: this method must be able to handle pre-save state also.

            entities ??= product.ProductPictures;
            if (entities == null)
            {
                return(false);
            }

            var transientEntities = entities.Where(x => x.Id == 0);

            var sortedEntities = entities
                                 // Remove transient entities.
                                 .Except(transientEntities)
                                 .OrderBy(x => x.DisplayOrder)
                                 .ThenBy(x => x.Id)
                                 .Select(x => db.Entry(x))
                                 .Where(x => x.State != EfState.Deleted && x.State != EfState.Detached)
                                 .Select(x => x.Entity)
                                 // Added/transient entities must be appended.
                                 .Concat(transientEntities.OrderBy(x => x.DisplayOrder));

            var newMainPictureId = sortedEntities.FirstOrDefault()?.MediaFileId;

            if (newMainPictureId != product.MainPictureId)
            {
                product.MainPictureId = newMainPictureId;
                return(true);
            }

            return(false);
        }
Exemple #2
0
        public async Task PrepareProductReviewsModelAsync(ProductReviewsModel model, Product product, int take = int.MaxValue)
        {
            Guard.NotNull(product, nameof(product));
            Guard.NotNull(model, nameof(model));

            model.ProductId     = product.Id;
            model.ProductName   = product.GetLocalized(x => x.Name);
            model.ProductSeName = await product.GetActiveSlugAsync();

            var query = _db.Entry(product).Collection(x => x.ProductReviews).Query()
                        .Where(x => x.IsApproved)
                        .Include(x => x.Customer)
                        .ThenInclude(x => x.CustomerContent)
                        .Include(x => x.Customer)
                        .ThenInclude(x => x.CustomerRoleMappings.Select(c => c.Customer));

            model.TotalReviewsCount = await query.CountAsync();

            var reviews = await query
                          .OrderByDescending(x => x.CreatedOnUtc)
                          .Take(take)
                          .ToListAsync();

            foreach (var review in reviews)
            {
                model.Items.Add(new ProductReviewModel
                {
                    Id                   = review.Id,
                    CustomerId           = review.CustomerId,
                    CustomerName         = review.Customer.FormatUserName(),
                    AllowViewingProfiles = _customerSettings.AllowViewingProfiles && review.Customer != null && !review.Customer.IsGuest(),
                    Title                = review.Title,
                    ReviewText           = review.ReviewText,
                    Rating               = review.Rating,
                    Helpfulness          = new ProductReviewHelpfulnessModel
                    {
                        ProductReviewId = review.Id,
                        HelpfulYesTotal = review.HelpfulYesTotal,
                        HelpfulNoTotal  = review.HelpfulNoTotal,
                    },
                    WrittenOnStr = _dateTimeHelper.ConvertToUserTime(review.CreatedOnUtc, DateTimeKind.Utc).ToString("D"),
                    WrittenOn    = review.CreatedOnUtc
                });
            }

            model.CanCurrentCustomerLeaveReview = _catalogSettings.AllowAnonymousUsersToReviewProduct || !_services.WorkContext.CurrentCustomer.IsGuest();
            model.DisplayCaptcha = _captchaSettings.CanDisplayCaptcha && _captchaSettings.ShowOnProductReviewPage;
        }