Esempio n. 1
0
        public IActionResult Update(CustomerViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            /**************** Updating Customer **************/

            var single = _context.Customers.Single(x => x.CustomerId == model.CustomerId);

            single.CustomerName       = model.CustomerName;
            single.NIC                = model.NIC;
            single.HomeAddress        = model.HomeAddress;
            single.HomeLandline       = model.HomeLandline;
            single.OfficeAddress      = model.OfficeAddress;
            single.OfficeLandline     = model.OfficeLandline;
            single.Mobile             = model.Mobile;
            single.OpeningBalance     = model.OpeningBalance;
            single.CurrentBalance     = model.CurrentBalance;
            single.LastCollectionDate = model.LastCollectionDate;

            var flag  = _context.SaveChanges();
            var cusId = single.CustomerId;


            /**************** Updating Customer Attachments and Saving **************/
            DeleteFiles(model.CustomerId);
            Miscellaneous.CreateFolder(_hostingEnvironment.WebRootPath + @"\files");
            IList <CustomerAttachmentViewModel> customerAttachmentList = model.CustomerAttachmentList;

            foreach (var item in customerAttachmentList)
            {
                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());
                CustomerAttachment customerAttachment = new CustomerAttachment()
                {
                    AttachmentName = item.AttachmentName,
                    AttachmentPath = randName,
                    CustomerId     = cusId,
                    MimeType       = item.AttachmentFile.Split(",")[0]
                };
                _context.CustomerAttachments.Add(customerAttachment);
                _context.SaveChanges();
            }

            return(Ok(true));
        }
        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));
        }
Esempio n. 3
0
        public IActionResult Create(CustomerViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            /**************** Saving Customer **************/
            Customer customer = _mapper.Map <Customer>(model);

            customer.CurrentStatus = 1;
            customer.CreatedDate   = DateTime.Now;
            _context.Customers.Add(customer);
            var flag       = _context.SaveChanges();
            var customerId = customer.CustomerId;


            if (flag > 0)
            {
                /**************** Creating Customer Attachments and Saving **************/
                Miscellaneous.CreateFolder(_hostingEnvironment.WebRootPath + @"\files");
                IList <CustomerAttachmentViewModel> customerAttachmentList = model.CustomerAttachmentList;
                foreach (var item in customerAttachmentList)
                {
                    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());
                    CustomerAttachment customerAttachment = new CustomerAttachment()
                    {
                        AttachmentName = item.AttachmentName,
                        AttachmentPath = randName,
                        CustomerId     = customerId,
                        MimeType       = item.AttachmentFile.Split(",")[0]
                    };
                    _context.CustomerAttachments.Add(customerAttachment);
                    _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));
        }