Esempio n. 1
0
        public HttpResponseMessage Get(string gender = "All")
        {
            string username = Thread.CurrentPrincipal.Identity.Name;

            gender = username;

            using (API018Entities entities = new API018Entities())
            {
                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 be All, Male or Female. " + gender + " is invalid."));
                }
            }
        }
Esempio n. 2
0
        public HttpResponseMessage Put(int id, [FromBody] Employee employee)
        {
            try
            {
                using (API018Entities entities = new API018Entities())
                {
                    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 update"));
                    }
                    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));
            }
        }
Esempio n. 3
0
 public static bool Login(string username, string password)
 {
     using (API018Entities entities = new API018Entities())
     {
         return(entities.Users.Any(user => user.Username.Equals(username,
                                                                StringComparison.OrdinalIgnoreCase) && user.Password == password));
     }
 }
Esempio n. 4
0
 public HttpResponseMessage Get(int id)
 {
     using (API018Entities entities = new API018Entities())
     {
         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"));
         }
     }
 }
Esempio n. 5
0
        public HttpResponseMessage Post([FromBody] Employee employee)
        {
            try
            {
                using (API018Entities entities = new API018Entities())
                {
                    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));
            }
        }
Esempio n. 6
0
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (API018Entities entities = new API018Entities())
         {
             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));
     }
 }