public JsonResult Create(EmployeeDetailView vm) { CreateEmployeeRequest request = new CreateEmployeeRequest(); request.LastName = vm.LastName; request.FirstName = vm.FirstName; request.Title = vm.Title; request.TitleOfCourtesy = vm.TitleOfCourtesy; request.BirthDate = vm.BirthDate; request.HireDate = vm.HireDate; request.Address = vm.Address; request.City = vm.City; request.Region = vm.Region; request.PostalCode = vm.PostalCode; request.Country = vm.Country; request.HomePhone = vm.HomePhone; request.Extension = vm.Extension; request.Photo = vm.Photo; request.Notes = vm.Notes; GetEmployeeRequest employeeRequest = new GetEmployeeRequest(); employeeRequest.EmployeeID = vm.EmployeeReferenceEmployeeID; request.EmployeeReference = _employeeService.GetEmployee(employeeRequest).Employee; request.PhotoPath = vm.PhotoPath; CreateEmployeeResponse response = _employeeService.CreateEmployee(request); return(Json(response)); }
void SendResultInfoAsJson(CreateEmployeeResponse res) { string strJson = JsonConvert.SerializeObject(res); Response.ContentType = "application/json; charset=utf-8"; Response.Write(strJson); Response.End(); }
void SendResultInfoAsJson(CreateEmployeeResponse res) { string strJson = JsonConvert.SerializeObject(res); Response.ContentType = "application/json; charset=utf-8"; Response.AppendHeader("Access-Control-Allow-Origin", "*"); Response.Write(strJson); Response.End(); }
public CreateEmployeeResponse CreateEmployee(CreateEmployeeRequest request) { CreateEmployeeResponse response = new CreateEmployeeResponse(); Employee employee = new Employee(); employee.LastName = request.LastName; employee.FirstName = request.FirstName; employee.Title = request.Title; employee.TitleOfCourtesy = request.TitleOfCourtesy; employee.BirthDate = request.BirthDate; employee.HireDate = request.HireDate; employee.Address = request.Address; employee.City = request.City; employee.Region = request.Region; employee.PostalCode = request.PostalCode; employee.Country = request.Country; employee.HomePhone = request.HomePhone; employee.Extension = request.Extension; employee.Photo = request.Photo; employee.Notes = request.Notes; employee.PhotoPath = request.PhotoPath; employee.Territories = request.Territories.ConvertToTerritories(); employee.Orders = request.Orders.ConvertToOrders(); employee.EmployeeReference = request.EmployeeReference.ConvertToEmployee(); if (employee.GetBrokenRules().Count() > 0) { response.Errors = employee.GetBrokenRules().ToList(); } else { try { _employeeRepository.Add(employee); _uow.Commit(); response.Errors = new List <BusinessRule>(); } catch (Exception ex) { List <BusinessRule> errors = new List <BusinessRule>(); do { errors.Add(new BusinessRule("DAL", "DAL_ERROR: " + ex.Message)); ex = ex.InnerException; } while (ex != null); response.Errors = errors; } } return(response); }
protected void Page_Load(object sender, EventArgs e) { CreateEmployeeRequest req; CreateEmployeeResponse res = new CreateEmployeeResponse(); res.error = String.Empty; // 1. Deserialize the incoming Json. try { req = GetRequestInfo(); } catch (Exception ex) { res.error = ex.Message.ToString(); // Return the results as Json. SendResultInfoAsJson(res); return; } SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); try { connection.Open(); string getUserInfo = "SELECT EmployeeID,UserName,Password,FirstName,LastName,Email,Phone,StoreID,JobType,Status FROM Employee WHERE UserName = @UserName AND (Password = @Password OR 1 = @Flag)"; SqlCommand getUserInfoCommand = new SqlCommand(getUserInfo, connection); getUserInfoCommand.Parameters.Add("@UserName", SqlDbType.NVarChar); getUserInfoCommand.Parameters.Add("@Password", SqlDbType.NVarChar); getUserInfoCommand.Parameters.Add("@Flag", SqlDbType.NVarChar); getUserInfoCommand.Parameters["@UserName"].Value = req.UserName; getUserInfoCommand.Parameters["@Password"].Value = req.Password; getUserInfoCommand.Parameters["@Flag"].Value = 1; SqlDataReader reader = getUserInfoCommand.ExecuteReader(); if (reader.HasRows) { res.error = "Username already in use"; SendResultInfoAsJson(res); return; } reader.Close(); string sql = "INSERT INTO Employee(UserName,Password,FirstName,LastName,Email,Phone,StoreID,JobType,Status) Values(@UserName,@Password,@FirstName,@LastName,@Email,@Phone,@StoreID,@JobType,0)"; SqlCommand createEmp = new SqlCommand(sql, connection); createEmp.Parameters.Add("@UserName", SqlDbType.NVarChar); createEmp.Parameters.Add("@Password", SqlDbType.NVarChar); createEmp.Parameters.Add("@FirstName", SqlDbType.NVarChar); createEmp.Parameters.Add("@LastName", SqlDbType.NVarChar); createEmp.Parameters.Add("@Email", SqlDbType.NVarChar); createEmp.Parameters.Add("@Phone", SqlDbType.NVarChar); createEmp.Parameters.Add("@StoreID", SqlDbType.Int); createEmp.Parameters.Add("@JobType", SqlDbType.Int); createEmp.Parameters["@UserName"].Value = req.UserName; createEmp.Parameters["@Password"].Value = req.Password; createEmp.Parameters["@FirstName"].Value = req.FirstName; createEmp.Parameters["@LastName"].Value = req.LastName; createEmp.Parameters["@Email"].Value = req.Email; createEmp.Parameters["@Phone"].Value = req.Phone; createEmp.Parameters["@StoreID"].Value = req.StoreID; createEmp.Parameters["@JobType"].Value = req.JobType; createEmp.ExecuteNonQuery(); getUserInfoCommand.Parameters["@Flag"].Value = 0; reader = getUserInfoCommand.ExecuteReader(); if (reader.HasRows) { if (reader.Read()) { res.EmployeeID = Convert.ToInt32(reader["EmployeeID"]); res.StoreID = Convert.ToInt32(reader["StoreID"]); res.JobType = Convert.ToInt32(reader["JobType"]); res.Status = Convert.ToInt32(reader["Status"]); res.FirstName = Convert.ToString(reader["FirstName"]); res.LastName = Convert.ToString(reader["LastName"]); res.Email = Convert.ToString(reader["Email"]); res.Phone = Convert.ToString(reader["Phone"]); } } reader.Close(); } catch (Exception ex) { res.error = ex.Message.ToString(); } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } // Return the results as Json. SendResultInfoAsJson(res); }