public EmployeeUtil GetEmployeeDetails(string employeeId)
        {
            int          empId = Int32.Parse(employeeId);
            EmployeeUtil emp   = list.Single(e => e.Id == empId);

            return(emp);
        }
        public IActionResult Post([FromBody] EmployeeModel model)
        {
            var ret = EmployeeUtil.CreateEmployee(model);

            try
            {
                if (ret.Status.Code == ResCode.Success)
                {
                    return(Ok(new { result = ret }));
                }
                else
                {
                    return(BadRequest(new { result = ret }));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(Unauthorized(new { result = ret }));
            }
        }
        public IActionResult Get()
        {
            var ret = EmployeeUtil.GetEmployee();

            try
            {
                if (ret.Status.Code == ResCode.Success)
                {
                    return(Ok(new { result = ret }));
                }
                else
                {
                    return(BadRequest(new { result = ret }));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(Unauthorized(new { result = ret }));
            }
        }
Ejemplo n.º 4
0
        public async Task <Employee> GetEmployeeDetails(int EmployeeId)
        {
            Employee employee = new Employee();
            string   empData  = string.Empty;
            var      response = await EmployeeUtil.GetHttpResponse("http://dummy.restapiexample.com/api/v1/employee/" + EmployeeId.ToString());

            if (response.IsSuccessStatusCode)
            {
                empData = await response.Content.ReadAsStringAsync();
            }
            if (empData != null)
            {
                employee = EmployeeUtil.JsonToObject <Employee>(empData);
                //EmployeeResponse empResponse =
                //       (EmployeeResponse)Newtonsoft.Json.JsonConvert.DeserializeObject(empData, typeof(EmployeeResponse));
                //if (empResponse != null)
                //{
                //    employee = (Employee) empResponse.data;

                //}
            }
            return(employee);
        }
Ejemplo n.º 5
0
        public async Task <List <Employee> > GetEmployeeData()
        {
            List <Employee> empList = new List <Employee>();
            string          empData = string.Empty;

            var response = await EmployeeUtil.GetHttpResponse("http://dummy.restapiexample.com/api/v1/employees");

            if (response.IsSuccessStatusCode)
            {
                empData = await response.Content.ReadAsStringAsync();
            }
            if (empData != null)
            {
                empList = EmployeeUtil.JsonToObject <List <Employee> >(empData);
                //EmployeeListResponse empResponse =
                //       (EmployeeListResponse)Newtonsoft.Json.JsonConvert.DeserializeObject(empData, typeof(EmployeeListResponse));
                //if (empResponse != null)
                //{
                //    empList = empResponse.data.ToList<Employee>();

                //}
            }
            return(empList);
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> OnPostAsync(string returnURL = "/index")
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var values = new Dictionary <string, string>
            {
                { "username", Username },
                { "password", Password }
            };

            HttpClient client = new HttpClient();

            var content = new StringContent("{\"username\":\"" + Username + "\",\"password\": \"" + Password + "\"}",
                                            Encoding.UTF8,
                                            "application/json");

            var response = await client.PostAsync("https://api-gateway-343.herokuapp.com/auth/login", content);

            var responseString = await response.Content.ReadAsStringAsync();

            try {
                Response resp = JsonConvert.DeserializeObject <Response>(responseString);


                if (!resp.Status)
                {
                    Error = "Username or Password is incorrect. Please try again";
                    return(Page());
                }
                else
                {
                    var jwt = resp.Token;

                    string[] parts     = jwt.Split(".".ToCharArray());
                    var      header    = parts[0];
                    var      payload   = parts[1];
                    var      signature = parts[2];//Base64UrlEncoded signature from the token

                    byte[] bytesToSign = Encoding.UTF8.GetBytes(string.Join(".", header, payload));

                    byte[] secret = Encoding.UTF8.GetBytes("secretkey");

                    var alg  = new HMACSHA256(secret);
                    var hash = alg.ComputeHash(bytesToSign);

                    var computedSignature = Base64UrlEncode(hash);

                    if (signature != computedSignature)
                    {
                        Error = "Something went wrong. Please try again.";
                        return(Page());
                    }

                    var handler = new JwtSecurityTokenHandler();

                    var token = handler.ReadToken(jwt) as JwtSecurityToken;

                    var userID = token.Claims.First(claim => claim.Type == "id").Value;

                    var userType = token.Claims.First(claim => claim.Type == "accountType").Value;

                    if (userType != "employee")
                    {
                        Error = "Username or Password is incorrect. Please try again";
                        return(Page());
                    }

                    EmployeeUtil empUtil = new EmployeeUtil(_context);

                    EmployeeAPIDAO userEmployee = empUtil.GetEmployeeByUserId(Convert.ToInt32(userID));

                    var userRole = userEmployee.RoleName;


                    // var role = user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role)?.Value;

                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, Username),
                        new Claim(ClaimTypes.Name, jwt),
                        new Claim(ClaimTypes.Role, userRole),
                        new Claim(ClaimTypes.PrimarySid, Convert.ToString(userEmployee.Id))
                    };

                    var             userIdentity = new ClaimsIdentity(claims, "login");
                    ClaimsPrincipal principal    = new ClaimsPrincipal(userIdentity);

                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                    return(Redirect(returnURL));
                }
            } catch (JsonReaderException) {
                Error = "Username or Password is incorrect. Please try again";
                return(Page());
            }
        }
 public KennUwareHR_EmployeeUtilityShould(DatabaseFixture fixture)
 {
     _employeeUtil = new EmployeeUtil(fixture.Context);
 }
        public IActionResult GetEmployees([FromQuery] string region,
                                          [FromQuery] string position,
                                          [FromQuery] string department,
                                          [FromQuery] string id,
                                          [FromQuery] string userId,
                                          [FromQuery] string limit,
                                          [FromQuery] string pageNum)
        {
            var EmployeeUtilities = new EmployeeUtil(_context);

            if (!String.IsNullOrEmpty(id))
            {
                EmployeeAPIDAO e = EmployeeUtilities.GetEmployee(Int32.Parse(id));

                if (e == null)
                {
                    return(BadRequest(new
                    {
                        error = "Invalid EmployeeId Provided"
                    }));
                }

                return(Ok(e));
            }

            if (!String.IsNullOrEmpty(userId))
            {
                EmployeeAPIDAO e = EmployeeUtilities.GetEmployeeByUserId(Int32.Parse(userId));

                if (e == null)
                {
                    return(BadRequest(new
                    {
                        error = "Invalid userId Provided"
                    }));
                }

                return(Ok(e));
            }

            int itemLimit;

            if (String.IsNullOrEmpty(limit))
            {
                itemLimit = 25;
            }
            else
            {
                itemLimit = Int32.Parse(limit);
            }

            int page;

            if (String.IsNullOrEmpty(pageNum))
            {
                page = 1;
            }
            else
            {
                page = Int32.Parse(pageNum);
            }


            List <EmployeeAPIDAO> emps = EmployeeUtilities
                                         .GetEmployees(region, position, department, ((page - 1) * itemLimit), itemLimit);

            if (emps.Count() < itemLimit)
            {
                return(Ok(new
                {
                    data = emps
                }));
            }

            var nextString = "";

            if (!String.IsNullOrEmpty(region))
            {
                nextString += "&region=" + region;
            }
            if (!String.IsNullOrEmpty(department))
            {
                nextString += "&department=" + department;
            }
            if (!String.IsNullOrEmpty(position))
            {
                nextString += "&position=" + position;
            }


            return(Ok(new
            {
                data = emps,
                page = new {
                    nextPage = "?pageNum=" + (page + 1) + "&limit=" + itemLimit + nextString
                }
            }));
        }
        public IActionResult Post([FromBody] List <EmployeeModel> model)
        {
            var ret = EmployeeUtil.CreateBulkEmployee(model);

            return(Ok(new { result = ret }));
        }