public ActionResult CreateKnowledgeBaseItem(KnowledgeBase model) { try { SiteUser siteUser = (SiteUser)Session["SiteUser"]; ModelServices modelService = new ModelServices(); List<HttpPostedFileBase> httpPostedFileBases = null; if (model.Files.ToList()[0] != null) { httpPostedFileBases = model.Files.ToList(); var allowedFileSize = ConfigurationManager.AppSettings["FileSize"].ToString(); if (!DocumentManagementService.ValidateAttachmentSize(httpPostedFileBases)) { ModelState.AddModelError("Files", "Attachment Size exceeds allowed limit of " + allowedFileSize + " MB"); } } if (ModelState.IsValid) { var db = new dbTIREntities(); KnowledgeBaseService kbService = new KnowledgeBaseService(siteUser, db); if (!kbService.IsKnowledgeBaseExists(model.Title)) { //TODO: Till nowwe are picking first district Id. Need to refactor code when a user belongs to more than 1 district. model.DistrictId = siteUser.Districts[0].Id; model.RoleId = model.RoleId; model.CreatedDateTime = DateTime.Now; model.CreatedUserId = siteUser.EdsUserId; model.FileDetails = model.Files.ToList()[0] != null ? DocumentManagementService.GetFileDetail(model.Files) : null; kbService.SaveKnowledgeBase(model); return RedirectToAction("Index"); } else { ModelState.AddModelError("Title", "Duplicate Title - please choose a unique title."); } } ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc"); return View(); } catch (Exception ex) { Logging log = new Logging(); log.LogException(ex); return View("GeneralError"); } }
/// <summary> /// SaveKnowledgeBase() method is used to save knowledgebase data /// </summary> /// <param name="KnowledgeBase"></param> public void SaveKnowledgeBase(KnowledgeBase knowledgeBase) { using (var dbContextTransaction = _db.Database.BeginTransaction()) { try { tblKnowledgeBase objKnowledgeBase = null; objKnowledgeBase = new tblKnowledgeBase() { Title = knowledgeBase.Title, Text = knowledgeBase.Text, DistrictId = knowledgeBase.DistrictId, RoleId = knowledgeBase.RoleId, CreateDatetime = knowledgeBase.CreatedDateTime, }; _db.tblKnowledgeBases.Add(objKnowledgeBase); _db.SaveChanges(); tblAttachment objAttachment = null; if (knowledgeBase.FileDetails != null) { foreach (var file in knowledgeBase.FileDetails) { objAttachment = new tblAttachment() { CreatedDate = knowledgeBase.CreatedDateTime, CreatedUserId = knowledgeBase.CreatedUserId, FileName = file.PhysicalFileName, OriginalFileName = file.OriginalFileName, FileExtension = file.FileExtension }; _db.tblAttachments.Add(objAttachment); _db.SaveChanges(); tblKnowledgeBaseAttachment objKnowledgeBaseAttachment = null; objKnowledgeBaseAttachment = new tblKnowledgeBaseAttachment() { AttachmentId = objAttachment.AttachmentId, KnowledgeBaseId = objKnowledgeBase.KnowledgeBaseId, CreatedDate = knowledgeBase.CreatedDateTime, CreatedUserId = knowledgeBase.CreatedUserId }; _db.tblKnowledgeBaseAttachments.Add(objKnowledgeBaseAttachment); _db.SaveChanges(); } } dbContextTransaction.Commit(); } catch (Exception ex) { dbContextTransaction.Rollback(); throw ex; } } }