public ActionResult CreateNewThread(NewThreadModel model) { DatabaseAccess client = DatabaseAccess.Instance; if (model.Topic == null) { ModelState.AddModelError("", "Brak nazwy wątku!"); } if (model.Content == null) { ModelState.AddModelError("", "Treść nie może być pusta"); } if (ModelState.Values.SelectMany(s => s.Errors).Count() == 0) { var registerResult = client.RegisterNewThread(model.UserLogin, model.Topic, model.Content, model.Tags); if (registerResult.Item1) { return(RedirectToAction("ShowThread", new { threadId = registerResult.Item2 })); } else { ModelState.AddModelError("", "Nastąpił błąd, spróbuj ponownie później"); } } return(RedirectToAction("NewThread", model)); }
/// <summary> /// Gets user input into a model for making a new Thread. /// </summary> /// <param name="model">NewThreadModel obj.; what is needed at least to make a new one.</param> /// <param name="user">Current user.</param> /// <returns>Object: a new ThreadEntity based on the NewThreadModel data template.</returns> private ThreadEntity BuildThread(NewThreadModel model, ApplicationUser user) { var forum = _forumEntityService.GetById(model.ForumId); return(new ThreadEntity { Title = model.Title, Content = model.Content, CreatedAt = DateTime.Now, //ModifiedAt = DateTime.Now, User = user, Forum = forum }); }
public IActionResult Create(int id) { var forum = _forumEntityService.GetById(id); var model = new NewThreadModel { ForumId = forum.Id, ForumName = forum.Title, AuthorName = User.Identity.Name // the User obj. from the current http context, // so: whoever is logged in & using the Thread Creation form is the User. }; return(View(model)); }
public async Task <IActionResult> AddThread(NewThreadModel model) { var userId = _userManager.GetUserId(User); //var user = _userManager.FindByIdAsync(userId).Result; var user = await _userManager.FindByIdAsync(userId); var thread = BuildThread(model, user); await _threadEntityService.Create(thread); // Block current thread, wait for task completion - NOTE: using "await" gives error. //_threadEntityService.Create(thread).Wait(); await _appUserService.UpdateRating(userId, typeof(ThreadEntity)); return(RedirectToAction("Index", "Thread", new { id = thread.Id })); }
/// <summary> /// 创建一个主题 /// </summary> /// <param name="entity"></param> /// <returns></returns> public void CreateThread(NewThreadModel model) { //创建主题仓储 //IForumThreadRepository forumThreadRep = FBS.Factory.Factory<IForumThreadRepository>.GetConcrete(); //IAccountRepository accountRep = FBS.Factory.Factory<IAccountRepository>.GetConcrete(); //IForumsRepository forumsRep = FBS.Factory.Factory<IForumsRepository>.GetConcrete(); IRepository <ForumThread> threadRep = Factory.Factory <IRepository <ForumThread> > .GetConcrete <ForumThread>(); IRepository <ThreadRootMessage> msgRep = Factory.Factory <IRepository <ThreadRootMessage> > .GetConcrete <ThreadRootMessage>(); //Account creater = null; ForumThread topic = null; ThreadRootMessage rootMsg = null; //帖子内容对象 MessageVO messageVO = new MessageVO(); messageVO.Body = model.Body; messageVO.Subject = model.Title; try { //creater = accountRep.GetByKey(model.AccountID); rootMsg = new ThreadRootMessage(messageVO, model.AccountID, model.ForumID); topic = new ForumThread(rootMsg, model.ForumID); //topic.RootMessage = rootMsg; threadRep.Add(topic); msgRep.Add(rootMsg); threadRep.PersistAll(); msgRep.PersistAll(); //forumThreadRep.Add(topic); //forumThreadRep.PersistAll(); } catch { throw new AddForumThreadException("添加主题至仓储时异常"); } }
public ActionResult Create([Bind(Include = "Title,Text")] BLL.ViewModels.MessageThread.CreateModel createModel, int?receiverId) { UserInt user = _uow.GetRepository <IUserIntRepository>().GetById(receiverId); if (user == null && User.Identity.GetUserId <int>() != receiverId) { return(HttpNotFound()); } if (ModelState.IsValid) { UserInt sender = UserIntFactory.CreateFromIdentity(_uow, User); NewThreadModel newThreadModel = new NewThreadModel(sender, user); newThreadModel.Prepare(createModel.Title, createModel.Text); int id = _uow.GetRepository <IMessageThreadRepository>().Add(newThreadModel.MessageThread); _uow.Commit(); Message message = newThreadModel.Message; message.MessageThreadId = id; message.AuthorId = newThreadModel.MessageThread.AuthorId; message.Status = MessageStatus.New; int messageId = _uow.GetRepository <IMessageRepository>().Add(message); foreach (var messageReceiver in newThreadModel.MessageReceivers) { messageReceiver.MessageId = messageId; _uow.GetRepository <IMessageReceiverRepository>().Add(messageReceiver); } foreach (var messageThreadReceiver in newThreadModel.MessageThreadReceivers) { messageThreadReceiver.MessageThreadId = id; _uow.GetRepository <IMessageThreadReceiverRepository>().Add(messageThreadReceiver); } _uow.Commit(); return(RedirectToAction("Index")); } return(View(createModel)); }