コード例 #1
0
        public static SkillWithUsersModel ToSkillWithUsersModel(this Skill skill, IWorkContext workContext, IStoreContext storeContext, ICustomerService customerService, IUserSkillService userSkillService, IMediaService mediaService,
                                                                MediaSettings mediaSettings, ICustomerFollowService followService, ICustomerLikeService likeService, ICustomerCommentService commentService, ICustomerProfileService customerProfileService, ICustomerProfileViewService customerProfileViewService, IPictureService pictureService, UrlHelper urlHelper, IWebHelper webHelper)
        {
            var currentUser = workContext.CurrentCustomer;
            var model       = new SkillWithUsersModel()
            {
                Skill = skill.ToModel(workContext),
                FeaturedMediaImageUrl = skill.FeaturedImageId > 0 ? mediaService.GetPictureUrl(skill.FeaturedImageId) : ""
            };

            var perPage = 15;
            //by default we'll send data for 15 users. rest can be queried with paginated request
            //todo: make this thing configurable to set number of users to return with this response
            var userSkills = userSkillService.Get(x => x.SkillId == skill.Id).ToList();

            model.UserSkills =
                userSkills.OrderBy(x => x.Id).Take(perPage).Select(
                    x =>
                    x.ToModel(mediaService, mediaSettings, workContext, storeContext, customerService,
                              customerProfileViewService, customerProfileService, pictureService, urlHelper, webHelper, false, true, true, false)).ToList();

            model.CurrentPage   = 1;
            model.UsersPerPage  = perPage;
            model.TotalUsers    = userSkillService.Count(x => x.SkillId == skill.Id);
            model.FollowerCount = followService.GetFollowerCount <Skill>(skill.Id);

            //does this user follow this skill?
            var userFollow = followService.GetCustomerFollow <Skill>(currentUser.Id, skill.Id);

            model.CanFollow     = true;
            model.FollowStatus  = userFollow == null ? 0 : 1;
            model.HasSkill      = userSkills.Any(x => x.UserId == currentUser.Id);
            model.TotalComments = commentService.GetCommentsCount(skill.Id, "skill");
            model.LikeStatus    = likeService.GetCustomerLike <Skill>(currentUser.Id, skill.Id) == null ? 0 : 1;
            model.TotalLikes    = likeService.GetLikeCount <Skill>(skill.Id);
            return(model);
        }
コード例 #2
0
        public static MediaReponseModel ToModel <T>(this Media media, int entityId,
                                                    IMediaService mediaService,
                                                    MediaSettings mediaSettings,
                                                    IWorkContext workContext,
                                                    IStoreContext storeContext,
                                                    ICustomerService userService,
                                                    ICustomerProfileService customerProfileService,
                                                    ICustomerProfileViewService customerProfileViewService,
                                                    IPictureService pictureService,
                                                    UrlHelper urlHelper,
                                                    IWebHelper webHelper,
                                                    IFriendService friendService           = null,
                                                    ICustomerCommentService commentService = null,
                                                    ICustomerLikeService likeService       = null,
                                                    bool withUserInfo             = true,
                                                    bool withSocialInfo           = false,
                                                    bool withNextAndPreviousMedia = false,
                                                    bool avoidMediaTypeForNextAndPreviousMedia = false) where T : BaseEntity
        {
            var storeUrl = webHelper.GetStoreLocation();
            var model    = new MediaReponseModel()
            {
                Id             = media.Id,
                MediaType      = media.MediaType,
                Url            = media.MediaType == MediaType.Image ? mediaService.GetPictureUrl(media) : mediaService.GetVideoUrl(media),
                MimeType       = media.MimeType,
                DateCreatedUtc = media.DateCreated,
                ThumbnailUrl   = media.MediaType == MediaType.Image ? mediaService.GetPictureUrl(media) : media.ThumbnailPath.Replace("~", storeUrl)
            };

            if (withUserInfo && userService != null)
            {
                var user = userService.GetCustomerById(media.UserId);
                if (user != null)
                {
                    var dateTimeHelper = EngineContext.Current.Resolve <IDateTimeHelper>();
                    model.CreatedBy        = user.ToPublicModel(workContext, customerProfileViewService, customerProfileService, pictureService, mediaSettings, urlHelper);
                    model.DateCreatedLocal = dateTimeHelper.ConvertToUserTime(media.DateCreated, DateTimeKind.Utc);
                }
            }
            if (withSocialInfo)
            {
                if (likeService != null)
                {
                    model.TotalLikes = likeService.GetLikeCount <Media>(media.Id);
                    model.LikeStatus =
                        likeService.GetCustomerLike <Media>(workContext.CurrentCustomer.Id, media.Id) != null
                            ? 1
                            : 0;
                }

                if (commentService != null)
                {
                    model.TotalComments = commentService.GetCommentsCount(media.Id, typeof(Media).Name);
                    model.CanComment    = true; //todo: perform check if comments are enabled or user has permission to comment
                }
            }

            if (withNextAndPreviousMedia)
            {
                MediaType?mediaType = null;
                if (!avoidMediaTypeForNextAndPreviousMedia)
                {
                    mediaType = media.MediaType;
                }
                var allMedia   = mediaService.GetEntityMedia <T>(entityId, mediaType, 1, int.MaxValue).ToList();
                var mediaIndex = allMedia.FindIndex(x => x.Id == media.Id);

                model.PreviousMediaId = mediaIndex <= 0 ? 0 : allMedia[mediaIndex - 1].Id;
                model.NextMediaId     = mediaIndex < 0 || mediaIndex == allMedia.Count - 1 ? 0 : allMedia[mediaIndex + 1].Id;
            }

            model.FullyLoaded = withSocialInfo && withNextAndPreviousMedia;
            return(model);

            ;
        }
コード例 #3
0
        private TimelinePostDisplayModel PrepareTimelinePostDisplayModel(TimelinePost post)
        {
            //total likes for this post
            var totalLikes = _customerLikeService.GetLikeCount <TimelinePost>(post.Id);
            //the like status for current customer
            var likeStatus =
                _customerLikeService.GetCustomerLike <TimelinePost>(_workContext.CurrentCustomer.Id, post.Id) == null
                    ? 0
                    : 1;

            var totalComments = _customerCommentService.GetCommentsCount(post.Id, typeof(TimelinePost).Name);
            var postModel     = new TimelinePostDisplayModel()
            {
                Id                       = post.Id,
                DateCreatedUtc           = post.DateCreated,
                DateUpdatedUtc           = post.DateUpdated,
                DateCreated              = _dateTimeHelper.ConvertToUserTime(post.DateCreated, DateTimeKind.Utc),
                DateUpdated              = _dateTimeHelper.ConvertToUserTime(post.DateUpdated, DateTimeKind.Utc),
                OwnerId                  = post.OwnerId,
                OwnerEntityType          = post.OwnerEntityType,
                PostTypeName             = post.PostTypeName,
                IsSponsored              = post.IsSponsored,
                Message                  = post.Message,
                AdditionalAttributeValue = post.AdditionalAttributeValue,
                CanDelete                = post.OwnerId == _workContext.CurrentCustomer.Id || _workContext.CurrentCustomer.IsAdmin(),
                TotalLikes               = totalLikes,
                LikeStatus               = likeStatus,
                TotalComments            = totalComments
            };

            if (post.OwnerEntityType == TimelinePostOwnerTypeNames.Customer)
            {
                //get the customer to retrieve info such a profile image, profile url etc.
                var customer = _customerService.GetCustomerById(post.OwnerId);

                postModel.OwnerName     = customer.GetFullName();
                postModel.OwnerImageUrl =
                    _pictureService.GetPictureUrl(
                        customer.GetAttribute <int>(SystemCustomerAttributeNames.AvatarPictureId),
                        _mediaSettings.AvatarPictureSize, true);

                postModel.OwnerProfileUrl = Url.RouteUrl("CustomerProfileUrl", new RouteValueDictionary()
                {
                    { "SeName", customer.GetSeName(_workContext.WorkingLanguage.Id, true, false) }
                });
            }
            //depending on the posttype, we may need to extract additional data e.g. in case of autopublished posts, we may need to query the linked entity
            switch (post.PostTypeName)
            {
            case TimelineAutoPostTypeNames.VideoBattle.Publish:
            case TimelineAutoPostTypeNames.VideoBattle.BattleStart:
            case TimelineAutoPostTypeNames.VideoBattle.BattleComplete:
                //we need to query the video battle
                if (post.LinkedToEntityId != 0)
                {
                    var battle = _videoBattleService.GetById(post.LinkedToEntityId);
                    if (battle == null)
                    {
                        break;
                    }
                    var battleUrl = Url.RouteUrl("VideoBattlePage",
                                                 new RouteValueDictionary()
                    {
                        { "SeName", battle.GetSeName(_workContext.WorkingLanguage.Id, true, false) }
                    });

                    //create a dynamic object for battle, we'll serialize this object to json and store as additional attribute value
                    //todo: to see if we have some better way of doing this
                    var coverImageUrl = "";
                    if (battle.CoverImageId.HasValue)
                    {
                        coverImageUrl = _pictureService.GetPictureUrl(battle.CoverImageId.Value);
                    }
                    var obj = new {
                        Name             = battle.Name,
                        Url              = battleUrl,
                        Description      = battle.Description,
                        VotingStartDate  = battle.VotingStartDate,
                        VotingEndDate    = battle.VotingEndDate,
                        CoverImageUrl    = coverImageUrl,
                        RemainingSeconds = battle.GetRemainingSeconds(),
                        Status           = battle.VideoBattleStatus.ToString()
                    };

                    postModel.AdditionalAttributeValue = JsonConvert.SerializeObject(obj);
                }
                break;
            }

            return(postModel);
        }