public ActionResult Create(int?groupId, string threadName, string body, HttpPostedFileBase uploadedFile) { // 基底クラスの共通処理により、Groupプロパティには必ず値が入っている。 var group = this.Group; Debug.Assert(group != null, "this.Group is null"); if (!IsAccessibleProject(group)) { // 参加者がアクセスできない公開範囲である場合はエラー return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (string.IsNullOrEmpty(threadName)) { ModelState.AddModelError("ThreadName", "スレッドタイトルを入力してください。"); } if (string.IsNullOrEmpty(body)) { ModelState.AddModelError("Body", "スレッドの説明を入力してください。"); } GroupThread thread = new GroupThread { ThreadName = threadName }; ViewBag.Body = body; if (!ModelState.IsValid) { ViewBag.GroupName = group.GroupName; ViewBag.GroupId = groupId; return(View(thread)); } var db = this.DbContext; Message message = new Message { Body = body, Sent = this.ExecutionContext.Now, }; AttachFile(message, uploadedFile); message.SentUser = this.CurrentUser; db.Users.Attach(message.SentUser); thread.Message.Add(message); //TODO スレッドのDurationの扱いについて要検討 thread.Duration = new Duration(); thread.Group = db.Groups.Find(groupId.Value); db.Groups.Attach(thread.Group); db.Threads.Add(thread); db.SaveChanges(); return(RedirectToAction("Index", new { groupId = groupId })); }
public ActionResult PostMessage( int?threadId, [Bind(Include = "Subject,Body")] Message message, HttpPostedFileBase uploadedFile) { if (threadId == null || message == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (!ModelState.IsValid) { ViewBag.Message = message; return(View(this.Project)); } GroupThread thread = db.Threads.OfType <GroupThread>() .Include(gt => gt.Group) .FirstOrDefault(gt => gt.ID == threadId); if (thread == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ProjectGroup projectGroup = thread.Group as ProjectGroup; if (projectGroup != null && projectGroup.Accessibility != ProjectGroupAccessibility.Public) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } message.SentUser = db.Users.Find(ctx.CurrentUser.ID); message.Sent = ctx.Now; message.Thread = thread; AttachFile(message, uploadedFile); db.Messages.Add(message); db.Threads.Attach(message.Thread); db.Users.Attach(message.SentUser); db.SaveChanges(); return(RedirectToAction("Details", new { threadId = threadId })); }
internal ActionResult CreateView(GroupThread thread, Message message) { db.Configuration.ProxyCreationEnabled = false; db.Configuration.LazyLoadingEnabled = false; // 現在のプロジェクトに属し、かつ、公開されているグループを取得する。 var groups = db.Groups.OfType <ProjectGroup>().Where(x => x.Project.ID == this.Project.ID && x.Accessibility == ProjectGroupAccessibility.Public).ToList(); var selectList = groups.Select(x => new SelectListItem { Text = x.GroupName, Value = x.ID.ToString() }); ViewBag.GroupId = selectList; ViewBag.Message = message ?? new Message(); ViewBag.GroupThread = thread ?? new Thread(); ViewBag.Project = this.Project; return(View(thread)); }
// GET: /GroupThread/Messages?groupId=1&threadId=2 public ActionResult Messages(int?groupId, int?threadId, bool?fromReply, string subject, string body) { if (groupId == null || threadId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (!IsAccessibleProject(this.Group)) { // 参加者がアクセスできない公開範囲である場合はエラー return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (fromReply == true) { // Replyで入力エラーが発生したときにここに到達する。 if (string.IsNullOrEmpty(subject)) { ModelState.AddModelError("Subject", "メッセージの件名を入力してください。"); } if (string.IsNullOrEmpty(body)) { ModelState.AddModelError("Body", "メッセージの本文を入力してください。"); } } if (threadId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var db = this.DbContext; GroupThread thread = db.Threads .OfType <GroupThread>() .Where(gt => gt.ID == threadId) .Include(gt => gt.Group) .FirstOrDefault(); if (thread == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ViewBag.ThreadName = thread.ThreadName; ViewBag.GroupId = thread.Group.ID; ViewBag.ThreadId = threadId.Value; List <Message> messages = db.Messages .Where(message => message.Thread.ID == threadId.Value) .Include(message => message.SentUser) .OrderBy(message => message.Sent) .ToList(); Dictionary <int, string> userNames = new Dictionary <int, string>(); foreach (Message m in messages) { userNames.Add(m.ID, m.SentUser.DisplayName); } ViewBag.UserNames = userNames; return(View(messages)); }
public ActionResult Create( int?groupId, string threadName, string body, HttpPostedFileBase uploadedFile) { db.Configuration.ProxyCreationEnabled = false; db.Configuration.LazyLoadingEnabled = false; // TODO: トランザクション // 現在のプロジェクトと一緒に、実行ユーザーと所属企業もAttachされる。 db.Projects.Attach(this.Project); if (groupId == null) { ModelState.AddModelError("GroupId", "グループを選択してください。"); } // 正しいグループかどうかチェック var targetGroup = db.Entry(this.Project) .Collection(p => p.ProjectGroups) .Query() .Include(g => g.ParticipantUsers) .Include(g => g.ParticipantUsers.Select(u => u.ParticipantUser)) .FirstOrDefault(g => g.ID == groupId); if (targetGroup == null) { ModelState.AddModelError("GroupId", "選択したグループは存在しません。"); } else { if (targetGroup.Accessibility != ProjectGroupAccessibility.Public) { ModelState.AddModelError("GroupId", "選択したグループは非公開です。"); } if (targetGroup.ParticipantUsers.Count == 0) { ModelState.AddModelError("GroupId", "選択したグループにはメンバーがいません。"); } } if (string.IsNullOrEmpty(threadName)) { ModelState.AddModelError("ThreadName", "スレッド名を入力してください。"); } if (string.IsNullOrEmpty(body)) { ModelState.AddModelError("Body", "本文を入力してください。"); } GroupThread groupThread = new GroupThread { ThreadName = threadName }; Message message = new Message { Body = body }; if (!ModelState.IsValid) { return(CreateView(groupThread, message)); } AttachFile(message, uploadedFile); // Threadの追加 // TODO: Durationの意味。返信可能な期限? db.Configuration.ProxyCreationEnabled = false; db.Configuration.LazyLoadingEnabled = false; groupThread.Group = targetGroup; groupThread.Duration = new Duration(); db.Threads.Add(groupThread); // Messageの追加 message.SentUser = ctx.CurrentUser; message.Sent = ctx.Now; groupThread.Message.Add(message); foreach (var groupuser in targetGroup.ParticipantUsers) { var relationship = new UserMessage { Message = message, User = groupuser.ParticipantUser }; db.UserMessages.Add(relationship); } db.SaveChanges(); return(RedirectToAction("Index")); }