Esempio n. 1
0
        // [Audit]
        public ActionResult Create(NhaThuocViewModel formCollection)
        {
            try
            {
                UserProfile tkQuanLy = null;
                // check administrator account.
                if (formCollection.Administrator <= 0)
                {
                    ModelState.AddModelError("Administrator", "Chưa chọn tài khoản quản lý");
                }
                else
                {
                    tkQuanLy = unitOfWork.UserProfileRepository.GetById(formCollection.Administrator);
                }
                if (tkQuanLy == null)
                {
                    formCollection.Administrator = 0;
                    ModelState.AddModelError("Administrator", "Tài khoản quản lý không tồn tại");
                }
                if (ModelState.IsValid)
                {
                    var nhaThuoc = formCollection.ToDomainModel();
                    nhaThuoc.HoatDong  = true; // Mặc định là true khi tạo mới
                    nhaThuoc.Created   = DateTime.Now;
                    nhaThuoc.CreatedBy = unitOfWork.UserProfileRepository.GetById(WebSecurity.GetCurrentUserId);

                    unitOfWork.NhaThuocRepository.Insert(nhaThuoc);
                    var nhanVienNhaThuoc = new NhanVienNhaThuoc()
                    {
                        NhaThuoc = nhaThuoc,
                        User     = tkQuanLy,
                        Role     = Constants.Security.Roles.Admin.Value
                    };
                    unitOfWork.NhanVienNhaThuocRespository.Insert(nhanVienNhaThuoc);
                    // default entities
                    taoDuLieuBanDauChoNhaThuoc(nhaThuoc);
                    unitOfWork.Save();
                    // assign Admin role to the user.
                    Roles.Provider.AddUsersToRoles(new[] { tkQuanLy.UserName }, new[] { Constants.Security.Roles.Admin.Value });
                }
                else
                {
                    //var items = new TinhThanhServices().getMany().ToList();
                    //ViewBag.LstTinhThanh = items;
                    return(View(formCollection));
                }


                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMsg = "Có lỗi xảy ra, vui lòng liên hệ quản trị hệ thống!";
                return(RedirectToAction("Index"));
            }
        }
Esempio n. 2
0
        // [Audit]
        public ActionResult EditInfo(NhaThuocViewModel formCollection)
        {
            if (ModelState.IsValid)
            {
                var nhaThuoc = formCollection.ToDomainModel(unitOfWork.NhaThuocRepository.GetById(formCollection.MaNhaThuoc));
                nhaThuoc.Modified   = DateTime.Now;
                nhaThuoc.ModifiedBy = unitOfWork.UserProfileRepository.GetById(WebSecurity.GetCurrentUserId);
                unitOfWork.NhaThuocRepository.Update(nhaThuoc);
                unitOfWork.Save();
                return(View("thongtin", formCollection));
            }

            return(View("Error"));
        }