Ejemplo n.º 1
0
        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());
        }
Ejemplo n.º 2
0
        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());
        }
Ejemplo n.º 3
0
        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());
        }
Ejemplo n.º 4
0
        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());
        }
Ejemplo n.º 5
0
        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());
        }
Ejemplo n.º 6
0
        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());
        }
Ejemplo n.º 7
0
        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());
        }