Ejemplo n.º 1
0
        public ActionResult Create(int?id = null)
        {
            if (id == null)
            {
                return(HttpNotFound());
            }
            var model = new TopicCreateView()
            {
                SectionId = (int)id
            };

            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult Create(TopicCreateView viewmodel)
        {
            if (ModelState.IsValid)
            {
                var forbiddenWords = SharedMethods.CheckForForbiddenWords.CheckForForbiddenWord(viewmodel.TopicName, unitOfWork);
                forbiddenWords.AddRange(SharedMethods.CheckForForbiddenWords.CheckForForbiddenWord(viewmodel.PostContent, unitOfWork));
                if (forbiddenWords.Count > 0)
                {
                    var modelx = new TopicCreateView()
                    {
                        TopicName      = viewmodel.TopicName,
                        PostContent    = viewmodel.PostContent,
                        ForbiddenWords = forbiddenWords,
                    };


                    return(View("Create", modelx));
                }

                Attachment attachmentx    = null;
                byte[]     attachmentData = null;
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase attachment = Request.Files["UserAttachment"];
                    if (attachment.ContentLength > 0)
                    {
                        using (var binary = new BinaryReader(attachment.InputStream))
                        {
                            attachmentData = binary.ReadBytes(attachment.ContentLength);
                        }
                        if (attachmentData.Length <= 3145728 * 5)
                        {
                            attachmentx = new Attachment()
                            {
                                AttachmentData = attachmentData,
                                AttachmentName = Path.GetFileName(Request.Files["UserAttachment"].FileName),
                            };
                        }
                        else
                        {
                            ModelState.AddModelError("AttachmentSizeError", "Attachment size too big");
                        }
                    }
                }

                var topic = new Topic()
                {
                    Name         = viewmodel.TopicName,
                    CreateDate   = DateTime.Now,
                    LastModified = DateTime.Now,
                    AuthorId     = (User as CustomPrincipal).UserId,
                    SectionId    = viewmodel.SectionId,
                    Sticked      = false,
                };


                topic.ViewsCount++;
                Session[topic.TopicId.ToString()] = true;
                unitOfWork.Topics.Add(topic);


                var post = new Post()
                {
                    Content      = viewmodel.PostContent,
                    CreateDate   = DateTime.Now,
                    LastModified = DateTime.Now,
                    AuthorId     = (User as CustomPrincipal).UserId,
                    TopicId      = topic.TopicId,
                    Attachment   = attachmentx
                };
                post.Content = FindEmote.FindEmotes(post.Content, unitOfWork);
                unitOfWork.Repository <Post>().Add(post);
                topic.PostsCount++;
                unitOfWork.Complete();

                return(RedirectToAction("Create", "Post", new { id = topic.TopicId }));
            }
            else
            {
                var model = new TopicCreateView()
                {
                    SectionId = viewmodel.SectionId
                };
                return(View("Create", model));
            }
        }