private void btnLuu_Click(object sender, EventArgs e)
        {
            btnLuu.Text    = "Lưu";
            btnLuu.Enabled = false;
            string[] str = { };

            string rePassword = txt_RePassword.Text.ToString();

            var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

            outPutDirectory = outPutDirectory.Replace(@"\QUANLYLINHKIEN_PTUD\bin\Debug", @"\Dataaccess\Images\StaffAvatar");
            string directoryPath = new Uri(outPutDirectory).LocalPath;

            if (!string.IsNullOrEmpty(openFileName))
            {
                File.Copy(openFileName, Path.Combine(directoryPath, Path.GetFileName(openFileName)), true);
                str = openFileName.Split(new[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
            }
            StaffCreatingDto staff = new StaffCreatingDto()
            {
                Name           = txt_Name.Text.ToString(),
                Email          = txt_Email.Text.ToString(),
                IdentifyNumber = txt_Identify.Text.ToString(),
                NumberPhone    = txt_Identify.Text.ToString(),
                BirthDate      = dtp_BirthDate.Value,
                Role           = Convert.ToInt32(cbx_Role.SelectedIndex),
                Password       = txt_Password.Text.ToString(),
                Avatar         = (openFileName == null)? null : str[str.Length - 1]
            };
            Result result              = null;
            var    taskCreateStaff     = Task.Factory.StartNew(() => result = staffbll.CreateOrUpdateStaff(staff, rePassword));
            var    taskOpenWaitingForm = Task.Factory.StartNew(() => openWaitingForm());

            taskOpenWaitingForm.Wait();
            taskCreateStaff.Wait();
            //bindingSource.Add(staff);

            if (!result.IsSuccess)
            {
                CreateDataGridView();
            }
            //if(result.IsSuccess)
            //    MessageBox.Show(result.ResultMessage, "Thông báo", MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
            //else
            //    MessageBox.Show(result.ResultMessage, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

            MessageBox.Show(result.ResultMessage, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);


            //Phat is working here, don't delete
            //string[] str1 = openFileName.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            //File.Move(openFileName,"Staff." + str1[str1.Length - 1]);
            //MessageBox.Show(openFileName);

            btnLuu.Enabled = true;
        }
示例#2
0
        public Result CreateOrUpdateStaff(StaffCreatingDto entity)
        {
            var entityTemp = db.Staffs.FirstOrDefault(x => x.Email.Equals(entity.Email));

            entity.Password = accountEx.Hash(entity.Password);
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <StaffCreatingDto, Staff>();
            });
            IMapper mapper = config.CreateMapper();

            var staff = mapper.Map <StaffCreatingDto, Staff>(entity);

            staff.IsActived = true;

            if (entityTemp == null)
            {
                db.Staffs.Add(staff);
            }
            else
            {
                entityTemp.Name           = entity.Name;
                entityTemp.NumberPhone    = entity.NumberPhone;
                entityTemp.IdentifyNumber = entity.IdentifyNumber;
                entityTemp.BirthDate      = entity.BirthDate;
                entityTemp.Role           = entity.Role;
                entityTemp.Password       = entity.Password;
                entityTemp.Avatar         = entity.Avatar;
            }

            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                return(new Result
                {
                    ResultMessage = e
                                    .EntityValidationErrors
                                    .LastOrDefault()
                                    .ValidationErrors
                                    .LastOrDefault()
                                    .ErrorMessage,
                    IsSuccess = false
                });
            }
            return(new Result
            {
                ResultMessage = "Tạo nhân viên thành công",
                IsSuccess = true
            });
        }
 public Result CreateOrUpdateStaff
     (StaffCreatingDto staff, string rePassword)
 {
     if (!staff.Password.Equals(rePassword))
     {
         return new Result
                {
                    ResultMessage = "Mật khẩu không trùng",
                    IsSuccess     = false
                }
     }
     ;
     return(dal.CreateOrUpdateStaff(staff));
 }