public async Task <IActionResult> UpdateRoleMenus(string roleId, string menus) { Guid rId = new Guid(roleId); var rms = _scontext.SecurityRoleMenus.Where(rr => rr.RoleId == rId).ToList(); _scontext.RemoveRange(rms); await _scontext.SaveChangesAsync(); var menuar = menus.Split('|'); foreach (string menuId in menuar) { SecurityRoleMenus r = new SecurityRoleMenus(); r.RoleId = rId; r.MenuId = int.Parse(menuId); r.EditedBy = new Guid(HttpContext.Session.GetString("memberId")); r.EditedDate = DateTime.Now; _scontext.Add(r); } await _scontext.SaveChangesAsync(); return(Json(new { result = "success" })); }
public async Task <IActionResult> CreateRole([Bind("RoleName")] SecurityRoles securityRoles) { if (ModelState.IsValid) { securityRoles.RoleId = Guid.NewGuid(); securityRoles.CreatedBy = new Guid(HttpContext.Session.GetString("memberId")); securityRoles.CreatedDate = DateTime.Now; securityRoles.EditedBy = new Guid(HttpContext.Session.GetString("memberId")); securityRoles.EditedDate = DateTime.Now; securityRoles.x_status = "Y"; securityRoles.RoleName = securityRoles.RoleName.Trim(); _scontext.Add(securityRoles); SecurityRoleMenus rm1 = new SecurityRoleMenus(); rm1.RoleId = securityRoles.RoleId; rm1.MenuId = 951000; rm1.EditedBy = securityRoles.EditedBy; rm1.EditedDate = securityRoles.EditedDate; _scontext.Add(rm1); SecurityRoleMenus rm2 = new SecurityRoleMenus(); rm2.RoleId = securityRoles.RoleId; rm2.MenuId = 951030; rm2.EditedBy = securityRoles.EditedBy; rm2.EditedDate = securityRoles.EditedDate; _scontext.Add(rm2); SecurityRoleMenus rm3 = new SecurityRoleMenus(); rm3.RoleId = securityRoles.RoleId; rm3.MenuId = 951040; rm3.EditedBy = securityRoles.EditedBy; rm3.EditedDate = securityRoles.EditedDate; _scontext.Add(rm3); SecurityRoleMenus rm4 = new SecurityRoleMenus(); rm4.RoleId = securityRoles.RoleId; rm4.MenuId = 951050; rm4.EditedBy = securityRoles.EditedBy; rm4.EditedDate = securityRoles.EditedDate; _scontext.Add(rm4); try { await _scontext.SaveChangesAsync(); } catch (Exception e) { //"Violation of UNIQUE KEY constraint 'UK_SecurityRoles'. Cannot insert duplicate key in object 'dbo.SecurityRoles'. The duplicate key value is (??????).\r\nThe statement has been terminated." if (e.InnerException.Message.Contains("UNIQUE")) { return(Json(new { result = "dup", RoleName = securityRoles.RoleName })); } else { return(Json(new { result = "fail" })); } } return(Json(new { result = "success" })); } return(Json(new { result = "fail" })); }
//public async Task<IActionResult> Create(string birthdate, string cid_card, string email, string fname, string lname, string mobile, string mem_photo, string cid_card_pic) public IActionResult RegisterMember(string birthdate, string cid_card, string email, string fname, string lname, string mobile, string mem_photo, string cid_card_pic) { DateTime bd = Convert.ToDateTime(birthdate); //birthdate = (bd.Year).ToString() + bd.Month.ToString() + bd.Day.ToString(); birthdate = (bd.Year).ToString() + bd.ToString("MMdd"); string password = cid_card.Substring(cid_card.Length - 4); string passwordMD5 = Utils.EncodeMd5(password); try { if ((!string.IsNullOrEmpty(mem_photo)) && (mem_photo.Substring(0, 1) != "M")) { var fileName = mem_photo.Substring(9); var fileExt = Path.GetExtension(fileName); var s = Path.Combine(Path.Combine(_env.WebRootPath, _configuration.GetSection("Paths").GetSection("images_upload").Value), mem_photo); //var d = Path.Combine(Path.Combine(_env.WebRootPath, _configuration.GetSection("Paths").GetSection("images_member").Value), fileName); //System.IO.File.Copy(s, d, true); //System.IO.File.Delete(s); pic_image m = new pic_image(); m.image_code = "M" + DateTime.Now.ToString("yyMMddhhmmssfffffff") + fileExt; m.x_status = "Y"; m.image_name = fileName; string base64String = ""; using (System.Drawing.Image image = System.Drawing.Image.FromFile(s)) { using (MemoryStream mem = new MemoryStream()) { image.Save(mem, image.RawFormat); byte[] imageBytes = mem.ToArray(); base64String = Convert.ToBase64String(imageBytes); } } m.image_file = base64String; m.ref_doc_type = "member"; m.ref_doc_code = cid_card; //member_code; fileName = m.image_code; _context.pic_image.Add(m); _context.SaveChanges(); System.IO.File.Delete(s); //clearImageUpload(); mem_photo = m.image_code; } if ((!string.IsNullOrEmpty(cid_card_pic)) && (cid_card_pic.Substring(0, 1) != "C")) { var fileName = cid_card_pic.Substring(9); var fileExt = Path.GetExtension(fileName); var s = Path.Combine(Path.Combine(_env.WebRootPath, _configuration.GetSection("Paths").GetSection("images_upload").Value), cid_card_pic); //var d = Path.Combine(Path.Combine(_env.WebRootPath, _configuration.GetSection("Paths").GetSection("images_member").Value), fileName); //System.IO.File.Copy(s, d, true); //System.IO.File.Delete(s); pic_image pic_image = new pic_image(); pic_image.image_code = "C" + DateTime.Now.ToString("yyMMddhhmmssfffffff") + fileExt; pic_image.x_status = "Y"; pic_image.image_name = fileName; string base64String = ""; using (System.Drawing.Image image = System.Drawing.Image.FromFile(s)) { using (MemoryStream mem = new MemoryStream()) { image.Save(mem, image.RawFormat); byte[] imageBytes = mem.ToArray(); base64String = Convert.ToBase64String(imageBytes); } } pic_image.image_file = base64String; pic_image.ref_doc_type = "cidcard"; pic_image.ref_doc_code = cid_card; //member_code; fileName = pic_image.image_code; _context.pic_image.Add(pic_image); _context.SaveChanges(); System.IO.File.Delete(s); //clearImageUpload(); cid_card_pic = pic_image.image_code; } _context.Database.ExecuteSqlCommand("INSERT INTO member (member_code,cid_card,birthdate,fname,lname,mobile,email,x_status,mem_username,mem_password,mem_role_id,mem_photo,cid_card_pic) VALUES ('" + cid_card + "','" + cid_card + "','" + birthdate + "',N'" + fname + "',N'" + lname + "','" + mobile + "','" + email + "','Y','" + cid_card + "','" + passwordMD5 + "','17822a90-1029-454a-b4c7-f631c9ca6c7d','" + mem_photo + "','" + cid_card_pic + "')"); var mb = _context.member.SingleOrDefault(mm => mm.member_code == cid_card); SecurityMemberRoles smr = new SecurityMemberRoles(); smr.MemberId = mb.id; smr.CreatedDate = DateTime.Now; smr.CreatedBy = mb.id; smr.EditedDate = DateTime.Now; smr.EditedBy = mb.id; smr.x_status = "Y"; _scontext.Add(smr); _scontext.SaveChanges(); SendEmail(email, cid_card, password); } catch (SqlException ex) { var errno = ex.Number; var msg = ""; if (errno == 2627) //Violation of primary key. Handle Exception { msg = "duplicate"; } return(Json(new { result = "fail", error_code = errno, error_message = msg })); } catch (Exception ex) { var errno = ex.HResult; var msg = ""; if (ex.InnerException.Message.IndexOf("PRIMARY KEY") != -1) { msg = "duplicate"; } return(Json(new { result = "fail", error_code = errno, error_message = msg })); } return(Json(new { result = "success" })); }