Exemplo n.º 1
0
        /// <summary>
        /// Edit user
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            var user = userService.GetById(id);

            if (user == null)
            {
                this.NotifyError("User not found.");
                return(RedirectToAction("List"));
            }

            var roles = db.Roles.Select(x => new ApplicationRoleModel
            {
                Id   = x.Id,
                Name = x.Name
            }).ToList();

            var posts = postService.GetActives(id).Select(x => new PostModel
            {
                Id           = x.Id,
                Title        = x.Title,
                CreateDate   = x.CreateDateUtc,
                Url          = urlService.GetUrl(x.Id, nameof(Post)),
                CommentCount = postService.GetCommentCount(x.Id, null),
                ViewCount    = x.ViewCount,
                IsActive     = x.IsActive,
                UserName     = x.UserName,
                PicturePath  = x.Picture.FilePath
            }).ToList();

            var model = new ApplicationUserModel
            {
                Id                = user.Id,
                UserName          = user.UserName,
                Email             = user.Email,
                EmailConfirmed    = user.EmailConfirmed,
                IsActive          = user.IsActive,
                AvatarId          = user.PictureId,
                Description       = user.Description,
                Location          = user.Location,
                Name              = user.Name,
                Surname           = user.Surname,
                AvatarUrl         = mediaStorageService.GetPictureUrl(user.PictureId),
                Roles             = roles,
                SelectedRoleNames = userService.GetRoles(user.Id).ToArray(),
                IsAdmin           = roles.Any(s => s.Name == SystemRoles.Admin),
                IsOwnAccount      = User.Identity.GetUserId() == user.Id,
                Posts             = posts
            };

            return(View(model));
        }
Exemplo n.º 2
0
        public void Return_User_Correctly_By_UserId()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Get_Users_By_Id").Options;

            using (var ctx = new ApplicationDbContext(options))
            {
                ctx.ApplicationUsers.Add(new ApplicationUser
                {
                    Id       = "111",
                    UserName = "******"
                });

                ctx.ApplicationUsers.Add(new ApplicationUser
                {
                    Id       = "3332",
                    UserName = "******"
                });

                ctx.SaveChanges();
            }


            //Act
            using (var ctx = new ApplicationDbContext(options))
            {
                var appUserService = new ApplicationUserService(ctx);
                var user           = appUserService.GetById("111");

                //Assert
                Assert.AreEqual(user.UserName, "Leonard");
            }
        }
Exemplo n.º 3
0
        public ActionResult Post(int?Id)
        {
            if (!Id.HasValue)
            {
                return(HomePage());
            }

            var post = postService.GetById(Id.Value);

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

            if (!post.IsActive || post.IsDelete)
            {
                return(NotActive());
            }

            string cookieKey = string.Format(CookieConstant.POST_VIEW, post.Id);

            if (!CookieHelper.Exists(cookieKey))
            {
                CookieHelper.Set(cookieKey, WebHelper.IpAddress, 1);
                post.ViewCount += 1;
                postService.Update(post);
            }

            var model = new PostDetailModel
            {
                Id              = post.Id,
                Title           = post.Title,
                Content         = post.Description,
                PicturePath     = post.Picture.FilePath,
                Url             = urlService.GetUrl(post.Id, nameof(Post)),
                User            = post.User,
                CreateDate      = post.CreateDateUtc,
                UpdateDate      = post.UpdateDateUtc,
                ViewCount       = post.ViewCount,
                CategoryName    = post.Category.Name,
                CategoryUrl     = post.Category.Url,
                AllowComment    = post.AllowComment,
                PostFormat      = post.Format,
                PostFormatValue = post.FormatValue,
                ApproveComment  = post.ApproveComment,
                CommentCount    = postService.GetCommentCount(post.Id, true),
                Tags            = tagService.GetTagsByPostId(postId: post.Id).Select(x => new TagModel
                {
                    Name = x.Name
                }).ToList()
            };

            model.User.SocialNetworks = post.User.SocialNetworks;

            if (settingService.GetByName("post.related.view").BoolValue)
            {
                model.RelatedPosts = postService.GetRelatedPosts(post.CategoryId, post.Id).Select(x => new PostModel
                {
                    Id           = x.Id,
                    Title        = x.Title,
                    PicturePath  = mediaStorageService.GetPictureUrl(x.PictureId),
                    CreateDate   = x.CreateDateUtc,
                    CategoryName = categoryService.GetById(x.CategoryId).Name,
                    User         = userService.FindByIdAsync(x.UserId).Result,
                    CommentCount = 0,
                    ViewCount    = x.ViewCount,
                    Url          = urlService.GetUrl(x.Id, nameof(Post))
                }).ToList();
            }

            if (settingService.GetByName("post.comment.enabled").BoolValue)
            {
                model.Comments = postService.GetComments(post.Id, true).Select(x => new CommentListModel
                {
                    Id            = x.Id,
                    ParentId      = x.ParentId,
                    FullName      = x.FullName,
                    Comment       = x.Commentary,
                    UserId        = x.UserId,
                    UserAvatarUrl = userService.GetById(x.UserId).Avatar.FilePath,
                    Approved      = x.Approved,
                    CreateDate    = x.CommentDateUtc.ToRelativeFormat(),
                    ChildComments = postService.GetChildComments(post.Id, x.Id, approved: true).Select(c => new CommentListModel
                    {
                        Id            = c.Id,
                        ParentId      = c.ParentId,
                        FullName      = c.FullName,
                        Comment       = c.Commentary,
                        UserId        = c.UserId,
                        UserAvatarUrl = userService.GetById(x.UserId).Avatar.FilePath,
                        Approved      = c.Approved,
                        CreateDate    = c.CommentDateUtc.ToRelativeFormat(),
                    }).ToList()
                }).ToList();
            }

            return(View(this.GetViewName(post), model));
        }