Пример #1
0
        public async Task <bool> UpLoadFile(List <IFormFile> files, BaiPost model)
        {
            if (files == null)
            {
                return(true);
            }
            string[] permittedExtensions = { ".png", ".jpeg", ".jpg" };
            string   uniqueFileName      = null;

            foreach (var file in files)
            {
                ImgBaiPost img = new ImgBaiPost();
                var        ext = Path.GetExtension(file.FileName).ToLowerInvariant();

                if (string.IsNullOrEmpty(ext) || !permittedExtensions.Contains(ext))
                {
                    return(false);
                }
                string UploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "img/ThaoLuan");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName;
                string filePath = Path.Combine(UploadsFolder, uniqueFileName);
                await file.CopyToAsync(new FileStream(filePath, FileMode.Create));

                ImgBaiPost imgBaiPost = new ImgBaiPost {
                    IdbaiPost  = model.Id,
                    TenAnh     = file.FileName,
                    KichThuoc  = FileSizeFormatter.FormatSize(file.Length),
                    AnhDinhKem = uniqueFileName
                };
                model.ImgBaiPost.Add(imgBaiPost);
            }
            return(true);
        }
Пример #2
0
        public Dictionary <string, BaiPost> getListFileFromPath(string path)
        {
            Dictionary <string, BaiPost> result = new Dictionary <string, BaiPost>();
            DirectoryInfo d = new DirectoryInfo(path);          //Assuming Test is your Folder

            FileInfo[]    Files          = d.GetFiles("*.txt"); //Getting Text files
            string[]      textFiles      = Directory.GetFiles(path, "*.txt").Select(Path.GetFileName).ToArray();
            List <string> myListFileName = textFiles.ToList();
            List <string> myListContent  = new List <string>();

            foreach (string file in Directory.EnumerateFiles(path))
            {
                //  string contents = ;

                myListContent.Add(File.ReadAllText(file));
                // MessageBox.Show(contents);
                //      BaiPost bp = new BaiPost();
            }
            for (int i = 0; i < myListFileName.Count; i++)
            {
                BaiPost bp = new BaiPost();
                bp.Ma      = myListFileName[i];
                bp.NoiDung = myListContent[i];
                bp.Loai    = true;
                result.Add(myListFileName[i], bp);
            }


            return(result);
        }
Пример #3
0
        public async Task <IActionResult> XoaBaiPost(int id)
        {
            BaiPost baiPost = await _service.GetById(id);

            var isAuthorize = await _authorizationService.AuthorizeAsync(User, baiPost, ThaoLuanOperation.Delete);

            if (!isAuthorize.Succeeded)
            {
                return(Ok(new
                {
                    status = false,
                    mess = MessageResult.AccessDenied
                }));
            }
            if (baiPost != null)
            {
                baiPost.Status = (int)BaseStatus.Disable;
                await _service.Update(baiPost);

                return(Ok(new
                {
                    status = true,
                    mess = MessageResult.UpdateSuccess
                }));
            }
            return(Ok(new
            {
                status = false,
            }));
        }
        public IViewComponentResult Invoke(BaiPost model)
        {
            List <ImgBaiPost> listImg = model.ImgBaiPost.Reverse().ToList();

            ViewBag.listImg = listImg;
            return(View(model));
        }
Пример #5
0
        public async Task <IViewComponentResult> InvokeAsync(BaiPost model)
        {
            ViewBag.NgayPost = model.NgayPost.Value.ToString("HH:mm dd/MM/yyyy");
            var user = await _userManager.FindByNameAsync(model.IdnguoiTao.ToString());

            var claim = await _userManager.GetClaimsAsync(user);

            ViewBag.Claim      = claim[0].Value;
            ViewBag.IdnguoiTao = model.IdnguoiTao.ToString();
            return(View());
        }
Пример #6
0
 public bool DeleteImg(int[] idImg, BaiPost model)
 {
     foreach (int i in idImg)
     {
         ImgBaiPost imgBaiPost = model.ImgBaiPost.SingleOrDefault(x => x.Id == i);
         if (imgBaiPost == null)
         {
             return(false);
         }
         model.ImgBaiPost.Remove(imgBaiPost);
     }
     return(true);
 }
Пример #7
0
        public async Task <IActionResult> Create(BaiPostViewModel vmodel)
        {
            string  tempNgay = DateTime.Now.ToString("dd/MM/yyyy");
            BaiPost entity   = new BaiPost();

            entity.TieuDe           = vmodel.TieuDe;
            entity.NoiDung          = vmodel.NoiDung;
            entity.Loai             = (int)BaiPostType.RiengTu;
            entity.IdnguoiTao       = long.Parse(User.Identity.Name);
            entity.NgayPost         = DateTime.Now;
            entity.IddeTaiNghienCuu = vmodel.IdDeTaiNghienCuu;
            //KenhThaoLuan kenhThaoLuan = await _serviceKenhThaoLuan.GetEntity(x => x.IddeTai == vmodel.IdDeTaiNghienCuu);
            //if (kenhThaoLuan == null)
            //{
            //    return Ok(new
            //    {
            //        status = false,
            //        mess = MessageResult.Fail
            //    });
            //}
            //entity.IdkenhThaoLuan = kenhThaoLuan.Id;
            entity.Status = (int)BaseStatus.Active;
            await _serviceBaiPost.Add(entity);

            if (await UpLoadFile(vmodel.Files, entity))
            {
                await _serviceBaiPost.Update(entity);

                return(Ok(new
                {
                    status = true,
                    data = entity,
                    temp = tempNgay,
                    mess = MessageResult.CreateSuccess
                }));
            }
            else
            {
                return(Ok(new
                {
                    status = false,
                    mess = MessageResult.UpLoadFileFail
                }));
            }
        }
Пример #8
0
        public async Task <IActionResult> Create(BaiPostViewModel vmodel)
        {
            BaiPost entity   = new BaiPost();
            string  tempNgay = DateTime.Now.ToString("dd/MM/yyyy");

            entity.TieuDe = vmodel.TieuDe;
            if (vmodel.Loai == (int)BaiPostType.CongKhai)
            {
                entity.IddeTaiNghienCuu = DefaultValue.IddeTaiNghienCuu;
            }
            else
            {
                entity.IddeTaiNghienCuu = vmodel.IdDeTaiNghienCuu;
            }
            entity.NoiDung    = vmodel.NoiDung;
            entity.Loai       = vmodel.Loai;
            entity.IdnguoiTao = long.Parse(User.Identity.Name);
            entity.NgayPost   = DateTime.Now;
            entity.Status     = (int)BaseStatus.Active;
            await _service.Add(entity);

            if (await UpLoadFile(vmodel.Files, entity))
            {
                await _service.Update(entity);

                return(Ok(new
                {
                    status = true,
                    data = entity,
                    temp = tempNgay,
                    mess = MessageResult.CreateSuccess
                }));
            }
            else
            {
                return(Ok(new
                {
                    status = false,
                    mess = MessageResult.UpLoadFileFail
                }));
            }
        }
Пример #9
0
        public async Task <IViewComponentResult> InvokeAsync(BaiPost model)
        {
            var Comments = model.Comments.ToList();
            List <LoadCommentModel> data = new List <LoadCommentModel>();

            foreach (var item in Comments)
            {
                var user = await _userManager.FindByNameAsync(item.IdnguoiTao.ToString());

                var claim = await _userManager.GetClaimsAsync(user);

                LoadCommentModel loadCommentModel = new LoadCommentModel
                {
                    NoiDungComment = item.NoiDungComment,
                    NgayPost       = item.NgayPost.Value.ToString("HH:mm dd/MM/yyyy"),
                    NguoiComment   = claim[0].Value,
                    AnhDinhKem     = item.AnhDinhKem
                };
                data.Add(loadCommentModel);
            }
            data.Reverse();
            return(await Task.FromResult <IViewComponentResult>(View(data)));
        }
Пример #10
0
        public async Task <IActionResult> NoiDungBaiPost(int id)
        {
            BaiPost baiPost = await _service.GetById(id);

            return(PartialView("_NoiDung", baiPost));
        }