Пример #1
0
 public ActionResult Create()
 {
     methodName = MethodBase.GetCurrentMethod().Name;
     Blog blog = new Blog {Blogger = repository.GetFirstBlogger()};
     blog.BloggerID = blog.Blogger.BloggerID;
     BlogViewModel viewModel = new BlogViewModel(blog, new List<Post>(), repository.GetAllBlogs(), repository.GetAllBloggers(), _postsDisplayed);
     WriteLog(string.Format(InfoLogMessage, methodName), (int)LogTypeNames.Info);
     return View(viewModel);
 }
Пример #2
0
        public ActionResult Create(BlogViewModel viewModel)
        {
            methodName = MethodBase.GetCurrentMethod().Name;
            Blog blog = viewModel.Blog;
            blog.Blogger = repository.GetBloggerByID(viewModel.Blog.BloggerID);

            if (ModelState.IsValid)
            {
                try
                {
                    repository.AddNewBlog(blog);
                }
                catch (DbEntityValidationException vex)
                {
                    WriteException(vex);
                    foreach (var evError in vex.EntityValidationErrors)
                    {
                        foreach (var vError in evError.ValidationErrors)
                        {
                            WriteLog(String.Format("Model Error found in {0}: " + vError.ErrorMessage, methodName), (int)LogTypeNames.Error);
                            ModelState.AddModelError(string.Empty, vError.ErrorMessage);
                        }
                    }
                }
                catch (DataException dex)
                {
                    WriteException(dex);
                    ModelState.AddModelError(string.Empty, Constants.Constants.DataExceptionMessage);
                }

                return RedirectToAction("../Blog/List");
            }
            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    WriteLog(String.Format("ModelState Error found in {0}: " + error.ErrorMessage, methodName), (int)LogTypeNames.Error);
                }
            }
            WriteLog(String.Format("Created Blog with id={0} by {1}", blog.BlogID, methodName), (int)LogTypeNames.Info);
            return View(new BlogViewModel(blog, null, repository.GetAllBlogs(), null, _postsDisplayed));
        }
Пример #3
0
        public ActionResult Edit(BlogViewModel viewModel)
        {
            methodName = MethodBase.GetCurrentMethod().Name;
            Blog blog = viewModel.Blog;
            blog.BloggerID = viewModel.Blog.Blogger.BloggerID;
            blog.Blogger = repository.GetBloggerByID(blog.BloggerID);

            if (ModelState.IsValid)
            {
                repository.EditExistingBlog(blog);
                WriteLog(String.Format("Edited Blog with id={0} by {1}", blog.BlogID, methodName), (int)LogTypeNames.Info);
                return RedirectToAction("../Blog/List");
            }
            WriteLog(String.Format("Invalid Model State in {0}", methodName), (int)LogTypeNames.Warn);
            return View(viewModel);
        }
Пример #4
0
        /// <summary>
        /// Creates a ViewModel given a blog id
        /// </summary>
        /// <param name="id">Id of a blog</param>
        /// <returns>ViewModel contains data about Blog, Posts, all blogs, all bloggers and number of posts to display by default</returns>
        public BlogViewModel ViewModelFromBlogID(int p, int blogId, string label)
        {
            methodName = MethodBase.GetCurrentMethod().Name;
            List<Post> posts;
            int totalPosts;
            if (label == string.Empty)
            {
                posts = repository.GetPostPage(p - 1, 10, blogId);
                totalPosts = repository.TotalPosts(blogId);
            }
            else
            {
                posts = repository.GetPostPageForLabel(p - 1, 10, blogId, label);
                totalPosts = repository.TotalPostsForLabel(blogId, label);
            }

            BlogViewModel model = new BlogViewModel
            {
                BlogId = blogId,
                Blog = repository.GetBlogByID(blogId),
                Posts = posts,
                Blogs = repository.GetAllBlogs(),
                Bloggers = repository.GetAllBloggers(),
                TotalPosts = totalPosts,
                //Labels = repository.GetAllLabels()
                Labels = repository.GetLabelsByBlogId(blogId)
            };

            return model;
        }
Пример #5
0
        public ActionResult Details(int id = 0)
        {
            methodName = MethodBase.GetCurrentMethod().Name;
            Blog blog = repository.GetBlogByID(id);
            if (blog == null)
            {
                Logger.WriteEntry(string.Format("Blog with id={0} not found by {1} method.", id, methodName), GetType().FullName, (int)LogTypeNames.Warn);
                return HttpNotFound();
            }
            blog.Blogger = repository.GetBloggerByID(blog.BloggerID);

            BlogViewModel viewModel = new BlogViewModel(blog, null, null, null, _postsDisplayed);
            WriteLog(string.Format(InfoLogMessageWithParam, methodName, "id", id), (int)LogTypeNames.Info);
            return View(viewModel);
        }