forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlogArticleServices.cs
96 lines (78 loc) · 2.8 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Blog.Core.Common;
using Blog.Core.IRepository;
using Blog.Core.IServices;
using Blog.Core.Model.Models;
using Blog.Core.Model.ViewModels;
using Blog.Core.Services.BASE;
namespace Blog.Core.Services
{
public class BlogArticleServices : BaseServices<BlogArticle>, IBlogArticleServices
{
IBlogArticleRepository _dal;
IMapper _mapper;
public BlogArticleServices(IBlogArticleRepository dal, IMapper mapper)
{
this._dal = dal;
base.BaseDal = dal;
this._mapper = mapper;
}
/// <summary>
/// 获取视图博客详情信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<BlogViewModels> GetBlogDetails(int id)
{
var bloglist = await base.Query(a => a.bID > 0, a => a.bID);
var blogArticle = (await base.Query(a => a.bID == id)).FirstOrDefault();
BlogViewModels models = null;
if (blogArticle != null)
{
BlogArticle prevblog;
BlogArticle nextblog;
int blogIndex = bloglist.FindIndex(item => item.bID == id);
if (blogIndex >= 0)
{
try
{
prevblog = blogIndex > 0 ? (((BlogArticle)(bloglist[blogIndex - 1]))) : null;
nextblog = blogIndex + 1 < bloglist.Count() ? (BlogArticle)(bloglist[blogIndex + 1]) : null;
// 注意就是这里,mapper
models = _mapper.Map<BlogViewModels>(blogArticle);
if (nextblog != null)
{
models.next = nextblog.btitle;
models.nextID = nextblog.bID;
}
if (prevblog != null)
{
models.previous = prevblog.btitle;
models.previousID = prevblog.bID;
}
}
catch (Exception) { }
}
blogArticle.btraffic += 1;
await base.Update(blogArticle, new List<string> { "btraffic" });
}
return models;
}
/// <summary>
/// 获取博客列表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Caching(AbsoluteExpiration = 10)]
public async Task<List<BlogArticle>> GetBlogs()
{
var bloglist = await base.Query(a => a.bID > 0, a => a.bID);
return bloglist;
}
}
}