예제 #1
0
        //  delete

        public HttpResponseMessage Delete(int id)
        {
            try
            {
                using (testapiEntities entities = new testapiEntities())
                {
                    var entity = entities.employees.FirstOrDefault(e => e.id == id);

                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "employrr is requested not found"));
                    }
                    else
                    {
                        entities.employees.Remove(entity);
                        entities.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
예제 #2
0
        // put is update to database

        public HttpResponseMessage put(int id, [FromBody] employee employee)
        {
            try
            {
                using (testapiEntities entities = new testapiEntities())
                {
                    var entity = entities.employees.FirstOrDefault(e => e.id == id);

                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "employrr is requested not found"));
                    }
                    else
                    {
                        entity.Firstname = employee.Firstname;
                        entity.Lastname  = employee.Lastname;
                        entity.gender    = employee.gender;
                        entity.age       = employee.age;
                        entities.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
예제 #3
0
        //post method
        public HttpResponseMessage post([FromBody] employee employee)
        {
            try {
                using (testapiEntities entites = new testapiEntities())
                {
                    entites.employees.Add(employee);
                    entites.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));
            }
        }