public PostsResponse Edit(Post post) { PostsResponse response = new PostsResponse(); try { using (var context = new PersonalBlogEntities()) { var toEdit = context.Posts.Where(p => p.PostId == post.PostId).First(); context.Entry(toEdit).State = System.Data.Entity.EntityState.Modified; toEdit.CreatedDate = post.CreatedDate; toEdit.ImageFileName = post.ImageFileName; toEdit.IsApproved = post.IsApproved; toEdit.CategoryId = post.CategoryId; toEdit.PostBody = post.PostBody; toEdit.Tags.Clear(); toEdit.PostTitle = post.PostTitle; context.SaveChanges(); toEdit.Tags = context.Tags.AsEnumerable().Where(t => post.Tags.Any(postTag => postTag.TagId == t.TagId)).ToList(); context.SaveChanges(); response.Success = true; } } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public CategoryResponse Add(Category cat) { CategoryResponse response = new CategoryResponse(); if (string.IsNullOrEmpty(cat.CategoryName)) { response.Success = false; response.Message = "Category Name cannot be false"; return(response); } try { using (var context = new PersonalBlogEntities()) { context.Categories.Add(cat); context.SaveChanges(); response.Success = true; } } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public CategoryResponse Edit(Category cat) { CategoryResponse response = new CategoryResponse(); if (cat.CategoryId == 0 || string.IsNullOrEmpty(cat.CategoryName)) { response.Success = false; response.Message = "Required field was null"; return(response); } try { using (var context = new PersonalBlogEntities()) { var toEdit = context.Categories.Where(c => c.CategoryId == cat.CategoryId).First(); toEdit = cat; context.SaveChanges(); response.Success = true; } } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public TagsResponse Delete(int id) { TagsResponse response = new TagsResponse(); if (id == 0) { response.Success = false; response.Message = "Tag id was not passed or was null/default."; return(response); } try { using (var context = new PersonalBlogEntities()) { var toRemove = context.Tags.Where(t => t.TagId == id).First(); context.Tags.Remove(toRemove); context.SaveChanges(); response.Success = true; } } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public TagsResponse Edit(Tag tag) { TagsResponse response = new TagsResponse(); if (string.IsNullOrEmpty(tag.TagName) || tag.Posts.Count() < 1) { response.Success = false; response.Message = "Name is required, tag must be associated with at least one post."; return(response); } try { using (var context = new PersonalBlogEntities()) { var toEdit = context.Tags.Where(t => t.TagId == tag.TagId).First(); toEdit = tag; context.SaveChanges(); response.Success = true; } } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public Result <int> GetResult(PersonalBlogEntities db) { Result <int> result = new Result <int>(); //int save = db.SaveChanges(); int save = db.SaveChanges(); if (save > 0) { result.UserMessage = "Successful"; result.IsSucceeded = true; result.ProcessResult = save; } else { result.UserMessage = "Unsuccessful"; result.IsSucceeded = false; result.ProcessResult = save; } return(result); }
public PostsResponse Add(Post post) { PostsResponse response = new PostsResponse(); try { using (var context = new PersonalBlogEntities()) { context.Posts.Add(post); context.SaveChanges(); response.Success = true; } } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public StaticPgResponse Edit(StaticPage page) { StaticPgResponse response = new StaticPgResponse(); if (string.IsNullOrEmpty(page.PageBody)) { response.Success = false; response.Message = "Body missing. Oh the horror"; return(response); } if (page.StaticPageId > 0 && page.StaticPageId > 3) { response.Success = false; response.Message = "You messed up the id brah."; return(response); } using (var context = new PersonalBlogEntities()) { try { StaticPage toEdit = GetById(page.StaticPageId).StaticPage; toEdit.PageBody = page.PageBody; DbEntityEntry entry = context.Entry(toEdit); entry.State = EntityState.Modified; context.SaveChanges(); response.Success = true; } catch (Exception ex) { response.Success = false; response.Message = ex.Message + "This is very bad"; } return(response); } }