public IHttpActionResult CreateAttachment(CreateCourseDiscussionAttachmentModel attachment) { try { using (DbEntities _db = new DbEntities()) { FileDocument _attachment = new FileDocument() { FileName = attachment.FileName, FileSize = attachment.FileSize, FileType = attachment.FileType, FilePath = attachment.FilePath, FileTag = attachment.FileTag, FileNameOnStorage = attachment.FileNameOnStorage, CreatedBy = attachment.CreatedBy, CreatedDate = attachment.CreatedDate, }; _db.FileDocument.Add(_attachment); _db.SaveChanges(); return(Ok(_attachment.Id)); } } catch { return(Ok()); } }
public async Task <ActionResult> _Create(CreateCourseDiscussionModel model) { var nameResponse = await WepApiMethod.SendApiAsync <bool>(HttpVerbs.Get, $"eLearning/CourseDiscussion/IsNameExist?id={null}&name={model.Name}"); if (nameResponse.isSuccess) { TempData["ErrorMessage"] = Language.eLearning.CourseCategory.ValidExistName; return(RedirectToAction("List")); } if (ModelState.IsValid) { DateTime _now = DateTime.Now; model.Post.UserId = model.UserId = CurrentUser.UserId.Value; if (!Directory.Exists(StorageRoot)) { Directory.CreateDirectory(StorageRoot); } int _attachmentId = 0; var r = new List <CreateCourseDiscussionAttachmentModel>(); foreach (string file in Request.Files)// set 1 only { var headers = Request.Headers; if (string.IsNullOrEmpty(headers["X-File-Name"])) { //UploadWholeFile(Request, statuses); for (int i = 0; i < Request.Files.Count; i++) { var filex = Request.Files[i]; if (!String.IsNullOrEmpty(filex.FileName)) { if (filex.ContentLength < RequestLimit) { var fileName = Path.GetFileName(filex.FileName); var fileId = _now.Ticks.ToString() + "_" + fileName; var fullPath = Path.Combine(StorageRoot, fileId); try { System.IO.Directory.CreateDirectory(StorageRoot); filex.SaveAs(fullPath); } catch { TempData["ErrorMessage"] = "Error save attachment"; return(RedirectToAction("List")); } var attachment = new CreateCourseDiscussionAttachmentModel() { Id = 0, FileName = fileName, FileSize = filex.ContentLength, FileType = filex.ContentType, FilePath = @"~\App_Data\DiscussionAttachment\" + fileId, FileTag = model.Name, FileNameOnStorage = fileId, CreatedBy = model.UserId, CreatedDate = _now, }; var attachmentresponse = await WepApiMethod.SendApiAsync <int>(HttpVerbs.Post, $"eLearning/CourseDiscussion/CreateAttachment", attachment); if (attachmentresponse.isSuccess) { _attachmentId = attachmentresponse.Data; model.Post.GotAttachment = true; } } else { TempData["ErrorMessage"] = "Attachment uploaded is too big"; return(RedirectToAction("List")); } } else { model.Post.GotAttachment = false; } } } } if (model.Post.GotAttachment.HasValue) { if (_attachmentId > 0 && model.Post.GotAttachment.Value) { model.AttachmentId = _attachmentId; } } var response = await WepApiMethod.SendApiAsync <string>(HttpVerbs.Post, $"eLearning/CourseDiscussion", model); if (response.isSuccess) { TempData["SuccessMessage"] = "Create New Discussion Completed"; LogActivity(Modules.Learning, "Create Discussion Topic", model); } else { TempData["ErrorMessage"] = "Failed Create New Discussion"; } } return(RedirectToAction("List")); }
public async Task <ActionResult> CreateNewReply(DiscussionView NewPost) { if (NewPost != null) { if (NewPost.NewDiscussionReply != null) { DateTime _now = DateTime.Now; int _attachmentId = 0; //int attachmentId = SaveAttachment(_now, Request.Files).Result; foreach (string file in Request.Files)// set 1 only { for (int i = 0; i < Request.Files.Count; i++) { var filex = Request.Files[i]; if (!String.IsNullOrEmpty(filex.FileName)) { var fileName = Path.GetFileName(filex.FileName); var fileId = _now.Ticks.ToString() + "_" + fileName; var fullPath = Path.Combine(StorageRoot, fileId); try { System.IO.Directory.CreateDirectory(StorageRoot); filex.SaveAs(fullPath); var attachment = new CreateCourseDiscussionAttachmentModel() { Id = 0, FileName = fileName, FileSize = filex.ContentLength, FileType = filex.ContentType, FilePath = @"~\App_Data\DiscussionAttachment\" + fileId, FileTag = "", FileNameOnStorage = fileId, CreatedBy = CurrentUser.UserId.Value, CreatedDate = _now, }; var attachmentresponse = await WepApiMethod.SendApiAsync <int>(HttpVerbs.Post, $"eLearning/CourseDiscussion/CreateAttachment", attachment); if (attachmentresponse.isSuccess) { _attachmentId = attachmentresponse.Data; } } catch { TempData["ErrorMessage"] = "Error save attachment"; //return RedirectToAction("List"); } } } } // add attachment // add reply (attach attachment id if any) DiscussionPost _post = new DiscussionPost(); _post.DiscussionId = NewPost.NewDiscussionReply.DiscussionId; _post.ParentId = null; _post.Topic = ""; _post.Message = NewPost.NewDiscussionReply.Message; _post.IsDeleted = false; _post.UserId = CurrentUser.UserId.Value; _post.CreatedBy = CurrentUser.UserId.Value; _post.CreatedDate = _now; _post.UpdatedBy = CurrentUser.UserId.Value; //db.DiscussionPosts.Add(_post); //db.SaveChanges(); var addNewReply = await WepApiMethod.SendApiAsync <DiscussionPost>(HttpVerbs.Post, $"eLearning/CourseDiscussion/AddDiscussionReply", _post); if (addNewReply.isSuccess) { var addedReply = addNewReply.Data; if (_attachmentId > 0) { var linkAttachment = await WepApiMethod.SendApiAsync <bool>(HttpVerbs.Post, $"eLearning/CourseDiscussion/AddAttachmentToDisccusion?pid={addedReply.Id}&aid={_attachmentId}"); if (linkAttachment.isSuccess) { } else { } //DiscussionAttachment _attachment = new DiscussionAttachment() //{ // AttachmentId = _attachmentId, // PostId = _post.Id, //}; //db.DiscussionAttachment.Add(_attachment); //db.SaveChanges(); } } else { //error } } } return(RedirectToAction("View", new { id = NewPost.NewDiscussionReply.DiscussionId })); }