コード例 #1
0
        public ActionResult ViewThread(int id, int?page)
        {
            int postsPerPage;
            var user = db.Users.Find(User.Identity.GetUserId());

            PrivateThreadViewModel viewModel = new PrivateThreadViewModel();

            viewModel.User = db.Users.ToList().Find(x => x.Id == user.Id);

            if (User.Identity.IsAuthenticated)
            {
                postsPerPage = user.PostsPerPage.Quantity;
            }
            else
            {
                postsPerPage = 25;
            }

            viewModel.PostsCount  = db.Posts.ToList().FindAll(x => x.UserID == viewModel.User.Id).Count();
            viewModel.TopicsCount = db.Topics.ToList().FindAll(x => x.UserID == viewModel.User.Id).Count();

            viewModel.Roles = new List <IdentityRole>();

            var tmpMessages = db.PrivateMessages.ToList().FindAll(x => x.PrivateThreadID == id);

            viewModel.PrivateThread      = db.PrivateThreads.ToList().Find(x => x.ID == id);
            viewModel.PrivateThread.Seen = true;
            db.SaveChanges();

            int currPage = page.HasValue ? page.Value : 1;

            viewModel.Messages = tmpMessages.ToPagedList(currPage, postsPerPage);

            foreach (IdentityUserRole role in viewModel.User.Roles)
            {
                viewModel.Roles.Add(db.Roles.ToList().Find(x => x.Id == role.RoleId));
            }

            var lastRole = viewModel.User.Roles.Last();

            viewModel.UserRole = db.Roles.Find(lastRole.RoleId).Name;

            return(View(viewModel));
        }
コード例 #2
0
        public ActionResult CreateReply(PrivateThreadViewModel request, int id)
        {
            var _newMessage = new PrivateMessage();
            var user        = db.Users.Find(User.Identity.GetUserId());
            int postsPerPage;

            _newMessage.Content = Html.EditMarkers(request.Content);

            request = new PrivateThreadViewModel();

            _newMessage.Date   = DateTime.Now;
            _newMessage.Author = db.Users.ToList().Find(x => x.Id == user.Id);

            request.User = _newMessage.Author;

            request.Content = "";

            request.PostsCount  = db.Posts.ToList().FindAll(x => x.UserID == request.User.Id).Count();
            request.TopicsCount = db.Topics.ToList().FindAll(x => x.UserID == request.User.Id).Count();

            request.Roles = new List <IdentityRole>();

            request.PrivateThread      = db.PrivateThreads.ToList().Find(x => x.ID == id);
            request.PrivateThread.Seen = false;

            _newMessage.PrivateThreadID = id;

            if (User.Identity.IsAuthenticated)
            {
                postsPerPage = user.PostsPerPage.Quantity;
            }
            else
            {
                postsPerPage = 25;
            }

            request.Messages = db.PrivateMessages.ToList().FindAll(x => x.PrivateThreadID == id).ToPagedList(1, postsPerPage);
            bool error = false;

            if (Request.Files.Count > 3)
            {
                error         = true;
                ViewBag.Error = Resources.AttachmentCount;
            }
            for (int i = 0; i < Request.Files.Count; i++)
            {
                if (Request.Files[i].ContentLength > 512 * 1024)
                {
                    ViewBag.Error = Resources.AttachmentSize;
                    error         = true;
                    break;
                }
            }
            if (error)
            {
                return(View("ViewThread", request));
            }

            db.PrivateMessages.Add(_newMessage);
            db.SaveChanges();
            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase file = Request.Files[i];

                if (file.FileName == "")
                {
                    continue;
                }

                file.SaveAs(HttpContext.Server.MapPath("~/Content/Attachments/")
                            + file.FileName);
                MessageFile _messageFile = new MessageFile();
                _messageFile.Filename       = "~/Content/Attachments/" + file.FileName;
                _messageFile.PrivateMessage = _newMessage;
                db.MessageFiles.Add(_messageFile);
                db.SaveChanges();
            }

            ModelState.Clear();

            return(RedirectToAction("ViewThread", new { id = id }));
        }