//inserting values using Url or in a single querry string //http://localhost:49965/api/UriTest?Sno=1&Ename=FN2&Ephone=9999&Eaddress=biharipur public HttpResponseMessage Put(int Sno, [FromUri] ApiTesting employee) { try { using (testingEntities entities = new testingEntities()) { var entity = entities.ApiTestings.FirstOrDefault(e => e.Sno == Sno); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id " + Sno.ToString() + " not found to update")); } else { entity.Ename = employee.Ename; entity.Ephone = employee.Ephone; entity.Eaddress = employee.Eaddress; entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, entity)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
// Delete of Database Entry public HttpResponseMessage Delete(int id) { try { using (testingEntities entities = new testingEntities()) { var entity = entities.ApiTestings.FirstOrDefault(e => e.Sno == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " not found to delete")); } else { entities.ApiTestings.Remove(entity); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
//[Authorize] public IEnumerable <ApiTesting> Get() { using (testingEntities entities = new testingEntities()) { return(entities.ApiTestings.ToList()); } }
//Get Method with HttpResponse public HttpResponseMessage Get(int id) { using (testingEntities entities = new testingEntities()) { var entity = entities.ApiTestings.FirstOrDefault(e => e.Sno == id); if (entity != null) { return(Request.CreateResponse(HttpStatusCode.OK, entity)); } else { return(Request.CreateResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " not found")); } } }
// Querry Strin Testing //http://localhost:49965/api/UriTest?addresses=delhi public HttpResponseMessage Get(string addresses = "All") { using (testingEntities entities = new testingEntities()) { switch (addresses.ToLower()) { case "all": return(Request.CreateResponse(HttpStatusCode.OK, entities.ApiTestings.ToList())); case "delhi": return(Request.CreateResponse(HttpStatusCode.OK, entities.ApiTestings.Where(e => e.Eaddress.ToLower() == "delhi").ToList())); case "haryana": return(Request.CreateResponse(HttpStatusCode.OK, entities.ApiTestings.Where(e => e.Eaddress.ToLower() == "haryana").ToList())); default: return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Value for addresses . " + addresses + " is invalid.")); } } }
//Post Method with Location of Inserted Record public HttpResponseMessage Post([FromBody] ApiTesting apiObject) { try { using (testingEntities entities = new testingEntities()) { entities.ApiTestings.Add(apiObject); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, apiObject); message.Headers.Location = new Uri(Request.RequestUri + apiObject.Sno.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public IManagedTypeModel Factory(object obj, testingEntities db) { CocosPackage f; try { f = obj as CocosPackage; } catch (Exception) { return(CreateNewInstance(db)); } if (f.Id == 0) { return(CreateNewInstance(db)); } return(new CocosPackageModel { age = f.age, cocoId = f.CocosId, id = f.Id, weight = f.Coco.Weight, db = db }); }
public IManagedTypeModel CreateNewInstance(testingEntities db) { return(new CocosPackageModel { age = 0, cocoId = null, id = null, weight = 0, db = db }); }