//Delete category public void DeleteCat(int Id) { using (WalkAboutDb dc = new WalkAboutDb()) { dc.Categories.Remove(dc.Categories.Single<Category>(c => c.CatId == Id)); //or use a regular Linq query } }
//readonly Models.WalkAboutDb dc = new Models.WalkAboutDb("WalkAboutDb"); public IEnumerable<User> GetAll() { using (WalkAboutDb dc = new WalkAboutDb()) { var AllUsers = (from a in dc.Users select a).ToList(); return AllUsers; } }
//get category by ID public Category GetByID(int Id) { using (WalkAboutDb dc = new WalkAboutDb()) { var CatById = (from c in dc.Categories where c.CatId == Id select c); return CatById.First(); } }
//get all categories public IEnumerable<Category> GetAllCat() { using (WalkAboutDb dc = new WalkAboutDb()) { var allCategory = (from a in dc.Categories select a).ToList(); return allCategory; } }
//PUT public void UpdateUser(User user) { using (WalkAboutDb dc = new WalkAboutDb()) { var CurUser = (from a in dc.Users where a.UsrId == user.UsrId select a).FirstOrDefault(); CurUser.Fname = user.Fname; CurUser.Lname = user.Lname; CurUser.CurrentCity = user.CurrentCity; dc.SaveChanges(); } }
// GET /api/user/5 public User GetById(string Id) { using (WalkAboutDb dc = new WalkAboutDb()) { var UserByID = (from a in dc.Users where a.UsrId == Id select a); return UserByID.First(); //or //return dc.Users.FirstOrDefault(u => u.UsrId == Id); } }
// POST public void AddUser(User user) { using (WalkAboutDb dc = new WalkAboutDb()) { User usr = new User { UsrId = user.UsrId , Fname = user.Fname , Lname = user.Lname , CurrentCity = user.CurrentCity }; dc.Users.Add(usr); dc.SaveChanges(); } }
//Post Category public void AddCategory(Category category) { using (WalkAboutDb dc = new WalkAboutDb()) { Category cat = new Category { CatId = category.CatId, CategoryName = category.CategoryName, IconNameLocal = category.IconNameLocal }; dc.Categories.Add(cat); dc.SaveChanges(); } }
//handles updates //Put Category public Category UpdateCategory(Category category) { using (WalkAboutDb dc = new WalkAboutDb()) { //get the current category var CurCat = (from a in dc.Categories where a.CatId == category.CatId select a).FirstOrDefault (); //perform update CurCat.CategoryName = category.CategoryName; CurCat.IconNameLocal = category.IconNameLocal; //persist to dbase dc.SaveChanges(); return CurCat; } }