Ejemplo n.º 1
0
        public async Task <ResponseModel> Add([FromBody] News news)
        {
            //生成随机得Id号
            news.Id = Guid.NewGuid().ToString();
            if (ModelState.IsValid)
            {
                //var files = collection.Files;
                //if (files.Count>0)
                //{
                //    string webRootPath = _webhost.WebRootPath;
                //    string relativeDirPath = "\\NewsPicture";
                //    string absolutePath = webRootPath + relativeDirPath;
                //    string[] fileType = new string[] { ".gif", ".jpg", ".jpeg", ".png", ".bmp" };
                //    string extension = Path.GetExtension(files[0].FileName);
                //    if (fileType.Contains(extension))
                //    {
                //        if (Directory.Exists(absolutePath)) Directory.CreateDirectory(absolutePath);
                //        string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
                //        var filePath = absolutePath + "\\" + fileName;
                //        using (var stream = new FileStream(filePath, FileMode.Create))
                //        {
                //            await files[0].CopyToAsync(stream);
                //        }
                //        news.smallpicture = "/NewsPicture/" + fileName;
                //    }
                //    return new JsonResult(new ResponseModel { code = 0, result = "图片格式有误" });
                //}
                //return new JsonResult(new ResponseModel { code = 0, result = "请上传新闻图片" });
                var result = await _newsService.AddAsync(news);

                if (result > 0)
                {
                    return new ResponseModel {
                               code = 200, result = "新闻添加成功"
                    }
                }
                ;
                return(new ResponseModel {
                    code = 0, result = "新闻添加失败"
                });
            }
            else
            {
                string errorMsg = "参数验证失败";
                if (ModelState.ErrorCount > 0)
                {
                    foreach (var key in ModelState.Keys)
                    {
                        errorMsg += ModelState.GetValidationState(key) + "|";
                    }
                    errorMsg.TrimEnd('|');
                }
                return(new ResponseModel
                {
                    code = 400,
                    result = errorMsg
                });
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] News model)
        {
            await _newsService.AddAsync(model);

            var uri = $"{Request.Path}/{model.Id}";

            return(Created(uri, model));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create(News news)
        {
            if (!ModelState.IsValid)
            {
                return(View(news));
            }

            var userId = 1;
            await _newsService.AddAsync(news, userId);

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 4
0
        public async Task <JsonResult> Add([FromBody] News news, IFormCollection collection)
        {
            //生成随机得Id号
            news.Id = Guid.NewGuid().ToString();

            if (ModelState.IsValid)
            {
                if (news.ClassId <= 0 || string.IsNullOrEmpty(news.Title) || string.IsNullOrEmpty(news.TextContent))
                {
                    return(new JsonResult(new ResponseModel {
                        code = 0, result = "参数有误"
                    }));
                }
                var files = collection.Files;
                if (files.Count > 0)
                {
                    string   webRootPath     = _webHost.WebRootPath;
                    string   relativeDirPath = "\\NewsPicture";
                    string   absolutePath    = webRootPath + relativeDirPath;
                    string[] fileType        = new string[] { ".gif", ".jpg", ".jpeg", ".png", ".bmp" };
                    string   extension       = Path.GetExtension(files[0].FileName);
                    if (fileType.Contains(extension))
                    {
                        if (Directory.Exists(absolutePath))
                        {
                            Directory.CreateDirectory(absolutePath);
                        }
                        string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
                        var    filePath = absolutePath + "\\" + fileName;
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await files[0].CopyToAsync(stream);
                        }
                        news.Picture = "/NewsPicture/" + fileName;
                    }
                    return(new JsonResult(new ResponseModel {
                        code = 0, result = "图片格式有误"
                    }));
                }
                //return new JsonResult(new ResponseModel { code = 0, result = "请上传新闻图片" });
                var responseModel = await _newsService.AddAsync(news);

                return(new JsonResult(responseModel));
            }
            else
            {
                return(new JsonResult(new ResponseModel
                {
                    code = 400,
                    result = "参数验证失败"
                }));
            }
        }
Ejemplo n.º 5
0
        public async Task <ActionResult> Post([FromBody] NewsPost value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var entityEntry = await newsServece.AddAsync(value)
                              .ConfigureAwait(true);

            return(CreatedAtAction("Get", new { Id = entityEntry.Id }, entityEntry));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            NewsViewModel.AuthorId = User.GetUserId();

            await newsService.AddAsync(NewsViewModel);

            return(RedirectToPage("../Index"));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Create(NewsCreateViewModel newsFromView) //[Bind("Title,Content,SourseURL")]
        {
            if (ModelState.IsValid)
            {
                NewsDTO newsDTO = new NewsDTO()
                {
                    Id            = Guid.NewGuid(),
                    Title         = newsFromView.Title,
                    Body          = newsFromView.Body,
                    SourseURL     = newsFromView.SourseURL,
                    CreatedOnDate = DateTime.Now,
                    EditedOnDate  = DateTime.Now,
                };

                await _newsService.AddAsync(newsDTO);

                return(RedirectToAction("Index", "Home"));
            }
            return(View(newsFromView));
        }
        public async Task <IActionResult> AddNews([FromForm] NewsAddViewModel news)
        {
            using (var ms = new MemoryStream())
            {
                news.File.CopyTo(ms);
                var fileBytes = ms.ToArray();

                var newsForCreate = new News
                {
                    Name        = news.Name,
                    Description = news.Description,
                    CategoryId  = news.CategoryId,
                    AuthorId    = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value),
                    Image       = fileBytes,
                    ImageType   = news.File.ContentType
                };

                await _newsService.AddAsync(newsForCreate);
            }

            return(Ok());
        }