forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlogArticleServices.cs
74 lines (62 loc) · 2.52 KB
/
BlogArticleServices.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Mapster;
using MineCosmos.Core.Common;
using MineCosmos.Core.IRepository.Base;
using MineCosmos.Core.IServices;
using MineCosmos.Core.Model.Models;
using MineCosmos.Core.Model.ViewModels;
using MineCosmos.Core.Services.BASE;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MineCosmos.Core.Services
{
public class BlogArticleServices : BaseServices<BlogArticle>, IBlogArticleServices
{
public BlogArticleServices()
{
}
/// <summary>
/// 获取视图博客详情信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<BlogViewModels> GetBlogDetails(int id)
{
// 此处想获取上一条下一条数据,因此将全部数据list出来,有好的想法请提出
//var bloglist = await base.Query(a => a.IsDeleted==false, a => a.bID);
var blogArticle = (await base.GetListAsync(a => a.Id == id && a.Category == "技术博文")).FirstOrDefault();
BlogViewModels models = null;
if (blogArticle != null)
{
models = blogArticle.Adapt<BlogViewModels>();
//要取下一篇和上一篇,以当前id开始,按id排序后top(2),而不用取出所有记录
//这样在记录很多的时候也不会有多大影响
var nextBlogs = await base.Query(a => a.Id >= id && a.IsDeleted == false && a.Category == "技术博文", 2, "bID");
if (nextBlogs.Count == 2)
{
models.next = nextBlogs[1].Title;
models.nextID = nextBlogs[1].Id;
}
var prevBlogs = await base.Query(a => a.Id <= id && a.IsDeleted == false && a.Category == "技术博文", 2, "bID desc");
if (prevBlogs.Count == 2)
{
models.previous = prevBlogs[1].Title;
models.previousID = prevBlogs[1].Id;
}
blogArticle.Traffic += 1;
await base.Update(blogArticle, new List<string> { "btraffic" });
}
return models;
}
/// <summary>
/// 获取博客列表
/// </summary>
/// <returns></returns>
[Caching(AbsoluteExpiration = 10)]
public async Task<List<BlogArticle>> GetBlogs()
{
var bloglist = await base.Query(a => a.Id > 0, a => a.Id);
return bloglist;
}
}
}