Пример #1
0
        public CategoryResponse GetById(int id)
        {
            CategoryResponse response = new CategoryResponse();

            if (id == 0)
            {
                response.Success = false;
                response.Message = "Value for Id not passed in.";
                return(response);
            }

            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.Categories = context.Categories
                                          .Where(c => c.CategoryId == id)
                                          .ToList();
                    if (response.Categories.Count == 0)
                    {
                        response.Success = false;
                        response.Message = "Nothing found.";
                        return(response);
                    }
                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }

                return(response);
            }
        }
Пример #2
0
        public PostsResponse Edit(Post post)
        {
            var context  = new PersonalBlogEntities();
            var response = new PostsResponse();

            if (string.IsNullOrEmpty(post.PostTitle))
            {
                response.Success = false;
                response.Message = "The post title cannot be left blank.";
            }
            else if (!post.IsApproved)
            {
                response.Success = false;
                response.Message = "This post has content that violates our blogging policy.";
            }
            else if (string.IsNullOrEmpty(post.PostBody))
            {
                response.Success = false;
                response.Message = "The post body cannot be left blank.";
            }
            else if (context.Categories.FirstOrDefault(c => c.CategoryId == post.CategoryId) == null)
            {
                response.Success = false;
                response.Message = "That category is invalid";
            }
            else
            {
                response         = repo.Edit(post);
                response.Message = $"Your changes to \"{post.PostTitle}\" have been saved.";
            }

            return(response);
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        public TagsResponse GetById(int id)
        {
            TagsResponse response = new TagsResponse();

            if (id == 0)
            {
                response.Success = false;
                response.Message = "TagId value empty.";
            }

            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.Tags = context.Tags
                                    .Include("Posts")
                                    .Where(t => t.TagId == id)
                                    .ToList();
                    if (response.Tags.Count == 0)
                    {
                        response.Success = false;
                        response.Message = "Nothing found.";
                        return(response);
                    }
                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
            }

            return(response);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
0
        public TagsResponse GetAll()
        {
            TagsResponse response = new TagsResponse();

            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.Tags = context.Tags
                                    .Include("Posts")
                                    .ToList();
                    if (response.Tags.Count == 0)
                    {
                        response.Success = false;
                        response.Message = "Nothing found.";
                        return(response);
                    }
                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
            }

            return(response);
        }
Пример #10
0
        public PostsResponse GetByApproval(bool isApproved)
        {
            PostsResponse response = new PostsResponse();

            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.Posts = context.Posts
                                     .Include("Tags")
                                     .Where(p => p.IsApproved == isApproved)
                                     .ToList();
                    if (response.Posts.Count == 0)
                    {
                        response.Success = false;
                        response.Message = "Nothing found.";
                        return(response);
                    }
                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
            }

            return(response);
        }
Пример #11
0
        public CategoryResponse GetAll()
        {
            CategoryResponse response = new CategoryResponse();

            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.Categories = context.Categories.ToList();
                    if (response.Categories.Count == 0)
                    {
                        response.Success = false;
                        response.Message = "Nothing found.";
                        return(response);
                    }
                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }

                return(response);
            }
        }
Пример #12
0
        public StaticPgResponse GetById(int pageId)
        {
            StaticPgResponse response = new StaticPgResponse();

            if (pageId > 0 && pageId > 3)
            {
                response.Success = false;
                response.Message = "You messed up the id brah.";
                return(response);
            }
            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.StaticPage = context.StaticPages.Where(sp => sp.StaticPageId == pageId).ToList().First();
                    response.Success    = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
                return(response);
            }
        }
Пример #13
0
        public static PersonalBlogEntities GetConnection()
        {
            if (db == null)
            {
                db = new PersonalBlogEntities();
            }

            return(db);
        }
Пример #14
0
        public PostsResponse Delete(int id)
        {
            var context = new PersonalBlogEntities();

            if (context.Posts.FirstOrDefault(p => p.PostId == id) == null)
            {
                var response = new PostsResponse();
                response.Success = false;
                response.Message = "There is no post in our database that matches the delete criteria.";
                return(response);
            }

            return(repo.Delete(id));
        }
Пример #15
0
        public CategoryResponse Edit(Category category)
        {
            var context = new PersonalBlogEntities();

            if (context.Categories.Contains(category))
            {
                var response = new CategoryResponse();
                response.Success = false;
                response.Message = $"The category {category.CategoryName} is already in the database.";
                return(response);
            }
            else
            {
                return(repo.Edit(category));
            }
        }
Пример #16
0
 public ActionResult Login(LoginVM model)
 {
     if (ModelState.IsValid)
     {
         using (PersonalBlogEntities db = new PersonalBlogEntities())
         {
             var user = db.Users.FirstOrDefault(x => x.Email == model.EMail && x.Password == model.Password && x.Role_ID == 1);
             if (user != null)
             {
                 FormsAuthentication.SetAuthCookie(user.FullName, true);
                 return(RedirectToAction("Index"));
             }
         }
     }
     return(Redirect("~/Home/Index"));
 }
Пример #17
0
        public TagsResponse Edit(Tag tag)
        {
            var context = new PersonalBlogEntities();

            if (context.Tags.Contains(tag))
            {
                var response = new TagsResponse();
                response.Success = false;
                response.Message = $"The tag {tag} already exists in the database.";
                response.Tags.Add(tag);
                return(response);
            }
            else
            {
                return(repo.Edit(tag));
            }
        }
Пример #18
0
        public PostsResponse Add(Post post)
        {
            var context  = new PersonalBlogEntities();
            var response = new PostsResponse();

            if (string.IsNullOrEmpty(post.PostTitle))
            {
                response.Success = false;
                response.Message = "The post title cannot be left blank.";
            }
            else if (post.CreatedDate < DateTime.Today.AddDays(1))
            {
                response.Success = false;
                response.Message = "The post cannot have a creation date before the current date.";
            }
            //else if (!post.IsApproved)
            //{
            //    response.Success = false;
            //    response.Message = "This post has content that violates our blogging policy.";
            //}
            else if (string.IsNullOrEmpty(post.PostBody))
            {
                response.Success = false;
                response.Message = "The post body cannot be left blank.";
            }
            else if (context.Categories.FirstOrDefault(c => c.CategoryId == post.CategoryId) == null)
            {
                response.Success = false;
                response.Message = "That category is invalid";
            }
            else
            {
                TagsRepo tagsRepo = new TagsRepo();

                List <Tag> allTags   = tagsRepo.GetAll().Tags.ToList();
                List <Tag> tagsToAdd = post.Tags.AsEnumerable().Where(t => post.Tags.Any(postTag => postTag.TagName != t.TagName)).ToList();
                foreach (Tag t in tagsToAdd)
                {
                    tagsRepo.Add(t);
                }
                response         = repo.Add(post);
                response.Message = $"The post \"{post.PostTitle}\" has been added to the database.";
            }

            return(response);
        }
Пример #19
0
        public PostsResponse GetByCategory(string category)
        {
            var context          = new PersonalBlogEntities();
            var possibleCategory = context.Categories.FirstOrDefault(c => c.CategoryName.ToLower() == category.ToLower());

            if (possibleCategory == null)
            {
                var response = new PostsResponse();
                response.Success = false;
                response.Message = $"{category} is not a valid category.";
                return(response);
            }
            else
            {
                return(repo.GetByCategory(possibleCategory.CategoryId));
            }
        }
Пример #20
0
        public PostsResponse GetByTitle(string title)
        {
            var context = new PersonalBlogEntities();

            if (string.IsNullOrEmpty(title))
            {
                return(repo.GetByTitle(title));
            }
            if (context.Posts.FirstOrDefault(p => p.PostTitle == title) == null)
            {
                var response = new PostsResponse();

                response.Success = false;
                response.Message = $"There are no posts that have the name of {title}";
                return(response);
            }

            return(repo.GetByTitle(title));
        }
Пример #21
0
        public PostsResponse GetByTag(int tagId)
        {
            var context = new PersonalBlogEntities();

            if (tagId == 0)
            {
                return(repo.GetByTag(tagId));
            }

            if (context.Tags.FirstOrDefault(t => t.TagId == tagId) == null)
            {
                var response = new PostsResponse();
                response.Success = false;
                response.Message = "That is not a valid tag.";
                return(response);
            }

            return(repo.GetByTag(tagId));
        }
Пример #22
0
        public CategoryResponse GetById(int id)
        {
            var context = new PersonalBlogEntities();

            if (id == 0)
            {
                return(repo.GetById(id));
            }

            if (context.Categories.FirstOrDefault(c => c.CategoryId == id) == null)
            {
                var response = new CategoryResponse();
                response.Success = false;
                response.Message = "That category is not valid.";
                return(response);
            }

            return(repo.GetById(id));
        }
Пример #23
0
        public TagsResponse GetById(int id)
        {
            var context = new PersonalBlogEntities();

            if (id == 0)
            {
                return(repo.GetById(id));
            }

            if (context.Tags.FirstOrDefault(t => t.TagId == id) == null)
            {
                var response = new TagsResponse();
                response.Success = false;
                response.Message = "That tag is not valid.";
                return(response);
            }

            return(repo.GetById(id));
        }
Пример #24
0
        public TagsResponse GetByName(string tag)
        {
            var context = new PersonalBlogEntities();

            if (string.IsNullOrEmpty(tag))
            {
                return(repo.GetByName(tag));
            }

            if (context.Tags.FirstOrDefault(p => p.TagName == tag) == null)
            {
                var response = new TagsResponse();
                response.Success = false;
                response.Message = $"There are no posts with the tag {tag}.";
                return(response);
            }
            else
            {
                return(repo.GetByName(tag));
            }
        }
        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);
        }
Пример #26
0
        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);
        }
Пример #27
0
        public PostsResponse GetAll()
        {
            PostsResponse response = new PostsResponse();

            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.Posts = context.Posts
                                     .Include("Tags")
                                     .ToList();
                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
            }

            return(response);
        }
Пример #28
0
        public TagsResponse GetByName(string tag)
        {
            TagsResponse response = new TagsResponse();

            if (string.IsNullOrEmpty(tag))
            {
                response.Success = false;
                response.Message = "Tag value empty.";
                return(response);
            }

            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.Tags = context.Tags
                                    .Include("Posts")
                                    .Where(t => (t.TagName.ToLower()).Contains(tag.ToLower()))
                                    .ToList();
                    if (response.Tags.Count == 0)
                    {
                        response.Success = false;
                        response.Message = "Nothing found.";
                        return(response);
                    }

                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
            }

            return(response);
        }
Пример #29
0
        public PostsResponse GetByTag(int tagId)
        {
            PostsResponse response = new PostsResponse();

            if (tagId == 0)
            {
                response.Success = false;
                response.Message = "TagId value not passed.";
                return(response);
            }

            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.Posts = context.Posts
                                     .Include("Tags")
                                     .Where(p => p.Tags.Any(t => t.TagId == tagId))
                                     .ToList();
                    if (response.Posts.Count == 0)
                    {
                        response.Success = false;
                        response.Message = "Nothing found.";
                        return(response);
                    }
                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
            }

            return(response);
        }
Пример #30
0
        public PostsResponse GetByCategory(int catId)
        {
            PostsResponse response = new PostsResponse();

            if (catId == 0)
            {
                response.Success = false;
                response.Message = "CategoryId was invalid.";
                return(response);
            }

            using (var context = new PersonalBlogEntities())
            {
                try
                {
                    response.Posts = context.Posts
                                     .Include("Tags")
                                     .Where(p => p.CategoryId == catId)
                                     .ToList();
                    if (response.Posts.Count == 0)
                    {
                        response.Success = false;
                        response.Message = "Nothing found.";
                        return(response);
                    }
                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
            }

            return(response);
        }