Ejemplo n.º 1
0
        public async Task <ActionResult> Create(BlogAddDto model, IFormFile photo)
        {
            if (ModelState.IsValid)
            {
                var allAdminUsers = _appUserService.GetAdminUsers();
                var arif          = allAdminUsers.FirstOrDefault(x => x.UserName == "*****@*****.**");

                if (arif != null)
                {
                    model.AppUserId = arif.Id;
                }

                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.AddAsync(_mapper.Map <Blog>(model));

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(BlogAddDto blogAddDto, IFormFile imgFile, [FromServices] IWebHostEnvironment webHostEnvironment)
        {
            if (ModelState.IsValid)
            {
                if (imgFile != null)
                {
                    string imgName = await ImageUploadHelper.ImageUploadAsync(webHostEnvironment, imgFile, "\\img\\blog");

                    blogAddDto.ImageUrl = imgName;
                }
                else
                {
                    blogAddDto.ImageUrl = "no-photo.png";
                }

                await _blogService.InsertAsync(new Blog
                {
                    AppUserId     = 1,
                    SubCategoryId = blogAddDto.SubCategoryId,
                    ImageUrl      = blogAddDto.ImageUrl,
                    Text          = blogAddDto.Text,
                    Title         = blogAddDto.Title,
                    CreatedDate   = DateTime.Now
                });

                return(RedirectToAction("Index"));
            }

            blogAddDto.CategoryList = new SelectList(await _categoryService.GetListAsync(), "Id", "Name");
            return(View(blogAddDto));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create()
        {
            TempData["active"] = "blog";
            BlogAddDto blogAddDto = new BlogAddDto();

            blogAddDto.CategoryList = new SelectList(await _categoryService.GetListAsync(), "Id", "Name", -1);
            return(View(blogAddDto));
        }
 public IActionResult YeniBlog(BlogAddDto blogAddDto)
 {
     if (ModelState.IsValid)
     {
         _blogService.Add(_mapper.Map <Blog>(blogAddDto));
         return(RedirectToAction("Index"));
     }
     return(View());
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 新增博客
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <CommonResultDto <string> > Post(BlogAddDto dto)
        {
            using (await _context.Database.BeginTransactionAsync())
            {
                System.ComponentModel.NullableConverter nullableDateTime = new System.ComponentModel.NullableConverter(typeof(DateTime?));
                string           blogContentId = Guid.NewGuid().ToString();
                string           blogId        = Guid.NewGuid().ToString();
                tbl_blog_content tbl_content   = new tbl_blog_content
                {
                    Id      = blogContentId,
                    Content = dto.Content
                };

                tbl_blog tbl_blog = new tbl_blog
                {
                    Id         = blogId,
                    Title      = dto.Title,
                    ContentId  = blogContentId,
                    Remark     = dto.Remark,
                    CategoryId = dto.CategoryId,
                    PicUrl     = dto.PicUrl,
                    ViewTimes  = 0,
                    LikeTimes  = 0,
                    State      = dto.State == "1" ? '1' : '2', //状态(枚举类型 1未发布 2已发布)
                    CreateAt   = DateTime.Now,
                    PublishAt  = dto.State == "2" ? DateTime.Now : (DateTime?)nullableDateTime.ConvertFromString(null),
                    DeleteAt   = null
                };

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

                await _context.tbl_blog.AddAsync(tbl_blog);

                await _context.tbl_blog_content.AddAsync(tbl_content);

                await _context.tbl_blog_tag_relation.AddRangeAsync(relationList);

                await _context.SaveChangesAsync();

                _context.Database.CommitTransaction();

                return(new CommonResultDto <string> {
                    Msg = "新增成功", Success = true
                });
            }
        }
Ejemplo n.º 6
0
        public async Task <CoreResult> Add([FromBody] BlogAddDto dto)
        {
            CoreResult     result  = new CoreResult();
            BlogAddCommand command = new BlogAddCommand(dto.Name, dto.Url);
            var            res     = await _bus.SendCommandAsync(command);

            if (res)
            {
                result.Success("添加成功");
            }
            else
            {
                result.Failed("添加失败");
            }
            return(result);
        }
Ejemplo n.º 7
0
        public IActionResult Add([FromBody] BlogAddDto blogAdd)
        {
            int userId = HttpContext.Session.GetInt32("UserId").Value;

            Blog.Data.Models.Blog blog = new Blog.Data.Models.Blog()
            {
                UserId     = userId,
                Title      = blogAdd.Title,
                Content    = blogAdd.Content,
                CreateDate = DateTime.UtcNow,
                Hit        = 0,
                Deleted    = false,
                CategoryId = blogAdd.CategoryId
            };

            _blogContext.Blogs.Add(blog);
            _blogContext.SaveChanges();
            return(Ok(blog));
        }
Ejemplo n.º 8
0
        public async Task <ActionResult <Blogs> > CreateBlog(BlogAddDto blog)
        {
            //创建html文件
            //1.设置文件名
            var fileName = DateTimeHelper.GetTimestamp(DateTime.Now);
            //创建文件
            var htmlHead    = System.IO.File.ReadAllText("../Wuther.Api/Resource/bloghead.txt");
            var htmlfoot    = System.IO.File.ReadAllText("../Wuther.Api/Resource/blogfoot.txt");
            var htmlContent = $"{htmlHead}{blog.Content}{htmlfoot}";

            var buffer = Encoding.UTF8.GetBytes(htmlContent);

            FileHelper.CreateFile($"D:\\Project4\\wuther.ui.vue3\\public\\admin\\{fileName}.html", buffer);

            var entity = _mapper.Map <Blogs>(blog);

            entity.CreateTime = DateTime.Now;
            entity.Path       = $"\\admin\\{fileName}.html";
            var contentStr = StringHelper.RemoveBlogContentSpecialChar(blog.Content);

            if (contentStr.Length > 200)
            {
                entity.Abstract = contentStr.Substring(0, 200);
            }
            else
            {
                entity.Abstract = contentStr.Substring(0, contentStr.Length);
            }
            var blogAdd = await _blogsRepository.InsertAsync(entity);

            var returnDto  = _mapper.Map <BlogDto>(blogAdd);
            var links      = CreateLinksForBlogs(returnDto.Id, null);
            var linkedDict = returnDto.ShapeData(null) as IDictionary <string, object>;

            linkedDict.Add("links", links);
            return(CreatedAtRoute(nameof(GetBlog), new { menuId = linkedDict["Id"] }, linkedDict));
        }
Ejemplo n.º 9
0
 public async Task <CommonResultDto <string> > Post(BlogAddDto dto)
 {
     return(await _blogManageService.Post(dto));
 }