增加资讯 api
parent
77fc3165b6
commit
4cb7801984
|
|
@ -1,30 +1,52 @@
|
|||
using System;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using New_College.IServices;
|
||||
using New_College.Model;
|
||||
using New_College.Model.Models;
|
||||
using New_College.Model.Request;
|
||||
using New_College.Model.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using New_College.IServices;
|
||||
using New_College.Model;
|
||||
using New_College.Model.Request;
|
||||
using New_College.Model.ViewModels;
|
||||
|
||||
namespace New_College.Api.Controllers.Front
|
||||
{
|
||||
[Route("api/front/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class NewsInfoController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ID_NewsCategoryServices iD_NewsCategory;
|
||||
private readonly ID_NewsInfoServices d_NewsInfo;
|
||||
public NewsInfoController(ID_NewsCategoryServices d_NewsCategoryServices, ID_NewsInfoServices d_NewsInfoServices)
|
||||
public NewsInfoController(ID_NewsCategoryServices d_NewsCategoryServices, ID_NewsInfoServices d_NewsInfoServices, IConfiguration configuration)
|
||||
{
|
||||
this.iD_NewsCategory = d_NewsCategoryServices;
|
||||
this.d_NewsInfo = d_NewsInfoServices;
|
||||
this._configuration = configuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 校验新闻导入Key
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool CheckNewsImportKey()
|
||||
{
|
||||
var configKey = _configuration["newskey"];
|
||||
|
||||
if (string.IsNullOrWhiteSpace(configKey))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Request.Headers.TryGetValue("X-Import-Key", out var requestKey))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return string.Equals(requestKey.ToString(), configKey, StringComparison.Ordinal);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取新闻分类
|
||||
/// </summary>
|
||||
|
|
@ -173,5 +195,270 @@ namespace New_College.Api.Controllers.Front
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 单独写入新闻资讯
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[AllowAnonymous]
|
||||
public async Task<MessageModel<int>> AddNewsInfo([FromBody] NewsInfoAddRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!CheckNewsImportKey())
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "无权限访问,Key校验失败",
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
if (request == null)
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "参数不能为空",
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
if (request.CategoryId <= 0)
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "新闻分类不能为空",
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.Title))
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "新闻标题不能为空",
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.ProvinceCode))
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "省份编号不能为空",
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
request.Title = request.Title.Trim();
|
||||
|
||||
// 标题判重:标题已存在则不重复添加
|
||||
var existNews = await d_NewsInfo.Query(s =>
|
||||
s.IsDelete == false &&
|
||||
s.Title == request.Title
|
||||
);
|
||||
|
||||
if (existNews != null && existNews.Any())
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "新闻标题已存在,未重复添加",
|
||||
success = true,
|
||||
response = 0
|
||||
};
|
||||
}
|
||||
|
||||
var entity = new D_NewsInfo()
|
||||
{
|
||||
CategoryId = request.CategoryId,
|
||||
Author = request.Author,
|
||||
ProvinceCode = request.ProvinceCode,
|
||||
Title = request.Title,
|
||||
CoverImg = request.CoverImg,
|
||||
Summary = request.Summary,
|
||||
Detail = request.Detail,
|
||||
Click = request.Click,
|
||||
IsDelete = false,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
|
||||
var id = await d_NewsInfo.Add(entity);
|
||||
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "success",
|
||||
success = true,
|
||||
response = id
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = ex.Message,
|
||||
success = false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 批量写入新闻资讯
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[AllowAnonymous]
|
||||
public async Task<MessageModel<int>> AddNewsInfoBatch([FromBody] NewsInfoBatchAddRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!CheckNewsImportKey())
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "无权限访问,Key校验失败",
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
if (request == null || request.NewsList == null || !request.NewsList.Any())
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "新闻列表不能为空",
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
var errorList = new List<string>();
|
||||
|
||||
for (int i = 0; i < request.NewsList.Count; i++)
|
||||
{
|
||||
var item = request.NewsList[i];
|
||||
|
||||
if (item.CategoryId <= 0)
|
||||
{
|
||||
errorList.Add($"第{i + 1}条新闻分类不能为空");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(item.Title))
|
||||
{
|
||||
errorList.Add($"第{i + 1}条新闻标题不能为空");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(item.ProvinceCode))
|
||||
{
|
||||
errorList.Add($"第{i + 1}条省份编号不能为空");
|
||||
}
|
||||
}
|
||||
|
||||
if (errorList.Any())
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = string.Join(";", errorList),
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
// 处理标题空格
|
||||
foreach (var item in request.NewsList)
|
||||
{
|
||||
item.Title = item.Title.Trim();
|
||||
}
|
||||
|
||||
// 获取本次提交的标题
|
||||
var requestTitles = request.NewsList
|
||||
.Select(s => s.Title)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
// 查询数据库中已存在的标题
|
||||
var existNewsList = await d_NewsInfo.Query(s =>
|
||||
s.IsDelete == false &&
|
||||
requestTitles.Contains(s.Title)
|
||||
);
|
||||
|
||||
var existTitleList = existNewsList
|
||||
.Select(s => s.Title)
|
||||
.ToList();
|
||||
|
||||
var newsList = new List<D_NewsInfo>();
|
||||
|
||||
// 用于处理本次批量数据内部标题重复
|
||||
var currentAddTitleList = new List<string>();
|
||||
|
||||
foreach (var item in request.NewsList)
|
||||
{
|
||||
// 数据库已存在的标题,跳过
|
||||
if (existTitleList.Contains(item.Title))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 本次批量中已经添加过的标题,跳过
|
||||
if (currentAddTitleList.Contains(item.Title))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
currentAddTitleList.Add(item.Title);
|
||||
|
||||
newsList.Add(new D_NewsInfo()
|
||||
{
|
||||
CategoryId = item.CategoryId,
|
||||
Author = item.Author,
|
||||
ProvinceCode = item.ProvinceCode,
|
||||
Title = item.Title,
|
||||
CoverImg = item.CoverImg,
|
||||
Summary = item.Summary,
|
||||
Detail = item.Detail,
|
||||
Click = item.Click,
|
||||
IsDelete = false,
|
||||
CreateTime = DateTime.Now
|
||||
});
|
||||
}
|
||||
|
||||
if (!newsList.Any())
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = "新闻标题均已存在,未新增数据",
|
||||
success = true,
|
||||
response = 0
|
||||
};
|
||||
}
|
||||
|
||||
var successCount = 0;
|
||||
|
||||
foreach (var news in newsList)
|
||||
{
|
||||
var id = await d_NewsInfo.Add(news);
|
||||
if (id > 0)
|
||||
{
|
||||
successCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = $"success,本次成功新增{successCount}条,跳过重复标题{request.NewsList.Count - successCount}条",
|
||||
success = true,
|
||||
response = successCount
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new MessageModel<int>()
|
||||
{
|
||||
msg = ex.Message,
|
||||
success = false
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3530,6 +3530,56 @@
|
|||
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:New_College.Model.Request.NewsInfoAddRequest">
|
||||
<summary>
|
||||
新增新闻资讯请求
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:New_College.Model.Request.NewsInfoAddRequest.CategoryId">
|
||||
<summary>
|
||||
新闻分类Id
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:New_College.Model.Request.NewsInfoAddRequest.Author">
|
||||
<summary>
|
||||
作者/来源
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:New_College.Model.Request.NewsInfoAddRequest.ProvinceCode">
|
||||
<summary>
|
||||
省份编号
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:New_College.Model.Request.NewsInfoAddRequest.Title">
|
||||
<summary>
|
||||
新闻标题
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:New_College.Model.Request.NewsInfoAddRequest.CoverImg">
|
||||
<summary>
|
||||
封面图
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:New_College.Model.Request.NewsInfoAddRequest.Summary">
|
||||
<summary>
|
||||
摘要
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:New_College.Model.Request.NewsInfoAddRequest.Detail">
|
||||
<summary>
|
||||
新闻详情
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:New_College.Model.Request.NewsInfoAddRequest.Click">
|
||||
<summary>
|
||||
点击量,不传默认0
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:New_College.Model.Request.NewsInfoBatchAddRequest">
|
||||
<summary>
|
||||
批量新增新闻资讯请求
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:New_College.Model.Request.SelectionRequest.LocationCode">
|
||||
<summary>
|
||||
这里
|
||||
|
|
|
|||
|
|
@ -629,6 +629,12 @@
|
|||
<param name="CustomerId"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:New_College.Api.Controllers.Front.NewsInfoController.CheckNewsImportKey">
|
||||
<summary>
|
||||
校验新闻导入Key
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:New_College.Api.Controllers.Front.NewsInfoController.GetNewsCategory">
|
||||
<summary>
|
||||
获取新闻分类
|
||||
|
|
@ -656,6 +662,20 @@
|
|||
<param name="request"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:New_College.Api.Controllers.Front.NewsInfoController.AddNewsInfo(New_College.Model.Request.NewsInfoAddRequest)">
|
||||
<summary>
|
||||
单独写入新闻资讯
|
||||
</summary>
|
||||
<param name="request"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:New_College.Api.Controllers.Front.NewsInfoController.AddNewsInfoBatch(New_College.Model.Request.NewsInfoBatchAddRequest)">
|
||||
<summary>
|
||||
批量写入新闻资讯
|
||||
</summary>
|
||||
<param name="request"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:New_College.Api.Controllers.Front.OrderController.CreateOrder(New_College.Model.ViewModels.UniOrderQuery)">
|
||||
<summary>
|
||||
下订单
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@
|
|||
"Name": "New_College"
|
||||
}
|
||||
},
|
||||
"newskey": "6bf23404dce9c86b0f1",
|
||||
"urls": "http://*:8081", // IIS 部署,注释掉
|
||||
"AllowedHosts": "*",
|
||||
"AppSettings": {
|
||||
|
|
|
|||
|
|
@ -16,4 +16,59 @@ namespace New_College.Model.Request
|
|||
public string provinceCode { get; set; } = "370000";
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 新增新闻资讯请求
|
||||
/// </summary>
|
||||
public class NewsInfoAddRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 新闻分类Id
|
||||
/// </summary>
|
||||
public int CategoryId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 作者/来源
|
||||
/// </summary>
|
||||
public string Author { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 省份编号
|
||||
/// </summary>
|
||||
public string ProvinceCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 新闻标题
|
||||
/// </summary>
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 封面图
|
||||
/// </summary>
|
||||
public string CoverImg { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 摘要
|
||||
/// </summary>
|
||||
public string Summary { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 新闻详情
|
||||
/// </summary>
|
||||
public string Detail { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 点击量,不传默认0
|
||||
/// </summary>
|
||||
public int Click { get; set; } = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量新增新闻资讯请求
|
||||
/// </summary>
|
||||
public class NewsInfoBatchAddRequest
|
||||
{
|
||||
public List<NewsInfoAddRequest> NewsList { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue