public bool UpdateCategory(CategoryItem_Create_Edit model) { using (var ctx = new ApplicationDbContext()) { //same as GetNoteById method var entity = ctx .Categories //checks if the id of the model we put in matches an existing category id from the list of categories in DbContext .Single(e => e.Id == model.Id && e.OwnerId == _userId); //sets the category's values to the new model's values entity.Name = model.Name; //returns a true/false value of whether we saved or not return(ctx.SaveChanges() == 1); } }
public bool CreateCategory(CategoryItem_Create_Edit model) { var entity = new Category() { //use the categoryService to create a guid to set our id equal to OwnerId = _userId, Name = model.Name, }; //created an instance of the DbContext, added our newly created category, entity, and saved changes. using (var ctx = new ApplicationDbContext()) { ctx.Categories.Add(entity); return(ctx.SaveChanges() == 1); } }
public IHttpActionResult Put(CategoryItem_Create_Edit note) { //if the user inputted model is not valid if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateCategoryService(); //if the DbContext did not save the changes we made for some reason if (!service.UpdateCategory(note)) { return(InternalServerError()); } //good :) return(Ok()); }
public IHttpActionResult Post(CategoryItem_Create_Edit category) { //returns a bad request if you send the wrong kind of model if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateCategoryService(); //returns an internal server error if a note cannot be created from the valid model if (!service.CreateCategory(category)) { return(InternalServerError()); } //a note was created from the valid model return(Ok()); }