internal void AttachFile(GroupWork groupwork, HttpPostedFileBase uploadedFile) { if (groupwork == null || uploadedFile == null) { return; } // TODO: 見直し。プロトでは特に検証せずDBに保存する。 var fileName = Path.GetFileName(uploadedFile.FileName); var extension = Path.GetExtension(fileName); var binaryData = GetBytes(uploadedFile.InputStream); if (binaryData.Length > 0) { var document = new Document { ID = Guid.NewGuid(), Uploaded = this.ExecutionContext.Now, User = this.CurrentUser, FileExtension = extension, BinaryData = binaryData, }; this.DbContext.Documents.Add(document); groupwork.AttachedFileName = fileName; groupwork.AttachedDocumentID = document.ID; groupwork.Status = GroupWorkStatus.Submitted; } }
public ActionResult Create(int? themeId, int? projectGroupId, GroupWorkStatus status, string evaluationJSON) { if (!themeId.HasValue) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } if (!projectGroupId.HasValue) { ModelState.AddModelError("ProjectGroupId", "グループを選択してください。"); } if (string.IsNullOrEmpty(evaluationJSON)) { ModelState.AddModelError("DocumentID", "評価を入力してください。"); } var db = this.DbContext; var themeQuery = db.Themes .Where(t => t.ID == themeId.Value && t.Project.ID == this.Project.ID); LogUtility.DebugWriteQuery(themeQuery); var theme = themeQuery.FirstOrDefault(); if (theme == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } GroupWork groupwork = new GroupWork { EvaluationJSON = evaluationJSON, Theme = theme, Status = status }; groupwork.ProjectGroup = db.Groups.OfType<ProjectGroup>().FirstOrDefault(pg => pg.ID == projectGroupId.Value && pg.Accessibility == ProjectGroupAccessibility.Public); if (groupwork.ProjectGroup.Accessibility != ProjectGroupAccessibility.Public) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var groupWorkForCheck = db.GroupWorks.FirstOrDefault(gw => gw.ThemeID == themeId && gw.ProjectGroupID == projectGroupId); if (groupWorkForCheck != null) { ModelState.AddModelError("ProjectGroupId", "選択したグループの評価がすでに存在します。"); } ViewBag.Project = this.Project; ViewBag.ProjectGroups = CreateProjectGroupList(projectGroupId); if (!ModelState.IsValid) { return View(groupwork); } db.Projects.Attach(this.Project); db.GroupWorks.Add(groupwork); db.Themes.Attach(groupwork.Theme); db.Groups.Attach(groupwork.ProjectGroup); db.SaveChanges(); return RedirectToAction("Index", new { selectedThemeId = themeId.Value }); }
public ActionResult GroupWork(int? themeId, int? groupId, bool? submitted, HttpPostedFileBase uploadedFile) { if (themeId == null || groupId == null || submitted == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var theme = GetTheme(themeId.Value); if (theme == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var group = GetGroup(groupId.Value); if (group == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var tuple = GetGroupWorkAndUploadedDate(themeId.Value, groupId.Value); var groupWork = tuple.Item1; // 評価済みの場合は更新不可 if (groupWork != null && groupWork.Status == GroupWorkStatus.Evaluated) { return RedirectToAction("Details", new { themeId = themeId }); } if (groupWork == null) { groupWork = new GroupWork { Theme = theme, ProjectGroup = group, EvaluationJSON = "{}" }; this.DbContext.GroupWorks.Add(groupWork); } AttachFile(groupWork, uploadedFile); groupWork.Status = (submitted == true) ? GroupWorkStatus.Submitted : GroupWorkStatus.Default; this.DbContext.SaveChanges(); return RedirectToAction("Details", new { themeId = themeId }); }