コード例 #1
0
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                using (var db = new ApiDemoEntities())
                {
                    var std = db.Deanslists.FirstOrDefault(s => s.ID == id);

                    if (std != null)
                    {
                        db.Deanslists.Remove(std);
                        db.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student with id=" + id.ToString() + " was not found to be deleted."));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
コード例 #2
0
        public HttpResponseMessage Put(int id, [FromBody] Deanslist std)
        {
            try
            {
                using (var db = new ApiDemoEntities())
                {
                    var stdnt = db.Deanslists.FirstOrDefault(s => s.ID == id);

                    if (stdnt != null)
                    {
                        stdnt.ID    = std.ID;
                        stdnt.Name  = std.Name;
                        stdnt.Grade = std.Grade;

                        db.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Id = " + id.ToString() + " wasn't found."));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
コード例 #3
0
 public IEnumerable <Student> Get()
 {
     using (var db = new ApiDemoEntities())
     {
         return(db.Students.ToList());
     }
 }
コード例 #4
0
 public IEnumerable <Deanslist> Get()
 {
     using (var db = new ApiDemoEntities())
     {
         return(db.Deanslists.ToList());
     }
 }
コード例 #5
0
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                using (var db = new ApiDemoEntities())
                {
                    var emp = db.Employees.FirstOrDefault(e => e.ID == id);

                    if (emp == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id " + id.ToString() + " was not found to delete."));
                    }
                    else
                    {
                        db.Employees.Remove(emp);
                        db.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
コード例 #6
0
        public HttpResponseMessage Put(int id, [FromBody] Student stdnt)
        {
            try
            {
                using (var db = new ApiDemoEntities())
                {
                    var std = db.Students.FirstOrDefault(s => s.ID == id);

                    if (std == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student with id = " + id.ToString() + " was not found to be updated."));
                    }
                    else
                    {
                        std.ID     = stdnt.ID;
                        std.Name   = stdnt.Name;
                        std.Gender = stdnt.Gender;

                        db.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
コード例 #7
0
        public HttpResponseMessage Put(int id, [FromBody] Employee employee)
        {
            try
            {
                using (var db = new ApiDemoEntities())
                {
                    var emp = db.Employees.FirstOrDefault(e => e.ID == id);

                    if (emp == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, " Employee with id = " + id.ToString() + " was not found to be updated."));
                    }
                    else
                    {
                        emp.FirstName = employee.FirstName;
                        emp.LastName  = employee.LastName;
                        emp.Gender    = employee.Gender;
                        emp.Salary    = employee.Salary;

                        db.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
コード例 #8
0
 public IEnumerable <Employee> Get()
 {
     using (var db = new ApiDemoEntities())
     {
         return(db.Employees.ToList());
     }
 }
コード例 #9
0
        public HttpResponseMessage Get(int id)
        {
            try
            {
                using (var db = new ApiDemoEntities())
                {
                    var std = db.Deanslists.FirstOrDefault(s => s.ID == id);

                    return(Request.CreateResponse(HttpStatusCode.OK, std));
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
コード例 #10
0
        public HttpResponseMessage Get(int id)
        {
            using (var db = new ApiDemoEntities())
            {
                var stdnt = db.Students.FirstOrDefault(std => std.ID == id);

                if (stdnt != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, stdnt));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Student with id= " + id.ToString() + " was not found."));
                }
            }
        }
コード例 #11
0
        public HttpResponseMessage Get(int id)
        {
            using (var db = new ApiDemoEntities())
            {
                var emps = db.Employees.FirstOrDefault(e => e.ID == id);

                if (emps != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, emps));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id = " + id.ToString() + " was not found"));
                }
            }
        }
コード例 #12
0
        public HttpResponseMessage Post([FromBody] Deanslist std)
        {
            try
            {
                using (var db = new ApiDemoEntities())
                {
                    db.Deanslists.Add(std);
                    db.SaveChanges();

                    var msg = Request.CreateResponse(HttpStatusCode.Created);
                    return(msg);
                }
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e));
            }
        }
コード例 #13
0
        public HttpResponseMessage Post([FromBody] Employee emp)
        {
            try
            {
                using (var db = new ApiDemoEntities())
                {
                    db.Employees.Add(emp);
                    db.SaveChanges();

                    var message = Request.CreateResponse(HttpStatusCode.Created, emp);
                    message.Headers.Location = new Uri(Request.RequestUri + emp.ID.ToString());
                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
コード例 #14
0
        public HttpResponseMessage Get(string gender = "All")
        {
            string username = Thread.CurrentPrincipal.Identity.Name;

            using (var entities = new ApiDemoEntities())
            {
                switch (username.ToLower())
                {
                case "male":
                    return(Request.CreateResponse(HttpStatusCode.OK,
                                                  entities.Students.Where(e => e.Gender.ToLower() == "male").ToList()));

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

                default:
                    return(Request.CreateResponse(HttpStatusCode.BadRequest));
                }
            }
        }
コード例 #15
0
        public HttpResponseMessage  Get(string gender)
        {
            using (var db = new ApiDemoEntities())
            {
                switch (gender.ToLower())
                {
                case "all":
                    return(Request.CreateResponse(HttpStatusCode.OK, db.Employees.ToList()));

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

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

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