Beispiel #1
0
        public IHttpActionResult GetCustomers()
        {
            var db        = new EmployeeDBEntities1();
            var customers = db.Customers.ToList();

            return(Ok(customers));
        }
 public IEnumerable <Employee> Get()
 {
     using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
     {
         return(entities.Employees.ToList());
     }
 }
 public Employee Get(string code)
 {
     using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
     {
         return(entities.Employees.FirstOrDefault(e => e.code == code));
     }
 }
Beispiel #4
0
 public HttpResponseMessage GetEmployees()
 {
     try
     {
         EmployeeDBEntities1 context = new EmployeeDBEntities1();
         var employees             = context.Employees.ToList();
         List <EmployeeModel> list = new List <EmployeeModel>();
         foreach (var item in employees)
         {
             EmployeeModel emp = new EmployeeModel();
             emp.Id            = item.Id;
             emp.FirstName     = item.FirstName;
             emp.LastName      = item.LastName;
             emp.Email         = item.Email;
             emp.Gender        = item.Gender;
             emp.Country       = item.Country.Name;
             emp.State         = item.State.Name;
             emp.City          = item.City.Name;
             emp.Department    = item.Department.Name;
             emp.DateOfBirth   = item.DateOfBirth;
             emp.DateOfJoining = item.DateOfJoining;
             emp.IsActive      = item.IsActive;
             emp.Role          = item.RoleMaster.Name;
             emp.Phone         = item.Phone;
             list.Add(emp);
         }
         return(Request.CreateResponse((employees != null && employees.Count > 0) ? HttpStatusCode.OK : HttpStatusCode.NoContent, list.OrderByDescending(x => x.Id)));
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
Beispiel #5
0
 public HttpResponseMessage GetCities(int stateId)
 {
     try
     {
         EmployeeDBEntities1 context = new EmployeeDBEntities1();
         var cities           = context.Cities.Where(x => x.StateId == stateId).ToList();
         List <CityModel> lst = new List <CityModel>();
         if (cities != null && cities.Count > 0)
         {
             foreach (var item in cities)
             {
                 CityModel sm = new CityModel()
                 {
                     Id = item.Id, Name = item.Name
                 };
                 lst.Add(sm);
             }
         }
         return(Request.CreateResponse(HttpStatusCode.OK, lst));
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
        //[Route("authenticate")]
        public IHttpActionResult AuthenticateUser([FromBody] LoginRequest request)
        {
            EmployeeDBEntities1 context = new EmployeeDBEntities1();
            var          loginResponse  = new LoginResponse();
            LoginRequest loginrequest   = new LoginRequest {
            };

            loginrequest.Username = request.Username.ToLower();
            loginrequest.Password = request.Password;
            IHttpActionResult   response;
            HttpResponseMessage responseMsg = new HttpResponseMessage();
            bool isUsernamePasswordValid    = false;

            if (request != null)
            {
                isUsernamePasswordValid = context.Users.Any(u => u.Username == request.Username && u.Password == request.Password);
            }

            // if credentials are valid
            if (isUsernamePasswordValid)
            {
                string token = createToken(loginrequest.Username);
                //return the token
                return(Ok <string>(token));
            }
            else
            {
                // if credentials are not valid send unauthorized status code in response
                loginResponse.responseMsg.StatusCode = HttpStatusCode.Unauthorized;
                response = ResponseMessage(loginResponse.responseMsg);
                return(response);
            }
        }
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
                {
                    var entity = entities.Employees.FirstOrDefault(e => e.ID == id);
                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " not found to delete"));
                    }

                    else
                    {
                        entities.Employees.Remove(entity);
                        entities.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
 public HttpResponseMessage Put(int id, [FromBody] Employee employee)
 {
     try
     {
         using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
         {
             var entity = entities.Employees.FirstOrDefault(e => e.ID == id);
             if (entity == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " has no record "));
             }
             else
             {
                 entity.FirstName = employee.FirstName;
                 entity.LastName  = employee.LastName;
                 entity.Gender    = employee.Gender;
                 entity.Salary    = employee.Salary;
                 entities.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK, entity));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Beispiel #9
0
 public EmployeeController()
 {
     if (_employeeDBEntities == null)
     {
         EmployeeDBEntities1 employeeDBEntities = new EmployeeDBEntities1();
         _employeeDBEntities = employeeDBEntities;
     }
 }
Beispiel #10
0
        public IHttpActionResult DeleteCustomer(int id)
        {
            var db       = new EmployeeDBEntities1();
            var customer = new Customer {
                Id = id
            };

            db.Customers.Attach(customer);
            db.Customers.Remove(customer);
            db.SaveChanges();
            return(Ok(id));
        }
Beispiel #11
0
 public HttpResponseMessage GetRoles()
 {
     try
     {
         EmployeeDBEntities1 context = new EmployeeDBEntities1();
         var countries = context.RoleMasters.ToList();
         return(Request.CreateResponse(HttpStatusCode.OK, countries));
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
        public HttpResponseMessage LoadEmployeeById(int id)
        {
            using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
            {
                var entity = entities.Employees.FirstOrDefault(e => e.ID == id);

                if (entity != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, entity));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id: " + id.ToString() + " not found"));
                }
            }
        }
Beispiel #13
0
 public IHttpActionResult SaveCustomer(Customer customer)
 {
     if (customer.Id > 0)
     {
         var db         = new EmployeeDBEntities1();
         var dbCustomer = db.Customers.FirstOrDefault(x => x.Id == customer.Id);
         dbCustomer.Name    = customer.Name;
         dbCustomer.Address = customer.Address;
         dbCustomer.Phone   = customer.Phone;
         db.SaveChanges();
     }
     else
     {
         var db = new EmployeeDBEntities1();
         db.Customers.Add(customer);
         db.SaveChanges();
     }
     return(Ok(customer.Id));
 }
Beispiel #14
0
 public HttpResponseMessage GetEmployeeById(int id)
 {
     try
     {
         EmployeeDBEntities1 context = new EmployeeDBEntities1();
         var employees = context.Employees.Where(emp => emp.Id == id).FirstOrDefault();
         if (employees == null)
         {
             return(Request.CreateResponse(HttpStatusCode.NoContent, string.Format("No Employee with Id = {0} found", id)));
         }
         else
         {
             EmployeeModel employee = new EmployeeModel()
             {
                 Id            = employees.Id,
                 FirstName     = employees.FirstName,
                 LastName      = employees.LastName,
                 Email         = employees.Email,
                 Gender        = employees.Gender,
                 Country       = employees.Country.Name,
                 State         = employees.State.Name,
                 City          = employees.City.Name,
                 Department    = employees.Department.Name,
                 DateOfBirth   = employees.DateOfBirth,
                 DateOfJoining = employees.DateOfJoining,
                 IsActive      = employees.IsActive,
                 Role          = employees.RoleMaster.Name,
                 Phone         = employees.Phone,
                 CountryId     = employees.CountryId,
                 StateId       = employees.StateId,
                 CityId        = employees.CityId,
                 DepartmentId  = employees.DepartmentId,
                 RoleType      = employees.RoleType
             };
             return(Request.CreateResponse(HttpStatusCode.OK, employee));
             //   return Request.CreateResponse(HttpStatusCode.NoContent);
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
        public HttpResponseMessage Post([FromBody] Employee employee)
        {
            try
            {
                using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
                {
                    entities.Employees.Add(employee);
                    entities.SaveChanges();

                    var message = Request.CreateResponse(HttpStatusCode.Created, employee);
                    message.Headers.Location = new Uri(Request.RequestUri + employee.ID.ToString());
                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
        public HttpResponseMessage LoadEmployee(string gender = "All")
        {
            using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
            {
                switch (gender.ToLower())
                {
                case "all":
                    return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.ToList()));

                case "male":
                    return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.
                                                  Where(e => e.Gender.ToLower() == "male").ToList()));

                case "female":
                    return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.
                                                  Where(e => e.Gender.ToLower() == "female").ToList()));

                default:
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                       "Value for gender must All, Male or Female " + gender + " is invalid "));
                }
            }
        }