public IHttpActionResult Get(int id)
        {
            var media = _mediaService.Get(id);

            if (media == null)
            {
                return(NotFound());
            }

            var entityMedia         = _entityMediaService.FirstOrDefault(x => x.MediaId == id);
            MediaReponseModel model = null;

            //todo: verify permissions to see if media can be viewed by logged in user
            switch (entityMedia.EntityName)
            {
            case "Skill":
                model = media.ToModel <Skill>(entityMedia.EntityId, _mediaService, _mediaSettings, _generalSettings, _userService,
                                              commentService: _commentService,
                                              likeService: _likeService,
                                              withSocialInfo: true,
                                              withNextAndPreviousMedia: true,
                                              avoidMediaTypeForNextAndPreviousMedia: true);
                break;

            case "UserSkill":
                model = media.ToModel <UserSkill>(entityMedia.EntityId, _mediaService, _mediaSettings, _generalSettings, _userService,
                                                  commentService: _commentService,
                                                  likeService: _likeService,
                                                  withSocialInfo: true,
                                                  withNextAndPreviousMedia: true,
                                                  avoidMediaTypeForNextAndPreviousMedia: true);
                break;

            default:
                model = media.ToModel(_mediaService, _mediaSettings, _generalSettings, _userService,
                                      commentService: _commentService,
                                      likeService: _likeService,
                                      withSocialInfo: true,
                                      withNextAndPreviousMedia: true);
                break;
            }


            return(RespondSuccess(new { Media = model }));
        }
        public IHttpActionResult Get(int id)
        {
            var media = _mediaService.GetById(id);

            if (media == null)
            {
                return(NotFound());
            }

            var entityMedia         = _entityMediaService.FirstOrDefault(x => x.MediaId == id);
            MediaReponseModel model = null;

            //todo: verify permissions to see if media can be viewed by logged in user
            switch (entityMedia.EntityName)
            {
            case "Skill":
                model = media.ToModel <Skill>(entityMedia.EntityId, _mediaService, _mediaSettings, _workContext,
                                              _storeContext, _userService,
                                              _customerProfileService, _customerProfileViewService, _pictureService, Url, _webHelper, _friendService,
                                              _commentService, _likeService, true, true, true, true);
                break;

            case "UserSkill":
                model = media.ToModel <UserSkill>(entityMedia.EntityId, _mediaService, _mediaSettings, _workContext,
                                                  _storeContext, _userService,
                                                  _customerProfileService, _customerProfileViewService, _pictureService, Url, _webHelper, _friendService,
                                                  _commentService, _likeService, true, true, true, true);
                break;

            default:
                model = media.ToModel(_mediaService, _mediaSettings, _workContext, _storeContext, _userService,
                                      _customerProfileService, _customerProfileViewService, _customerFollowService, _friendService,
                                      _commentService, _likeService, _pictureService, Url, _webHelper, true, true, true);
                break;
            }

            return(Response(new { Success = true, Media = model }));
        }
Exemple #3
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);

            ;
        }
        public static MediaReponseModel ToModel<T>(this Media media, int entityId,
            IMediaService mediaService,
            MediaSettings mediaSettings = null,
            GeneralSettings generalSettings = null,
            IUserService userService = null,
            IFollowService followService = null,
            IFriendService friendService = null,
            ICommentService commentService = null,
            ILikeService likeService = null,
            bool withUserInfo = true,
            bool withSocialInfo = false,
            bool withNextAndPreviousMedia = false,
            bool avoidMediaTypeForNextAndPreviousMedia = false) where T: BaseEntity
        {
            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, PictureSizeNames.ThumbnailImage) : WebHelper.GetUrlFromPath(media.ThumbnailPath, generalSettings?.ImageServerDomain)
            };
            if (withUserInfo && userService != null)
            {
                var user = userService.Get(media.UserId);
                if (user != null)
                {
                    model.CreatedBy = user.ToModel(mediaService, mediaSettings, followService, friendService);
                    model.DateCreatedLocal = DateTimeHelper.GetDateInUserTimeZone(media.DateCreated, DateTimeKind.Utc, user);
                }
            }
            if (withSocialInfo)
            {
                if (likeService != null)
                {
                    model.TotalLikes = likeService.GetLikeCount<Media>(media.Id);
                    model.LikeStatus =
                        likeService.GetCustomerLike<Media>(ApplicationContext.Current.CurrentUser.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;
            ;
        }