Beispiel #1
0
        public async Task UpdateBlog(int id, BlogUpdateDto blogUpdateDto)
        {
            Blog   blog     = _context.Blogs.Find(id);
            string filePath = Path.Combine(_env.WebRootPath, "blogImages", blog.Image);

            if (filePath != null)
            {
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
            }


            string     path       = Path.Combine(_env.WebRootPath, "blogImages");
            string     resultPath = Path.Combine(path, blog.Image);
            FileStream fileStream = new FileStream(resultPath, FileMode.Create);
            await blogUpdateDto.Photo.CopyToAsync(fileStream);

            blog.Image = Guid.NewGuid().ToString() + blogUpdateDto.Photo.FileName;
            blog.Id    = blogUpdateDto.Id;
            blog.Title = blogUpdateDto.Title;
            blog.Body  = blogUpdateDto.Body;
            await _context.SaveChangesAsync();
        }
 public IActionResult BlogGuncelle(BlogUpdateDto blogUpdateDto)
 {
     if (ModelState.IsValid)
     {
         _blogService.Update(_mapper.Map <Blog>(blogUpdateDto));
         return(RedirectToAction("Index"));
     }
     return(View());
 }
        public async Task <CoreResult> Update([FromBody] BlogUpdateDto dto)
        {
            CoreResult        result  = new CoreResult();
            BlogUpdateCommand command = new BlogUpdateCommand(dto.Id, dto.Name, dto.Url);
            var res = await _bus.SendCommandAsync(command);

            if (res)
            {
                result.Success("修改成功");
            }
            else
            {
                result.Failed("修改失败");
            }
            return(result);
        }
        public async Task <ActionResult <Blog> > Update(int id, [FromForm] BlogUpdateDto blogUpdateDto)
        {
            if (id != blogUpdateDto.Id)
            {
                return(BadRequest());
            }
            var mapperBlog = _mapper.Map <Blog>(blogUpdateDto);
            var blog       = await _blogRepository.UpdateBlogAsync(mapperBlog, _env.WebRootPath);

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

            return(Ok(blog));
        }
Beispiel #5
0
        /// <summary>
        /// 更新博客
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <CommonResultDto <string> > Update(BlogUpdateDto dto)
        {
            using (await _context.Database.BeginTransactionAsync())
            {
                tbl_blog blog = await _context.tbl_blog.FirstOrDefaultAsync(i => i.Id == dto.Id);

                tbl_blog_content content = await _context.tbl_blog_content.FirstOrDefaultAsync(i => i.Id == blog.ContentId);

                List <tbl_blog_tag_relation> tagRelations = await _context.tbl_blog_tag_relation.Where(i => i.BlogId == dto.Id).ToListAsync();

                List <tbl_blog_tag_relation> insertTags = new List <tbl_blog_tag_relation>();
                foreach (var tagId in dto.TagIdList)
                {
                    insertTags.Add(new tbl_blog_tag_relation
                    {
                        Id     = Guid.NewGuid().ToString(),
                        BlogId = dto.Id,
                        TagId  = tagId
                    });
                }

                blog.Title      = dto.Title;
                blog.PicUrl     = dto.PicUrl;
                blog.CategoryId = dto.CategoryId;
                blog.UpdateAt   = DateTime.Now;
                _context.tbl_blog.Update(blog);

                content.Content = dto.Content;
                _context.tbl_blog_content.Update(content);

                _context.tbl_blog_tag_relation.RemoveRange(tagRelations);
                await _context.tbl_blog_tag_relation.AddRangeAsync(insertTags);

                await _context.SaveChangesAsync();

                _context.Database.CommitTransaction();

                return(new CommonResultDto <string> {
                    Msg = "更新成功", Success = true
                });
            }
        }
        public async Task <ActionResult> Edit(BlogUpdateDto model, IFormFile photo)
        {
            if (ModelState.IsValid)
            {
                if (photo != null)
                {
                    string extension = Path.GetExtension(photo.FileName);
                    string photoName = Guid.NewGuid() + extension;
                    string path      = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/" + photoName);
                    await using var stream = new FileStream(path, FileMode.Create);
                    await photo.CopyToAsync(stream);

                    model.ImagePath = photoName;
                }

                await _blogService.UpdateAsync(_mapper.Map <Blog>(model));

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Beispiel #7
0
        public async Task <ActionResult <string> > UpdateBlog([FromForm] BlogUpdateDto blog)
        {
            var user = await GetCurrentUserAsync(HttpContext.User);

            if (user == null)
            {
                return(Unauthorized(new ApiResponse(401)));
            }

            // check id Blog existing
            var _blog = await _context.Blog.FindAsync(blog.Id);

            if (_blog == null)
            {
                return(NotFound(new ApiResponse(404)));
            }

            //check id category existing, example: Convert string "[1, 2, 3]" to int list
            List <int> _categoriesIds = blog.Categories.Trim('[', ']').Split(',').Select(int.Parse).ToList();

            foreach (var id in _categoriesIds)
            {
                var category = await _context.BlogCategory.FindAsync(id);

                if (category == null)
                {
                    return(NotFound(new ApiResponse(404, "Category Id:" + id + ", Not exist!")));
                }
            }

            // check the Permission
            var permission = await PermissionsManagement(user, _blog);

            if (!permission)
            {
                return(BadRequest(new ApiResponse(400, "current User doesn't has a permation to update this blog")));
            }

            try
            {
                // return Blog class (_blog)
                _mapper.Map(blog, _blog);
                _context.Entry(_blog).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                // update Bog's Categories after delete old categories
                var oldCats = await _context.BlogCategoryList.Where(c => c.BlogId == _blog.Id).ToArrayAsync();

                foreach (var oldCat in oldCats)
                {
                    _context.Remove(oldCat);
                }

                await _context.SaveChangesAsync();

                // Add new Categories
                foreach (var id in _categoriesIds)
                {
                    var newCat = new BlogCategoryList
                    {
                        BlogId         = _blog.Id,
                        BlogCategoryId = id
                    };
                    await _context.BlogCategoryList.AddAsync(newCat);
                }
                await _context.SaveChangesAsync();

                return(Ok($"Update Blog Successfully"));
            }
            catch (Exception ex)
            {
                return(BadRequest(new ApiResponse(400, $"Error: {ex.Message}")));
            }
        }
Beispiel #8
0
        public async Task <ActionResult> Put(int id, [FromForm] BlogUpdateDto blogUpdateDto)
        {
            await _blogRepo.UpdateBlog(id, blogUpdateDto);

            return(StatusCode(StatusCodes.Status200OK));
        }
Beispiel #9
0
 public async Task <CommonResultDto <string> > Update(BlogUpdateDto dto)
 {
     return(await _blogManageService.Update(dto));
 }