예제 #1
0
        public async Task <IActionResult> Post([FromBody] BlogViewModel blogVM)
        {
            var newBlog = Mapper.Map <BlogViewModel, Blog>(blogVM);

            _context.Blog.Add(newBlog);
            await _context.SaveChangesAsync();

            return(StatusCode(StatusCodes.Status200OK));
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("ID,User_publish,User_accept,Create_time,content,blog_id,comment_id")] mb_comment mb_comment, string bid, string createid)
        {
            if (ModelState.IsValid)
            {
                mb_comment.User_publish = User.Identity.Name;
                mb_comment.Create_time  = DateTime.Now;

                _context.Add(mb_comment);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", "Blog", new { id = bid, createid = createid }));
            }
            return(View(mb_comment));
        }
예제 #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var entity = await _dbContext.Authors.FindAsync(Id);

            if (entity == null)
            {
                return(BadRequest());
            }

            entity.FirstName = FirstName;
            entity.LastName  = LastName;
            entity.JobTitle  = JobTitle;
            entity.FullBio   = FullBio;
            entity.ShortBio  = ShortBio;
            entity.Permalink = Permalink;


            if (PhotoFile != null && PhotoFile.Length > 0)
            {
                entity.PhotoUrl = await _uploadManager.SaveAuthorImageAsync(PhotoFile);
            }

            await _dbContext.SaveChangesAsync();

            this.InformUser(FormResult.Updated, $"{entity.FirstName} {entity.LastName}", "author");
            return(RedirectToPage("Index"));
        }
예제 #4
0
        public async Task <IActionResult> OnPostAsync(string redirectTo)
        {
            if (!ModelState.IsValid)
            {
                await prepareModel();

                return(Page());
            }

            var entity = ToEntity();

            entity.CreationDate   = DateTime.UtcNow;
            entity.LastUpdateDate = DateTime.UtcNow;
            entity.ImageUrl       = await _uploadManager.SavePostImageAsync(ImageFile);

            await _dbContext.Posts.AddAsync(entity);

            await _dbContext.SaveChangesAsync();

            this.InformUser(FormResult.Added, Title, "post");
            if (redirectTo == "Edit")
            {
                return(RedirectToPage("Edit", new { id = entity.Id }));
            }
            else
            {
                return(RedirectToPage("Index"));
            }
        }
예제 #5
0
        public async Task <IActionResult> OnPostAsync(string redirectTo)
        {
            if (!ModelState.IsValid)
            {
                await prepareModelSelectLists();

                return(Page());
            }

            var entity = await _dbContext.Posts
                         .Include(x => x.CategoryPosts)
                         .Where(x => x.Id == Id)
                         .SingleOrDefaultAsync();

            if (entity == null)
            {
                return(NotFound());
            }

            //Delete post categories and insert them again
            _dbContext.Categories_Posts.RemoveRange(entity.CategoryPosts);

            //Update entity with entries in View Model.
            UpdateEntity(entity);

            entity.LastUpdateDate = DateTime.UtcNow;

            //Delete old image
            if (ImageFile != null && ImageFile.Length > 0)
            {
                _uploadManager.RemoveFile(entity.ImageUrl);
                entity.ImageUrl = await _uploadManager.SavePostImageAsync(ImageFile);
            }

            await _dbContext.SaveChangesAsync();

            this.InformUser(FormResult.Updated, Title, "posts");

            if (redirectTo == "Edit")
            {
                return(RedirectToPage("Edit", new { id = entity.Id }));
            }
            else
            {
                return(RedirectToPage("Index"));
            }
        }
예제 #6
0
        public async Task AddAsync(TEntity entity)
        {
            //throw new NotImplementedException();
            using var context = new MyBlogContext();
            await context.AddAsync(entity);

            await context.SaveChangesAsync();
        }
예제 #7
0
        public async Task <IActionResult> OnDeleteAsync(int id)
        {
            _dbContext.Categories.Remove(new Data.Models.Category()
            {
                Id = id
            });
            await _dbContext.SaveChangesAsync();

            return(Content("1"));
        }
예제 #8
0
        //订阅事件
        public async Task <IActionResult> Subscribe([Bind("ID,UserA_id,UserB_id")] mb_relationship relationship, string userid, string blogid)
        {
            _context.Add(relationship);
            await _context.SaveChangesAsync();

            //被订阅者粉丝数量加1
            var mb_user = await _context.mb_user.SingleOrDefaultAsync(m => m.User_id == userid);

            mb_user.Fans_num++;

            try
            {
                _context.Update(relationship);
                await _context.SaveChangesAsync();
            }
            catch { }


            return(RedirectToAction("Details", "blog", new { id = blogid, createid = userid }));
        }
예제 #9
0
        public async Task <IActionResult> Create([Bind("ID,User_id,User_name,Fans_num,Blog_num,Comment_num,User_pwd,Avatar")] mb_user mb_user)
        {
            if (ModelState.IsValid)
            {
                mb_user.Avatar = "../../userhead/1.png";
                _context.Add(mb_user);
                await _context.SaveChangesAsync();

                //用户标识
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                identity.AddClaim(new Claim(ClaimTypes.Name, mb_user.User_id));

                identity.AddClaim(new Claim(ClaimTypes.Role, "User"));
                //用户签到
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

                return(RedirectToAction("Index", "Home"));
            }
            return(View(mb_user));
        }
예제 #10
0
        public async Task <IActionResult> OnPostAsync()
        {
            var category = await _dbContext.Categories.FindAsync(Id);

            if (category == null)
            {
                return(BadRequest());
            }

            category.Name = Category.Name;
            await _dbContext.SaveChangesAsync();

            this.InformUser(FormResult.Updated, category.Name, "category");
            return(RedirectToPage("Index"));
        }
예제 #11
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var category = new Category()
            {
                Name = Category.Name
            };
            await _dbContext.Categories.AddAsync(category);

            await _dbContext.SaveChangesAsync();

            this.InformUser(FormResult.Added, category.Name, "category");
            return(RedirectToPage("Index"));
        }
예제 #12
0
 public async Task <int> SaveAsync()
 {
     return(await _context.SaveChangesAsync());
 }
예제 #13
0
        public async Task AddAsync(TEntity entity)
        {
            await _context.AddAsync(entity);

            await _context.SaveChangesAsync();
        }
예제 #14
0
 public async Task UpdateSync(TEntity entity)
 {
     using var context = new MyBlogContext();
     context.Update(entity);
     await context.SaveChangesAsync();
 }