Ejemplo n.º 1
0
        public virtual ActionResult Detail(string categorySlug, string photoSlug)
        {
            var photo = _context.FindPhotoBySlug(photoSlug);

            if (photo == null || !string.Equals(photo.Category.Slug, categorySlug, StringComparison.CurrentCulture))
            {
                return(HttpNotFound());
            }

            if (!photo.IsApproved && (HttpContext.FindUser().UserId != photo.UserId || HttpContext.FindUser().IsAdmin))
            {
                return(HttpNotFound());
            }

            _context.CreateUserPhotoAction(HttpContext.FindUser(false)?.UserId, photo.PhotoId, UserPhotoActionType.View, false);

            var model = new PhotoDetailViewModel {
                Photo = photo, PhotosCount = _context.FindPhotoCountsByUserId(photo.User.UserId), ViewsCount = _context.FindUserPhotoActionsCountByType(photo.PhotoId, UserPhotoActionType.View), LikesCount = _context.FindUserPhotoActionsCountByType(photo.PhotoId, UserPhotoActionType.Like), CommentsCount = _context.FindCommentsCountByPhotoId(photo.PhotoId), DownloadsCount = _context.FindUserPhotoActionsCountByType(photo.PhotoId, UserPhotoActionType.Download), IsLiked = Request.IsAuthenticated && _context.HasUserPhotoAction(HttpContext.FindUser().UserId, photo.PhotoId, UserPhotoActionType.Like), LikedUsers = _context.FindUserPhotoActionsByType(photo.PhotoId, UserPhotoActionType.Like), Comments = _context.FindCommentsByPhotoId(photo.PhotoId, 1, int.MaxValue)
            };

            return(View(Views.Detail, model));
        }
Ejemplo n.º 2
0
        public virtual ActionResult Delete(int id)
        {
            var category = _context.FindById <CategoryModel>(id);

            if (category == null)
            {
                return(HttpNotFound());
            }

            var photos = _context.FindPhotosByCategorySlug(category.Slug, 1, int.MaxValue);

            foreach (var photo in photos)
            {
                var userPhotoActions = _context.FindUserPhotoActionsByPhotoId(photo.PhotoId);
                var comments         = _context.FindCommentsByPhotoId(photo.PhotoId);

                foreach (var userPhotoAction in userPhotoActions)
                {
                    _context.Delete(userPhotoAction);
                    _context.SaveChanges();
                }

                foreach (var comment in comments)
                {
                    _context.Delete(comment);
                    _context.SaveChanges();
                }

                _context.Delete(photo);
                _context.SaveChanges();
            }

            _context.Delete(category);
            _context.SaveChanges();

            return(RedirectToAction(T4Routes.Category.Manage()));
        }