public IActionResult Create(StaffViewModel model) { /**************** Saving Staffs **************/ Staff staff = _mapper.Map <Staff>(model); staff.CurrentStatus = 1; _context.Staffs.Add(staff); var flag = _context.SaveChanges(); var staffId = staff.StaffId; if (flag > 0) { /**************** Creating Staff Attachments and Saving **************/ Miscellaneous.CreateFolder(_hostingEnvironment.WebRootPath + @"\files"); IList <StaffAttachmentViewModel> staffAttachmentlList = model.StaffAttachmentList; foreach (var item in staffAttachmentlList) { string randName = DateTime.Now.ToString("MMddyyyyhhmmssfff") + "_" + new Random().Next(0, 10000).ToString("D6") + "_" + item.AttachmentName; string base64 = item.AttachmentFile.Substring(item.AttachmentFile.IndexOf(',') + 1); byte[] data = Convert.FromBase64String(base64); string path = _hostingEnvironment.WebRootPath + @"\files\" + randName; System.IO.File.WriteAllBytes(path, data.ToArray()); StaffAttachment staffAttachment = new StaffAttachment() { AttachmentName = item.AttachmentName, AttachmentPath = randName, StaffId = staffId, MimeType = item.AttachmentFile.Split(",")[0] }; _context.StaffAttachments.Add(staffAttachment); _context.SaveChanges(); } /**************** Saving User **************/ if (model.UserViewModel.CurrentStatus == 1) { User userModel = _mapper.Map <User>(model.UserViewModel); userModel.StaffId = staffId; _context.Users.Add(userModel); _context.SaveChanges(); } return(Ok(true)); } return(Ok(false)); }
public IActionResult Update(StaffViewModel model) { /**************** Updating Staffs **************/ var single = _context.Staffs.Single(x => x.StaffId == model.StaffId); single.StaffName = model.StaffName; single.Nic = model.Nic; single.ContactNo = model.ContactNo; single.Address = model.Address; var flag = _context.SaveChanges(); var staffId = single.StaffId; /**************** Updating Staff Attachments and Saving **************/ DeleteFiles(model.StaffId); Miscellaneous.CreateFolder(_hostingEnvironment.WebRootPath + @"\files"); IList <StaffAttachmentViewModel> staffAttachmentlList = model.StaffAttachmentList; foreach (var item in staffAttachmentlList) { string randName = DateTime.Now.ToString("MMddyyyyhhmmssfff") + "_" + new Random().Next(0, 10000).ToString("D6") + "_" + item.AttachmentName; string base64 = item.AttachmentFile.Split(",")[1]; byte[] data = Convert.FromBase64String(base64); string path = _hostingEnvironment.WebRootPath + @"\files\" + randName; System.IO.File.WriteAllBytes(path, data.ToArray()); StaffAttachment staffAttachment = new StaffAttachment() { AttachmentName = item.AttachmentName, AttachmentPath = randName, StaffId = staffId, MimeType = item.AttachmentFile.Split(",")[0] }; _context.StaffAttachments.Add(staffAttachment); _context.SaveChanges(); } /**************** Updating User **************/ if (model.UserViewModel.CurrentStatus == 1) { if (_context.Users.Count(x => x.UserId == model.UserViewModel.UserId) > 0) { var user = _context.Users.Single(x => x.UserId == model.UserViewModel.UserId); user.UserName = model.UserViewModel.UserName; user.Password = model.UserViewModel.Password; user.CurrentStatus = model.UserViewModel.CurrentStatus; } else { User userModel = _mapper.Map <User>(model.UserViewModel); userModel.StaffId = staffId; _context.Users.Add(userModel); } _context.SaveChanges(); } else { if (_context.Users.Count(x => x.UserId == model.UserViewModel.UserId) > 0) { var user = _context.Users.Single(x => x.UserId == model.UserViewModel.UserId); user.CurrentStatus = 0; _context.SaveChanges(); } } return(Ok(true)); }