169 lines
5.2 KiB
C#
169 lines
5.2 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using VOL.Business.IRepositories;
|
|
using VOL.Business.IServices.Activity;
|
|
using VOL.Business.IServices.Norm;
|
|
using VOL.Core.Extensions.AutofacManager;
|
|
using VOL.Core.ManageUser;
|
|
using VOL.Core.Utilities;
|
|
using VOL.Entity.DomainModels;
|
|
using VOL.Model;
|
|
using VOL.Model.Norm.Request;
|
|
using VOL.Model.Norm.Response;
|
|
using VOL.System.IRepositories;
|
|
|
|
namespace VOL.Business.Services.Activity
|
|
{
|
|
public class G_ArticleService : IG_ArticleService, IDependency
|
|
{
|
|
#region 初始化
|
|
private readonly IMapper _mapper;
|
|
private readonly IG_ActivitiesRepository _articleRepository;
|
|
public G_ArticleService(IMapper mapper,
|
|
IG_ActivitiesRepository articleRepository)
|
|
{
|
|
_mapper = mapper;
|
|
_articleRepository = articleRepository;
|
|
}
|
|
|
|
#endregion
|
|
|
|
public async Task<PageDataDto<ArticleListModel>> GetArticlePageList(ArticlePageListParam paramDto)
|
|
{
|
|
var res = new PageDataDto<ArticleListModel>();
|
|
|
|
var query = from s in _articleRepository.DbContext.Set<G_Article>()
|
|
select new ArticleListModel()
|
|
{
|
|
Id = s.Id,
|
|
Title = s.Title,
|
|
Abstract = s.Abstract,
|
|
CoverImage = s.CoverImage,
|
|
ArticleUrl = s.ArticleUrl,
|
|
UpdateDate = s.UpdateDate
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(paramDto.Title))
|
|
{
|
|
query = query.Where(x => x.Title.Contains(paramDto.Title));
|
|
}
|
|
|
|
res.Total = await query.CountAsync();
|
|
|
|
var list = await query.OrderByDescending(x => x.UpdateDate)
|
|
.Skip((paramDto.PageIndex - 1) * paramDto.PageSize)
|
|
.Take(paramDto.PageSize)
|
|
.ToListAsync();
|
|
|
|
res.Datas = list;
|
|
|
|
return res;
|
|
}
|
|
|
|
public async Task<ArticleDetailsModel> GetArticleDetails(int id)
|
|
{
|
|
var article = await _articleRepository.FindAsyncFirst<G_Article>(x => x.Id == id);
|
|
|
|
if (article == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var result = new ArticleDetailsModel
|
|
{
|
|
Id = article.Id,
|
|
Title = article.Title,
|
|
Abstract = article.Abstract,
|
|
CoverImage = article.CoverImage,
|
|
ArticleUrl = article.ArticleUrl,
|
|
UpdateDate = article.UpdateDate
|
|
};
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增公众号文章
|
|
/// </summary>
|
|
/// <param name="model">文章信息</param>
|
|
/// <returns></returns>
|
|
public async Task<WebResponseContent> AddArticle(ArticleAddParam model)
|
|
{
|
|
WebResponseContent response = new WebResponseContent();
|
|
|
|
// 验证参数
|
|
if (string.IsNullOrEmpty(model.Title))
|
|
{
|
|
return response.Error("文章标题不能为空");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(model.CoverImage))
|
|
{
|
|
return response.Error("封面图片不能为空");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(model.ArticleUrl))
|
|
{
|
|
return response.Error("文章链接不能为空");
|
|
}
|
|
|
|
// 创建文章实体
|
|
var article = new G_Article
|
|
{
|
|
Title = model.Title,
|
|
Abstract = model.Abstract,
|
|
CoverImage = model.CoverImage,
|
|
ArticleUrl = model.ArticleUrl,
|
|
UpdateDate = DateTime.Now
|
|
};
|
|
|
|
try
|
|
{
|
|
// 保存到数据库
|
|
_articleRepository.Add<G_Article>(article);
|
|
await _articleRepository.SaveChangesAsync();
|
|
|
|
return response.OK("新增文章成功");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return response.Error("新增文章失败:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除公众号文章
|
|
/// </summary>
|
|
/// <param name="id">文章ID</param>
|
|
/// <returns></returns>
|
|
public async Task<WebResponseContent> DeleteArticle(int id)
|
|
{
|
|
WebResponseContent response = new WebResponseContent();
|
|
|
|
try
|
|
{
|
|
// 查找文章
|
|
var article = await _articleRepository.FindAsyncFirst<G_Article>(x => x.Id == id);
|
|
if (article == null)
|
|
{
|
|
return response.Error("文章不存在");
|
|
}
|
|
|
|
// 删除文章
|
|
_articleRepository.Delete<G_Article>(article);
|
|
await _articleRepository.SaveChangesAsync();
|
|
|
|
return response.OK("删除文章成功");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return response.Error("删除文章失败:" + ex.Message);
|
|
}
|
|
}
|
|
}
|
|
} |