Exemplo n.º 1
0
        public ActionResult Create(Topic topic, HttpPostedFileBase[] TopicFile, string[] TitleFile, string KeyWords)
        {
            if (ModelState.IsValid)
            {

                topic.ID = Guid.NewGuid();
                topic.DateIn = DateTime.Now;
                var userID = new AccountController().GetonlineUser_ID();
                topic.UserID = userID;
                topic.CountVisit = 0;
                topic.LikeCount = 0;
                topic.UnlikeCount = 0;
                db.Topics.Add(topic);
                var user = db.MembershipOfSites.Where(x => x.ID == userID).FirstOrDefault();
                var grade = db.Assistances.Select(x => x.GradeForTopic).FirstOrDefault();
                user.Grade += grade;
                db.SaveChanges();
                if (!string.IsNullOrEmpty(KeyWords))
                {
                    string[] KeyWordComponnets = KeyWords.Split(',');
                    foreach (var item in KeyWordComponnets)
                    {
                        try
                        {
                            var KeyWordId = int.Parse(item);
                            db.KeywordOfPosts.Add(new KeywordOfPost() { ID = Guid.NewGuid(), keyWordID = KeyWordId, topicID = topic.ID });
                        }
                        catch
                        {
                            KeyWord K = new KeyWord() { title = item };
                            db.KeyWords.Add(K);
                            db.SaveChanges();
                            db.KeywordOfPosts.Add(new KeywordOfPost() { ID = Guid.NewGuid(), keyWordID = K.ID, topicID = topic.ID });
                        }
                    }
                    db.SaveChanges();
                }
                if (TopicFile != null && TopicFile.Length > 0 && TopicFile[0] != null)
                {
                    for (int i = 0; i < TopicFile.Length; i++)
                    {
                        string filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(TopicFile[i].FileName);
                        TopicFile[i].SaveAs(Server.MapPath("~/Files/TopicAttachments/" + filename));
                        db.TopicAttachments.Add(new TopicAttachment() { filename = filename, ID = Guid.NewGuid(), title = TitleFile[i], topicID = topic.ID });
                    }
                    db.SaveChanges();
                }
                new UserLogsController().Create(new Log() { ActivityType = ActivityType.Create, CaseActivity = CaseActivity.Topic, TopicID = topic.ID });
                return RedirectToAction("details", "SubCategory", new { id = topic.SubCategoryID });
            }

            ViewBag.SubCategoryID = new SelectList(db.SubCategories, "ID", "SubCategorySub", topic.SubCategoryID);
            return View(topic);
        }
Exemplo n.º 2
0
        public ActionResult Edit(Topic topic, HttpPostedFileBase[] TopicFile, string[] TitleFile, string KeyWords)
        {
            if (ModelState.IsValid)
            {
                db.Entry(topic).State = EntityState.Modified;
                db.SaveChanges();
                new UserLogsController().Create(new Log() { ActivityType = ActivityType.Edit, CaseActivity = CaseActivity.Topic, TopicID = topic.ID });
                if (!string.IsNullOrEmpty(KeyWords))
                {
                    string[] KeyWordComponnets = KeyWords.Split(',');
                    foreach (var item in KeyWordComponnets)
                    {
                        try
                        {
                            var KeyWordId = int.Parse(item);
                            db.KeywordOfPosts.Add(new KeywordOfPost() { ID = Guid.NewGuid(), keyWordID = KeyWordId, topicID = topic.ID });
                        }
                        catch
                        {
                            KeyWord K = new KeyWord() { title = item };
                            db.KeyWords.Add(K);
                            db.SaveChanges();
                            db.KeywordOfPosts.Add(new KeywordOfPost() { ID = Guid.NewGuid(), keyWordID = K.ID, topicID = topic.ID });
                        }
                    }
                    db.SaveChanges();
                }
                if (TopicFile != null && TopicFile.Length > 0 && TopicFile[0] != null)
                {
                    for (int i = 0; i < TopicFile.Length; i++)
                    {
                        string filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(TopicFile[i].FileName);
                        TopicFile[i].SaveAs(Server.MapPath("~/Files/TopicAttachments/" + filename));
                        db.TopicAttachments.Add(new TopicAttachment() { filename = filename, ID = Guid.NewGuid(), title = TitleFile[i], topicID = topic.ID });
                    }
                    db.SaveChanges();
                }
                if (Roles.IsUserInRole("Admin"))
                {
                    return RedirectToAction("Index", "Topic");
                }
                else
                    return RedirectToAction("UserTopic", "Users", new { SuccessMessage = "ویرایش انجام شد" });
            }

            return View(topic);
        }