public static IEnumerable <TagViewModel> GetAllTags(this BlogWebDbContext _dbContext) { return(_dbContext.Tags.Select(x => new TagViewModel { Name = x.Name }).ToList()); }
public static IEnumerable <CategoryViewModel> GetAllCategories(this BlogWebDbContext _dbContext) { return(_dbContext.Categories.Select(x => new CategoryViewModel { Name = x.Name, PostsCount = x.Posts.Count() }).ToList()); }
public static PageModel GetPages(this BlogWebDbContext _dbContext, PageModel model) { int postsCount = _dbContext.Posts.Count(); int pagesCount = postsCount % 6 == 0 ? postsCount / 6 : postsCount / 6 + 1; model.PagesCount = pagesCount; return(model); }
public static async Task <int> AddCategoryAsync(this BlogWebDbContext _dbContext, CategoryCreateModel model) { Category category = new Category { Name = model.Name }; _dbContext.Categories.Add(category); return(await _dbContext.SaveChangesAsync()); }
public static IEnumerable <ArchiveViewModel> GetAllArchives(this BlogWebDbContext _dbContext) { var archives = _dbContext.Archives.Select(x => new ArchiveViewModel { Month = x.Month, Year = x.Year, PostsCount = x.Posts.Count() }).ToList(); return(archives); }
public static IEnumerable <MenuViewModel> GetAllMenus(this BlogWebDbContext _dbContext) { var menus = _dbContext.Menus.Where(x => x.IsActive) .Select(x => new MenuViewModel { Name = x.Name, Controller = x.Controller, Action = x.Action, }).ToList(); return(menus); }
public static async Task <int> RemoveCategoryAsync(this BlogWebDbContext _dbContext, int id) { Category category = await _dbContext.Categories.FindAsync(id); if (category != null) { _dbContext.Categories.Remove(category); } return(await _dbContext.SaveChangesAsync()); }
public static async Task <int> RemovePostAsync(this BlogWebDbContext _dbContext, int id) { Post p = await _dbContext.Posts.FindAsync(id); if (p != null) { _dbContext.Posts.Remove(p); } return(await _dbContext.SaveChangesAsync()); }
public static async Task <int> RemoveContactMessageAsync(this BlogWebDbContext _dbContext, int id) { ContactMessage c = await _dbContext.ContactMessages.FindAsync(id); if (c != null) { _dbContext.ContactMessages.Remove(c); } return(await _dbContext.SaveChangesAsync()); }
public static IEnumerable <PopularPostViewModel> GetPopularPosts(this BlogWebDbContext _dbContext) { return(_dbContext.Posts.OrderByDescending(x => x.ViewsCount).Take(3) .Select(x => new PopularPostViewModel { Id = x.Id, Title = x.Title, PublishDate = x.PublishDate, AuthorName = x.Author.User.Username, CommentsCount = x.Comments.Count, ImageData = x.ImageData, ImageMimeType = x.ImageMimeType }).ToList()); }
public static async Task <int> SendMessageAsync(this BlogWebDbContext _dbContext, ContactMessageViewModel model) { ContactMessage message = new ContactMessage { Email = model.Email, SubmmittedDate = DateTime.Now, Message = model.Message, Name = model.Name, Subject = model.Subject }; _dbContext.ContactMessages.Add(message); return(await _dbContext.SaveChangesAsync()); }
public static async Task <PostDetailsViewModel> GetSinglePostDetailsAsync(this BlogWebDbContext _dbContext, int id) { var post = await _dbContext.Posts.FindAsync(id); var postFull = new PostDetailsViewModel { Id = post.Id, ShortDescription = post.ShortDescription, ImageData = post.ImageData, ImageMimeType = post.ImageMimeType, Text = post.Text, Title = post.Title, ViewsCount = post.ViewsCount }; postFull.CategoryName = _dbContext.Categories.Where(x => id == post.CategoryId) .Select(x => new CategoryViewModel { Name = x.Name }).FirstOrDefaultAsync().GetAwaiter().GetResult().Name; postFull.Author = await _dbContext.Authors.Where(x => x.Id == post.AuthorId) .Select(x => new AuthorViewModel { Description = x.Description, ImagePath = x.User.ImagePath, Username = x.User.Username }).FirstOrDefaultAsync(); postFull.Comments = await _dbContext.Comments.Where(x => x.PostId == post.Id). Select(x => new CommentViewModel { Message = x.Message, SubmmittedDate = x.SubmmittedDate, User = x.User, Username = x.Username }).ToListAsync(); postFull.Tags = post.Tags.Select(x => new TagViewModel { Name = x.Name }).ToList(); return(postFull); }
public static async Task <int> AddCommentAsync(this BlogWebDbContext _dbContext, CommentPostModel model) { Comment comment = new Comment { Email = model.Email, SubmmittedDate = DateTime.Now, Message = model.Message, PostId = model.PostId, Username = model.Username, Website = model.Website, UserId = 1 }; _dbContext.Comments.Add(comment); return(await _dbContext.SaveChangesAsync()); }
public static async Task <PostEditModel> GetPostEditModelAsync(this BlogWebDbContext _dbContext, int id) { var post = await _dbContext.Posts.FindAsync(id); if (post != null) { return(new PostEditModel()); } else { var postFull = new PostEditModel { Id = post.Id, ShortDescription = post.ShortDescription, Text = post.Text, Title = post.Title, ImageData = post.ImageData, ImageMimeType = post.ImageMimeType, WrittenDate = post.WrittenDate, AuthorId = post.AuthorId, CategoryId = post.CategoryId, }; postFull.CategoryName = _dbContext.Categories.Where(x => id == post.CategoryId) .Select(x => new CategoryViewModel { Name = x.Name }).FirstOrDefaultAsync().GetAwaiter().GetResult().Name; postFull.Author = await _dbContext.Authors.Where(x => x.Id == post.AuthorId) .Select(x => new AuthorViewModel { Description = x.Description, ImagePath = x.User.ImagePath, Username = x.User.Username }).FirstOrDefaultAsync(); postFull.Categories = await _dbContext.Categories.Select(x => new SelectListItem { Value = x.Name, Text = x.Name }).ToListAsync(); return(postFull); } }
public static async Task <int> SavePostAsync(this BlogWebDbContext _dbContext, PostEditModel model) { var category = await _dbContext.Categories.Where(x => x.Name == model.CategoryName).FirstOrDefaultAsync(); var existingPost = await _dbContext.Posts.FindAsync(model.Id); Post post = new Post { AuthorId = model.AuthorId, CategoryId = model.CategoryId, ImageData = model.ImageData ?? existingPost.ImageData, ImageMimeType = model.ImageMimeType ?? existingPost.ImageMimeType, ShortDescription = model.ShortDescription, Text = model.Text, PublishDate = model.WrittenDate, WrittenDate = model.WrittenDate, Title = model.Title, Category = category, ArchiveId = 1 }; if (existingPost == null) { _dbContext.Posts.Add(post); } else { Post dbEntry = await _dbContext.Posts.FindAsync(model.Id); if (dbEntry != null) { dbEntry.Title = post.Title ?? existingPost.Title; dbEntry.CategoryId = category.Id; dbEntry.ImageData = post.ImageData ?? existingPost.ImageData; dbEntry.ImageMimeType = post.ImageMimeType ?? existingPost.ImageMimeType; dbEntry.ShortDescription = post.ShortDescription ?? existingPost.ShortDescription; dbEntry.Text = post.Text ?? existingPost.Text; dbEntry.PublishDate = post.WrittenDate; dbEntry.WrittenDate = post.WrittenDate; } } return(await _dbContext.SaveChangesAsync()); }
public MenuController() { _dbContext = new BlogWebDbContext(); }
public PostController() { _dbContext = new BlogWebDbContext(); }
public CategoryController() { _dbContext = new BlogWebDbContext(); }
public PaginationController() { _dbContext = new BlogWebDbContext(); }
public DashboardController() { _dbContext = new BlogWebDbContext(); }
public HomeController() { _dbContext = new BlogWebDbContext(); _itemsPerPage = 6; }
public static async Task <IEnumerable <ContactMessageReportViewModel> > GetAllContactMessagesAsync(this BlogWebDbContext _dbContext) { return(await _dbContext.ContactMessages.OrderBy(x => x.SubmmittedDate) .Select(x => new ContactMessageReportViewModel { Id = x.Id, Name = x.Name, Email = x.Email, Subject = x.Subject, Message = x.Message, SubmittedDate = x.SubmmittedDate, WrittenDate = x.WrittenDate }).ToListAsync()); }
public FashionController() { _dbContext = new BlogWebDbContext(); _itemsPerPage = 6; }
public TagController() { _dbContext = new BlogWebDbContext(); }
public static async Task <IEnumerable <PostTravelViewModel> > GetPaginatablePostTravelAsync(this BlogWebDbContext _dbContext, int itemsPerPage, PageModel model) { return(await _dbContext.Posts.OrderBy(x => x.WrittenDate) .Skip((itemsPerPage * model.Number) - itemsPerPage).Take(itemsPerPage) .Select(x => new PostTravelViewModel { Id = x.Id, Title = x.Title, ShortDescription = x.ShortDescription, WrittenDate = x.WrittenDate, CommentsCount = x.Comments.Count, ImageData = x.ImageData, ImageMimeType = x.ImageMimeType, Author = _dbContext.Authors.Where(y => y.Id == x.AuthorId) .Select(y => new AuthorViewModel { Description = y.Description, ImagePath = y.User.ImagePath, Username = y.User.Username }).FirstOrDefault() }).ToListAsync()); }
public static async Task <IEnumerable <PostViewModel> > GetPaginatablePostsAsync(this BlogWebDbContext _dbContext, int itemsPerPage, PageModel model) { return(await _dbContext.Posts.OrderBy(x => x.WrittenDate) .Skip((itemsPerPage * model.Number) - itemsPerPage).Take(itemsPerPage) .Select(x => new PostViewModel { Id = x.Id, Title = x.Title, ShortDescription = x.ShortDescription, WrittenDate = x.WrittenDate, CommentsCount = x.Comments.Count, CategoryName = x.Category.Name, ImageData = x.ImageData, ImageMimeType = x.ImageMimeType }).ToListAsync()); }
public AccountController() { _dbContext = new BlogWebDbContext(); }
public ArchiveController() { _dbContext = new BlogWebDbContext(); }
public ContactController() { _dbContext = new BlogWebDbContext(); }
public static async Task <IEnumerable <CategoryReportViewModel> > GetAllCategoriesAsync(this BlogWebDbContext _dbContext) { return(await _dbContext.Categories .Select(x => new CategoryReportViewModel { Id = x.Id, Name = x.Name, Posts = x.Posts, }).ToListAsync()); }