예제 #1
0
        public async Task <EmployeeResponseModel> UpdateEmployee(EmployeeUpdateRequestModel employeeUpdateRequestModel, int id)
        {
            var dbEmployee = await _employeesRepository.GetByIdAsync(id);

            if (dbEmployee == null)
            {
                throw new Exception("Employee Not Exist");
            }

            var employee = new Employees
            {
                Id          = dbEmployee.Id,
                Name        = employeeUpdateRequestModel.Name == null ? dbEmployee.Name : employeeUpdateRequestModel.Name,
                Password    = employeeUpdateRequestModel.Password == null ? dbEmployee.Password : employeeUpdateRequestModel.Password,
                Designation = employeeUpdateRequestModel.Designation == null ? dbEmployee.Designation : employeeUpdateRequestModel.Designation,
            };
            var updateEmployee = await _employeesRepository.UpdateAsync(employee);

            var response = new EmployeeResponseModel
            {
                Id          = updateEmployee.Id,
                Name        = updateEmployee.Name,
                Designation = updateEmployee.Designation,
            };

            return(response);
        }
예제 #2
0
        public async Task <EmployeeResponseModel> GetEmployeeAsync(int id)
        {
            EmployeeResponseModel  employeeModel = new EmployeeResponseModel();
            IEnumerable <Employee> employees     = await employeeService.GetEmployees();

            Employee employee = employees.SingleOrDefault(s => s.Id == id);

            if (employee != null)
            {
                ISalary salary = salaryFactory.SelectEmployeeType(employee.ContractTypeName);
                employeeModel = new EmployeeResponseModel
                {
                    Id               = employee.Id,
                    RoleId           = employee.RoleId,
                    RoleName         = employee.RoleName,
                    Name             = employee.Name,
                    RoleDescription  = employee.RoleDescription,
                    ContractTypeName = employee.ContractTypeName,
                    Salary           = salary.GetSalary(employee),
                    AnualSalary      = salary.GetAnualSalary(employee)
                };
            }


            return(employeeModel);
        }
예제 #3
0
        public async Task <EmployeeResponseModel> CreateEmployee(EmployeeRequestModel employeeRequestModel)
        {
            var dbEmployee = await _employeesRepository.GetEmployeeByName(employeeRequestModel.Name);

            if (dbEmployee != null)
            {
                throw new Exception("Employee exists, try Login");
            }
            //var salt = CreateSalt();
            //var hashedPassword = CreateHashedPassword(employeeRequestModel.Password, salt);
            var employee = new Employees
            {
                Name        = employeeRequestModel.Name,
                Password    = employeeRequestModel.Password,
                Designation = employeeRequestModel.Designation,
            };
            var response = await _employeesRepository.AddAsync(employee);

            var createdEmployee = new EmployeeResponseModel
            {
                Id          = response.Id,
                Name        = response.Name,
                Designation = response.Designation,
            };

            return(createdEmployee);
        }
예제 #4
0
        public async Task <EmployeeResponseModel> Login(string name, string password)
        {
            var dbEmployee = await _employeesRepository.GetEmployeeByName(name);

            if (dbEmployee == null)
            {
                return(null);
            }
            // get the password from UI and salt from above step from database and call CreateHashedPassword method

            var hashedPassword = CreateHashedPassword(password, dbEmployee.Salt);

            if (hashedPassword == dbEmployee.HashedPassword)
            {
                // user entered correct password
                var response = new EmployeeResponseModel
                {
                    Id          = dbEmployee.Id,
                    Name        = dbEmployee.Name,
                    Designation = dbEmployee.Designation,
                };
                return(response);
            }
            return(null);
        }
        public async Task <EmployeeResponseModel> Get(string employeeId)
        {
            var readModel = await QueryProcessor.ProcessAsync(
                new EmployeeGetQuery(new EmployeeId(employeeId)), CancellationToken.None)
                            .ConfigureAwait(false);

            var response = new EmployeeResponseModel {
                TenantId = readModel.TenantId, Id = readModel.Id.GetGuid().ToString( ), FullName = readModel.FullName, Department = readModel.Department
            };

            return(response);
        }
예제 #6
0
        public async Task <EmployeeResponseModel> GetEmployeeById(int id)
        {
            var employee = await _employeesRepository.GetByIdAsync(id);

            var response = new EmployeeResponseModel
            {
                Id          = employee.Id,
                Name        = employee.Name,
                Designation = employee.Designation,
            };

            return(response);
        }
        public IActionResult SearchEmployee([FromRoute] int EmployeeId)
        {
            try
            {
                EmployeeResponseModel response = new EmployeeResponseModel();

                string cacheKey = EmployeeId.ToString();
                string serializedEmployeeData;

                var encodedData = distributedCache.Get(cacheKey);

                if (encodedData != null)
                {
                    serializedEmployeeData = Encoding.UTF8.GetString(encodedData);
                    response = JsonConvert.DeserializeObject <EmployeeResponseModel>(serializedEmployeeData);
                }
                else
                {
                    response = this.employeeBusiness.SearchEmployee(EmployeeId);
                    serializedEmployeeData = JsonConvert.SerializeObject(response);
                    encodedData            = Encoding.UTF8.GetBytes(serializedEmployeeData);
                    var options = new DistributedCacheEntryOptions()
                                  .SetSlidingExpiration(TimeSpan.FromMinutes(20))
                                  .SetAbsoluteExpiration(DateTime.Now.AddHours(6));
                    distributedCache.Set(cacheKey, encodedData, options);
                }

                // Call the Search Employee Method of Employee Business classs
                //var response = this.employeeBusiness.SearchEmployee(EmployeeId);

                // check if response count is equal to 1
                if (!response.EmployeeId.Equals(0))
                {
                    bool status  = true;
                    var  message = "Record Found";
                    return(this.Ok(new { status, message, data = response }));
                }
                else
                {
                    bool status  = false;
                    var  message = "Record Not Found";
                    return(this.NotFound(new { status, message }));
                }
            }
            catch (Exception e)
            {
                return(this.BadRequest(new { status = false, message = e.Message }));
            }
        }
        private string GenreateJWT(EmployeeResponseModel employeeResponseModel)
        {
            // we will use the token libraries to create token

            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.NameIdentifier, employeeResponseModel.Id.ToString()),
                new Claim(JwtRegisteredClaimNames.GivenName, employeeResponseModel.Name),
            };

            // create identity object and store claims
            var identityClaims = new ClaimsIdentity();

            identityClaims.AddClaims(claims);

            // read the secret key from app settings, make sure secret key is unique and not guessable
            // In real world we use something like Azure KeyVault to store any secrets of application

            var secretKey   = _configuration["JwtSettings:SecretKey"];
            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));

            // get the expiration time of the token
            var expires = DateTime.UtcNow.AddDays(_configuration.GetValue <int>("JwtSettings:Expiration"));

            //pick an hashing algorithm
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);


            // create the token object that yu will use to create the token that will include all the information such as credentials, claims, expiration time,

            var tokenHandler = new JwtSecurityTokenHandler();

            var tokenDescriptor = new SecurityTokenDescriptor()
            {
                Subject            = identityClaims,
                Expires            = expires,
                SigningCredentials = credentials,
                Issuer             = _configuration["JwtSettings:Issuer"],
                Audience           = _configuration["JwtSettings:Audience"]
            };

            var encodedJwt = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(encodedJwt));
        }
예제 #9
0
        public async Task <EmployeeResponseModel> ValidateUser(string name, string password)
        {
            var dbEmployee = await _employeesRepository.GetEmployeeByName(name);

            if (dbEmployee == null)
            {
                return(null);
            }
            if (password == dbEmployee.Password)
            {
                var response = new EmployeeResponseModel
                {
                    Id          = dbEmployee.Id,
                    Name        = dbEmployee.Name,
                    Designation = dbEmployee.Designation,
                };
                return(response);
            }
            return(null);
        }
예제 #10
0
        /// <summary>
        /// This Method is used to add new record
        /// </summary>
        /// <param name="employeeModel">It is an object of Employee model class</param>
        /// <returns>If record added successfully it returns true</returns>
        public EmployeeResponseModel AddEmployee(EmployeeRequestModel employeeRequestModel)
        {
            try
            {
                EmployeeResponseModel employeeResponseModel = new EmployeeResponseModel();

                SqlCommand sqlCommand = this.StoreProcedureConnection("spInsertRecord", this.sqlConnection);

                // Add the First Name Value to database
                sqlCommand.Parameters.AddWithValue("@FirstName", employeeRequestModel.FirstName);

                // Add the Last Name Value to database
                sqlCommand.Parameters.AddWithValue("@LastName", employeeRequestModel.LastName);

                // Add the Gender Value to database
                sqlCommand.Parameters.AddWithValue("@Gender", employeeRequestModel.Gender);

                // Add the Email Id Value to database
                sqlCommand.Parameters.AddWithValue("@EmailId", employeeRequestModel.EmailId);

                // Add the Phone Number Value to database
                sqlCommand.Parameters.AddWithValue("@PhoneNumber", employeeRequestModel.PhoneNumber);

                // Add the City Value to database
                sqlCommand.Parameters.AddWithValue("@City", employeeRequestModel.City);

                // Opens the Sql Connection
                this.sqlConnection.Open();
                int status = 1;

                // Read the employee data from database using SqlDataReader
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    status = sqlDataReader.GetInt32(0);
                    if (status == 0)
                    {
                        return(employeeResponseModel);
                    }
                    // Read the Employee Id and convert it into integer
                    employeeResponseModel.EmployeeId = Convert.ToInt32(sqlDataReader["EmployeeId"]);

                    // Read the First Name
                    employeeResponseModel.FirstName = sqlDataReader["FirstName"].ToString();

                    // Read the Last Name
                    employeeResponseModel.LastName = sqlDataReader["LastName"].ToString();

                    // Read the Email Id
                    employeeResponseModel.EmailId = sqlDataReader["EmailId"].ToString();

                    // Read the Gender
                    employeeResponseModel.Gender = sqlDataReader["Gender"].ToString();

                    // Read the Phone Number
                    employeeResponseModel.PhoneNumber = sqlDataReader["PhoneNumber"].ToString();

                    // Read the City
                    employeeResponseModel.City = sqlDataReader["City"].ToString();

                    // Read the Registration date and convert into Date Time
                    employeeResponseModel.RegistrationDate = Convert.ToDateTime(sqlDataReader["RegistrationDate"]);
                }

                // close Sql Connection
                this.sqlConnection.Close();

                return(employeeResponseModel);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
예제 #11
0
        /// <summary>
        ///  This Method is used to Search the Record
        /// </summary>
        /// <param name="employeeModel">It contains the Object of Employee Model</param>
        /// <returns>It returns the searched record</returns>
        public EmployeeResponseModel SearchEmployee(int EmployeeId)
        {
            try
            {
                // New Ilist is created to store the result
                //IList<EmployeeResponseModel> employeeModelsList = new List<EmployeeResponseModel>();

                // create the object of SqlCommand and send the command and connection object
                SqlCommand sqlCommand = this.StoreProcedureConnection("spSearchEmployee", this.sqlConnection);

                if (EmployeeId > 0)
                {
                    // Add the Employee Id Value
                    sqlCommand.Parameters.AddWithValue("@EmployeeId", EmployeeId);
                }
                else
                {
                    sqlCommand.Parameters.AddWithValue("@EmployeeId", EmployeeId);
                    // sqlCommand.Parameters.AddWithValue("@EmailId", EmailId);
                }

                EmployeeResponseModel employeeModel = new EmployeeResponseModel();

                // Opens the Sql Connection
                this.sqlConnection.Open();

                int status = 1;

                // Read the employee data from database using SqlDataReader
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

                while (sqlDataReader.Read())
                {
                    status = sqlDataReader.GetInt32(0);
                    if (status == 0)
                    {
                        return(employeeModel);
                    }
                    // Read the Employee Id and convert it into integer
                    employeeModel.EmployeeId = Convert.ToInt32(sqlDataReader["EmployeeId"]);

                    // Read the First Name
                    employeeModel.FirstName = sqlDataReader["FirstName"].ToString();

                    // Read the Last Name
                    employeeModel.LastName = sqlDataReader["LastName"].ToString();

                    // Read the Email Id
                    employeeModel.EmailId = sqlDataReader["EmailId"].ToString();

                    // Read the Gender
                    employeeModel.Gender = sqlDataReader["Gender"].ToString();

                    // Read the Phone Number
                    employeeModel.PhoneNumber = sqlDataReader["PhoneNumber"].ToString();

                    // Read the City
                    employeeModel.City = sqlDataReader["City"].ToString();

                    // Read the Registration date and convert into Date Time
                    employeeModel.RegistrationDate = Convert.ToDateTime(sqlDataReader["RegistrationDate"]);

                    // Add all the data into Ilist
                    // employeeModelsList.Add(employeeModel);
                }

                // close Sql Connection
                this.sqlConnection.Close();

                // return the Ilist
                return(employeeModel);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }
예제 #12
0
        /// <summary>
        /// This Method is used to Read all Record
        /// </summary>
        /// <returns>It returns the all record</returns>
        public List <EmployeeResponseModel> ReadEmployee()
        {
            try
            {
                // New Ilist is created to store the result
                List <EmployeeResponseModel> employeeModelsList = new List <EmployeeResponseModel>();

                // create the object of SqlCommand and send the command and connection object
                SqlCommand sqlCommand = this.StoreProcedureConnection("spReadRecord", this.sqlConnection);

                // Opens the Sql Connection
                this.sqlConnection.Open();

                // Read the employee data from database using SqlDataReader
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

                // Data is read upto data is present
                while (sqlDataReader.Read())
                {
                    // Create the object of Employee Model Class
                    EmployeeResponseModel employeeModel = new EmployeeResponseModel();

                    // Read the Employee Id and convert it into integer
                    employeeModel.EmployeeId = Convert.ToInt32(sqlDataReader["EmployeeId"]);

                    // Read the First Name
                    employeeModel.FirstName = sqlDataReader["FirstName"].ToString();

                    // Read the Last Name
                    employeeModel.LastName = sqlDataReader["LastName"].ToString();

                    // Read the Email Id
                    employeeModel.EmailId = sqlDataReader["EmailId"].ToString();

                    // Read the Gender
                    employeeModel.Gender = sqlDataReader["Gender"].ToString();

                    // Read the Phone Number
                    employeeModel.PhoneNumber = sqlDataReader["PhoneNumber"].ToString();

                    // Read the City
                    employeeModel.City = sqlDataReader["City"].ToString();

                    // Read the Registration date and convert into Date Time
                    employeeModel.RegistrationDate = Convert.ToDateTime(sqlDataReader["RegistrationDate"]);

                    // Add all the data into Ilist
                    employeeModelsList.Add(employeeModel);
                }

                // close Sql Connection
                this.sqlConnection.Close();

                // return the Ilist
                return(employeeModelsList);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }
예제 #13
0
        public async Task <ActionResult <string> > GetAsync(int id)
        {
            EmployeeResponseModel employeeResponse = await employeeLogic.GetEmployeeAsync(id);

            return(Ok(employeeResponse));
        }