Example #1
0
 public ActionResult edit(Employees emp, HttpPostedFileBase Picture, HttpPostedFileBase Attachment, string Level)
 {
     if(ModelState.IsValid)
     {
         try
         {
             if (Picture != null)
             {
                 var path = Path.Combine(Server.MapPath("~/Content/Images/Admin"), emp.Email + ".png");
                 Picture.SaveAs(path);
                 emp.Picture = "Y";
             }
             if (Attachment != null)
             {
                 var path = Path.Combine(Server.MapPath("~/Content/Images/Admin"), emp.Email + Path.GetExtension(Attachment.FileName));
                 Picture.SaveAs(path);
                 emp.Attachment = "Y";
             }
             repository.SaveEmployee(emp, Level);
         }
         catch (RuleException ex)
         {
             ex.CopyToModelState(ModelState);
         }
     }
     if(ModelState.IsValid)
     {
         return RedirectToAction("index");
     }
     return View();
 }
Example #2
0
        public void SaveEmployee(Employees emp, string Level)
        {
            #region Vertification
            var errors = new NameValueCollection();

            errors.Add(GetRuleViolations.checkRequire("FullName" , "이름"    , emp.FullName));
            errors.Add(GetRuleViolations.checkBetween("FullName" , "이름"    , emp.FullName, 2, 50));
            errors.Add(GetRuleViolations.checkRequire("CellPhone", "휴대전화", emp.CellPhone));
            errors.Add(GetRuleViolations.checkBetween("CellPhone", "휴대전화", emp.CellPhone, 11, 20));
            //errors.Add(GetRuleViolations.checkPattern("CellPhone", "휴대전화", emp.CellPhone, Patterns.cell));
            errors.Add(GetRuleViolations.checkRequire("Email"    , "이메일"  , emp.Email));
            errors.Add(GetRuleViolations.checkPattern("Email"    , "이메일"  , emp.Email, Patterns.email));

            if (errors.Count > 0) throw new RuleException(errors);
            #endregion

            string procedureName = "UPDATE_EMPLOYEE";
            if (emp.EmployeeID == null)
            {
                procedureName = "ADD_EMPLOYEE";
                emp.EmployeeID = GenerateRandomCode.create_11();
            }
            using(SqlConnection connection = new SqlConnection(NihDbConnectionString.Generate))
            {
                SqlCommand command = new SqlCommand(procedureName, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@EmployeeID"     , SqlDbType.Char    , 11).Value  = emp.EmployeeID;
                command.Parameters.Add("@DepartmentID"   , SqlDbType.Char    , 5).Value   = emp.DepartmentID;
                command.Parameters.Add("@Fullname"       , SqlDbType.NVarChar, 50).Value  = emp.FullName;
                command.Parameters.Add("@JoinDate"       , SqlDbType.DateTime).Value      = emp.JoinDate;
                command.Parameters.Add("@PositionID"     , SqlDbType.Char    , 5).Value   = emp.PositionID;
                command.Parameters.Add("@CellPhone"      , SqlDbType.VarChar , 20).Value  = emp.CellPhone;
                command.Parameters.Add("@Email"          , SqlDbType.VarChar , 200).Value = emp.Email;
                command.Parameters.Add("@PostCode"       , SqlDbType.VarChar , 10).Value  = emp.PostCode;
                command.Parameters.Add("@Address"        , SqlDbType.NVarChar, 300).Value = emp.Address;
                command.Parameters.Add("@Picture"        , SqlDbType.Char    , 1).Value   = emp.Picture;
                command.Parameters.Add("@Attachment"     , SqlDbType.Char    , 1).Value   = emp.Attachment;
                command.Parameters.Add("@Level"          , SqlDbType.Char    , 1).Value   = Level;

                /// 만약 신규 사원등록이면
                /// 로그인 시 필요한 기본 비밀번호인 nih의 암호화코드 추가
                ///
                string passcode = string.Empty;
                if(procedureName == "ADD_EMPLOYEE")
                {
                    passcode = GenerateRandomCode.create_passcode();
                    command.Parameters.Add("@Password", SqlDbType.Char, 56).Value = GetHMAC.Get(passcode);
                }

                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                    if(procedureName == "ADD_EMPLOYEE")
                    {
                        EmailService.SendEmail(SetEmailModel.gmail, SetEmailModel.message(emp.Email, "관리자 페이지 계정입니다.", string.Format("<p><strong>아이디</strong> : {0}<br /><p><strong>비밀번호</strong> : {1}", emp.Email, passcode)));
                    }
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
        }