コード例 #1
0
        public async Task <IActionResult> PostArticle([FromBody] ArticlePostViewModel article)
        {
            string log = $"POST request: article" + Environment.NewLine +
                         $"From IP: {_accessor.HttpContext.Connection.RemoteIpAddress.ToString()}" + Environment.NewLine +
                         $"Title: {article.Title}" + Environment.NewLine;

            if (!ModelState.IsValid)
            {
                log += $"Error: bad model state, {ModelState.Values.First().Errors.First().ErrorMessage}";
                _logger.LogError(log);

                return(BadRequest(ModelState));
            }

            if (_context.Articles.Any(a => a.Title == article.Title.Trim()))
            {
                string msg = "An article with the exact same title already exists";
                log += $"Warning: " + msg;
                _logger.LogWarning(log);

                return(BadRequest(msg));
            }

            var entry = new Article
            {
                Title           = article.Title.Trim(),
                Body            = article.Body.Trim(),
                PreviewText     = article.PreviewText.Trim(),
                PreviewImageUri = article.PreviewImageUri.Trim(),
                CreateAt        = DateTime.Now,
                LastEditAt      = DateTime.Now,
            };

            _context.Articles.Add(entry);
            await _context.SaveChangesAsync();

            log += "Result: article has been saved in the database";
            _logger.LogInformation(log);

            return(CreatedAtAction("PostArticle", new { id = entry.Id }, entry));
        }
コード例 #2
0
        public ApiResult <string> Add([FromBody] ArticlePostViewModel vm)
        {
            // 以接口的形式返回数据
            var res = new ApiResult <string>()
            {
                statusCode = (int)ApiEnum.ParameterError
            };

            if (!string.IsNullOrWhiteSpace(vm.Title) && !string.IsNullOrWhiteSpace(vm.ArticleMenuIDs))
            {
                Article m = new Article();
                m.Title      = vm.Title;
                m.AddDate    = C.DateTimes(vm.AddDate);
                m.ImgUrl     = vm.ImgUrl;
                m.Type       = vm.Type;
                m.FileUrl    = vm.FileUrl;
                m.Keyword    = vm.Keyword;
                m.Sorting    = vm.Sorting;
                m.IsTop      = vm.IsTop;
                m.State      = vm.State;
                m.Synopsis   = vm.Synopsis;
                m.Contents   = vm.Contents;
                m.Source     = vm.Source;
                m.PageView   = vm.PageView;
                m.Author     = vm.Author;
                m.AddDate    = C.DateTimes(m.AddDate);
                m.FileSize   = m.FileSize + "";
                m.FileFormat = m.FileFormat + "";
                try
                {
                    if (vm.ImgUrl != null && !string.IsNullOrWhiteSpace(vm.ImgUrl))
                    {
                        //如有图片上传则保存到本地
                        if (vm.ImgUrl.Contains("base64"))
                        {
                            string path  = "UploadFiles/article/";
                            string path2 = Utility.HostAddress + "article\\";
                            m.ImgUrl = ImagesUtility.Base64StringToFile(vm.ImgUrl, path2, DateTime.Now.ToString("yyyyMMddHHmmssfff"));
                            if (m.ImgUrl != "")
                            {
                                m.ImgUrl = path + m.ImgUrl;
                            }
                        }
                    }
                    //if (vm.FileUrl != null && !string.IsNullOrWhiteSpace(vm.FileUrl))
                    //{
                    //    //如有图片上传则保存到本地
                    //    if (vm.FileUrl.Contains("base64"))
                    //    {
                    //        string path = "UploadFiles/article/";
                    //        string path2 = Utility.HostAddress + "article\\";
                    //        m.FileUrl = ImagesUtility.Base64StringToFile(vm.FileUrl, path2, DateTime.Now.ToString("yyyyMMddHHmmssfff"));
                    //        if (m.FileUrl != "")
                    //            m.FileUrl = path + m.FileUrl;
                    //    }
                    //}
                    m.ID        = db.Insert(m);
                    res.success = m.ID > 0;
                    // 处理文章栏目
                    if (res.success)
                    {
                        string[] array = vm.ArticleMenuIDs.Split(',');
                        if (array.Length > 0)
                        {
                            List <ArticleMenu_Article> list = new List <ArticleMenu_Article>();
                            foreach (string item in array)
                            {
                                list.Add(new ArticleMenu_Article
                                {
                                    ArticleID     = m.ID,
                                    ArticleMenuID = C.Int(item)
                                });
                            }
                            if (articleMenu_ArticleService.Add(list) > 0)
                            {
                                res.msg = "添加成功";
                            }
                            else
                            {
                                res.msg        = "栏目添加失败";
                                res.success    = false;
                                res.statusCode = (int)ApiEnum.Status;
                            }
                        }
                    }
                    else
                    {
                        res.msg        = "文章添加失败";
                        res.success    = false;
                        res.statusCode = (int)ApiEnum.Status;
                    }
                }
                catch (Exception ex)
                {
                    res.statusCode = (int)ApiEnum.Error;
                    res.msg        = ApiEnum.Error.GetEnumText() + ex.Message;
                }
            }
            else
            {
                res.msg = "参数丢失";
            }
            return(res);
        }