예제 #1
0
 public IEnumerable <Univercity> Get()
 {
     using (WebApiDemoContext contexts = new WebApiDemoContext())
     {
         return(contexts.Univercity.ToList());
     }
 }
예제 #2
0
 public HttpResponseMessage Put(int id, [FromBody] Univercity univercity)
 {
     try
     {
         using (WebApiDemoContext contexts = new WebApiDemoContext())
         {
             var context = contexts.Univercity.FirstOrDefault(e => e.UnivercityId == id);
             if (context == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Univercity with id = " + id.ToString() + "not found to delet"));
             }
             else
             {
                 context.UnivercityId   = univercity.UnivercityId;
                 context.UnivercityName = univercity.UnivercityName;
                 contexts.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK, context));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
예제 #3
0
        public string Put(int id, [FromBody] EmployeeDto employee)
        {
            var context = new WebApiDemoContext();

            EmployeeDto employeeUpdate = context
                                         .Employees
                                         .FirstOrDefault(e => e.EmployeeId == id);

            if (employeeUpdate == null)
            {
                return("Employee id " + id.ToString() + " not found.");
            }

            employeeUpdate.Firstname      = employee.Firstname;
            employeeUpdate.Lastname       = employee.Lastname;
            employeeUpdate.MiddleName     = employee.MiddleName;
            employeeUpdate.SuffixName     = employee.SuffixName;
            employeeUpdate.MobileNumber   = employee.MobileNumber;
            employeeUpdate.LandlineNumber = employee.LandlineNumber;

            context.SaveChanges();

            return("Employee number " + employee.EmployeeNumber
                   + " with name " + employee.Firstname
                   + " " + employee.Lastname + " successfully updated.");
        }
예제 #4
0
 public IEnumerable <Student> Get()
 {
     using (WebApiDemoContext contexts = new WebApiDemoContext())
     {
         // return all student
         return(contexts.Student.ToList());
     }
 }
예제 #5
0
        public string Post([FromBody] EmployeeDto employee)
        {
            var context = new WebApiDemoContext();

            context.Employees.Add(employee);
            context.SaveChanges();

            return("Employee #" + employee.EmployeeNumber + " is created successfully.");
        }
예제 #6
0
        //HttpGet
        // api/employee?dept=IT
        public IEnumerable <EmployeeDto> Get(string dept)
        {
            var context = new WebApiDemoContext();

            return(context
                   .Employees
                   .Where(e => e.Department == dept) //filter
                   .OrderBy(e => e.Firstname));      //sort
        }
예제 #7
0
        public IHttpActionResult Get(string field, string value, string sortField, string sortDir)
        {
            var context = new WebApiDemoContext();

            IQueryable <Product> query = context.Products;

            if (field.ToUpper() == "CATEGORY")
            {
                query = query.Where(p => p.Category == value);
            }
            else if (field.ToUpper() == "DESCRIPTION")
            {
                query = query.Where(p => p.Description.Contains(value));
            }
            else if (field.ToUpper() == "NUMBER")
            {
                query = query.Where(p => p.ProductNumber == value);
            }
            else
            {
                throw new Exception("Field is not available for filtering...");
            }

            if (sortField.ToUpper() == "NAME")
            {
                if (sortDir.ToUpper() == "ASC")
                {
                    query = query.OrderBy(p => p.Name);
                }
                else
                {
                    query = query.OrderByDescending(p => p.Name);
                }
            }
            else if (sortField.ToUpper() == "PRICE")
            {
                if (sortDir.ToUpper() == "ASC")
                {
                    query = query.OrderBy(p => p.UnitPrice);
                }
                else
                {
                    query = query.OrderByDescending(p => p.UnitPrice);
                }
            }
            else
            {
                query = query.OrderBy(p => p.ProductId);
            }

            var responseData = query.Select(p => new { p.ProductId, p.Name, p.Category, p.Description }).ToList();

            return(Ok(responseData));
        }
예제 #8
0
        // api/employee/4
        public EmployeeDto Get(int id)
        {
            var context = new WebApiDemoContext();

            var employee = context.Employees.FirstOrDefault(e => e.EmployeeId == id);

            if (employee == null)
            {
                throw new Exception("Employee id not found.");
            }

            return(employee);
        }
예제 #9
0
 public HttpResponseMessage Get(int id)
 {
     using (WebApiDemoContext contexts = new WebApiDemoContext())
     {
         var context = contexts.Univercity.FirstOrDefault(e => e.UnivercityId == id);
         if (context != null)
         {
             return(Request.CreateResponse(HttpStatusCode.OK, context));
         }
         else
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Univercity with id" + id.ToString() + "not found"));
         }
     }
 }
예제 #10
0
        public HttpResponseMessage Post([FromBody] Univercity univercity)
        {
            try
            {
                using (WebApiDemoContext contexts = new WebApiDemoContext())
                {
                    contexts.Univercity.Add(univercity);
                    contexts.SaveChanges();

                    var context = Request.CreateResponse(HttpStatusCode.Created, univercity);
                    context.Headers.Location = new Uri(Request.RequestUri + univercity.UnivercityId.ToString);
                    return(context);
                }
            }
            catch (Exception ex)
            {
                Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }
예제 #11
0
        public HttpResponseMessage Get(int id)
        {
            try
            {
                using (WebApiDemoContext contexts = new WebApiDemoContext())
                {
                    contexts.Student.Add(student);
                    contexts.SaveChanges();

                    var context = Request.CreateResponse(HttpStatusCode.Created, student);
                    context.Headers.Location = new Uri(Request.RequestUri + student.StudentId.ToString);
                    return(context);
                }
            }
            catch (Exception ex)
            {
                Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }
예제 #12
0
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (WebApiDemoContext contexts = new WebApiDemoContext())
         {
             var context = contexts.Student.FirstOrDefault(e => e.StudentId == id);
             if (context == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student with id = " + id.ToString() + "not found to delet"));
             }
             else
             {
                 contexts.Student.Remove(context);
                 contexts.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
예제 #13
0
        public List <EmployeeDto> Get()
        {
            var context = new WebApiDemoContext();

            return(context.Employees.ToList());
        }
예제 #14
0
 public ProductsController(WebApiDemoContext context)
 {
     _context = context;
 }
예제 #15
0
        public IHttpActionResult Get()
        {
            var context = new WebApiDemoContext();

            return(Ok(context.Products.ToList()));
        }