├── dll
├── MongoDB.Bson.dll
├── MongoDB.Driver.dll
└── MongoDB.Driver.Core.dll
├── NewsAPI
├── Handler.ashx
├── Properties
│ ├── PublishProfiles
│ │ ├── 2.pubxml
│ │ └── 2.pubxml.user
│ └── AssemblyInfo.cs
├── Web.Debug.config
├── Web.Release.config
├── NewsAPI.csproj.user
├── Web.config
├── Handler.ashx.cs
└── NewsAPI.csproj
├── NewsAPI.MongoDB
├── MongoDBClient.cs
├── User.cs
├── Properties
│ └── AssemblyInfo.cs
├── NewsReturn.cs
├── NewsAPI.MongoDB.csproj
└── BLL.cs
├── README.md
└── NewsAPI.sln
/dll/MongoDB.Bson.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ParryQiu/IMOOC-React-NewsAPI/HEAD/dll/MongoDB.Bson.dll
--------------------------------------------------------------------------------
/dll/MongoDB.Driver.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ParryQiu/IMOOC-React-NewsAPI/HEAD/dll/MongoDB.Driver.dll
--------------------------------------------------------------------------------
/NewsAPI/Handler.ashx:
--------------------------------------------------------------------------------
1 | <%@ WebHandler Language="C#" CodeBehind="Handler.ashx.cs" Class="NewsAPI.Handler" %>
2 |
--------------------------------------------------------------------------------
/dll/MongoDB.Driver.Core.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ParryQiu/IMOOC-React-NewsAPI/HEAD/dll/MongoDB.Driver.Core.dll
--------------------------------------------------------------------------------
/NewsAPI.MongoDB/MongoDBClient.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Driver;
2 |
3 | namespace NewsAPI.MongoDB
4 | {
5 | public static class MongoDBClient
6 | {
7 | public static IMongoDatabase GetMongoClient()
8 | {
9 | var clientPath = Common.Helper.ConfigurationManagerHelper.GetAppSettingValue("mongodb", "mongodb://127.0.0.1:27001");
10 | var client = new MongoClient(clientPath);
11 | return client.GetDatabase("reactnews");
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/NewsAPI/Properties/PublishProfiles/2.pubxml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 | FileSystem
9 | Release
10 | Any CPU
11 |
12 | True
13 | False
14 | C:\Users\Parry\Desktop\2
15 | False
16 |
17 |
--------------------------------------------------------------------------------
/NewsAPI.MongoDB/User.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using MongoDB.Bson;
7 |
8 | namespace NewsAPI.MongoDB
9 | {
10 | public class User
11 | {
12 | public ObjectId _id { get; set; }
13 |
14 | public int UserId { get; set; }
15 |
16 | public string NickUserName { get; set; }
17 |
18 | public string UserFace { get; set; }
19 |
20 | public string UserPassword { get; set; }
21 |
22 | }
23 |
24 | public class UserCollections
25 | {
26 | public ObjectId Id { get; set; }
27 |
28 | public int UserId { get; set; }
29 |
30 | public string uniquekey { get; set; }
31 |
32 | public string Title { get; set; }
33 |
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # IMOOC-React-NewsAPI
2 | 慕课网 ReactJS 课程项目新闻后台测试级别 API
3 |
4 | # API 请求地址
5 | * [获取新闻列表](http://newsapi.gugujiankong.com/Handler.ashx?action=getnews&type=top&count=10)
6 | * [获取新闻详情](http://newsapi.gugujiankong.com/Handler.ashx?action=getnewsitem&uniquekey=161022191707874)
7 | * [获取文章评论](http://newsapi.gugujiankong.com/Handler.ashx?action=getcomments&uniquekey=123)
8 | * [新闻添加评论](http://newsapi.gugujiankong.com/Handler.ashx?action=comment&userid=1&uniquekey=123&commnet=content)
9 | * [收藏新闻](http://newsapi.gugujiankong.com/Handler.ashx?action=uc&userid=1&uniquekey=123)
10 | * [注册登录接口](http://newsapi.gugujiankong.com/Handler.ashx?action=register&username=userName&password=password&r_userName=r_userName&r_password=r_password&r_confirmPassword=r_confirmPassword)
11 | * [获取用户收藏](http://newsapi.gugujiankong.com/Handler.ashx?action=getuc&userid=1)
12 | * [获取用户发出的评论](http://newsapi.gugujiankong.com/Handler.ashx?action=getusercomments&userid=1)
13 |
--------------------------------------------------------------------------------
/NewsAPI/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // 有关程序集的常规信息通过下列特性集
6 | // 控制。更改这些特性值可修改
7 | // 与程序集关联的信息。
8 | [assembly: AssemblyTitle("NewsAPI")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("NewsAPI")]
13 | [assembly: AssemblyCopyright("版权所有(C) 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // 将 ComVisible 设置为 false 会使此程序集中的类型
18 | // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的某个类型,
19 | // 请针对该类型将 ComVisible 特性设置为 true。
20 | [assembly: ComVisible(false)]
21 |
22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23 | [assembly: Guid("660997e7-edc6-433f-9e5b-1ce33bc50166")]
24 |
25 | // 程序集的版本信息由下列四个值组成:
26 | //
27 | // 主版本
28 | // 次版本
29 | // 内部版本号
30 | // 修订号
31 | //
32 | // 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,
33 | // 方法是按如下所示使用“*”:
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.0.0.0")]
36 |
--------------------------------------------------------------------------------
/NewsAPI.MongoDB/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // 有关程序集的一般信息由以下
6 | // 控制。更改这些特性值可修改
7 | // 与程序集关联的信息。
8 | [assembly: AssemblyTitle("NewsAPI.MongoDB")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("NewsAPI.MongoDB")]
13 | [assembly: AssemblyCopyright("Copyright © 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | //将 ComVisible 设置为 false 将使此程序集中的类型
18 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
19 | //请将此类型的 ComVisible 特性设置为 true。
20 | [assembly: ComVisible(false)]
21 |
22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23 | [assembly: Guid("9a86b2c0-d251-41de-9a7f-aa7473b77180")]
24 |
25 | // 程序集的版本信息由下列四个值组成:
26 | //
27 | // 主版本
28 | // 次版本
29 | // 生成号
30 | // 修订号
31 | //
32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
33 | // 方法是按如下所示使用“*”: :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/NewsAPI/Web.Debug.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
17 |
18 |
30 |
31 |
--------------------------------------------------------------------------------
/NewsAPI/Web.Release.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
17 |
18 |
19 |
31 |
32 |
--------------------------------------------------------------------------------
/NewsAPI/NewsAPI.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 2
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | CurrentPage
13 | True
14 | False
15 | False
16 | False
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | True
26 | True
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/NewsAPI.MongoDB/NewsReturn.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using MongoDB.Bson;
3 |
4 | namespace NewsAPI.MongoDB
5 | {
6 | public class NewsBase
7 | {
8 | public string reason { get; set; }
9 | public NewsResut result { get; set; }
10 | public string error_code { get; set; }
11 | }
12 |
13 | public class NewsResut
14 | {
15 | public string stat { get; set; }
16 | public List data { get; set; }
17 | }
18 |
19 | public class NewsData
20 | {
21 | public ObjectId Id { get; set; }
22 | public string title { get; set; }
23 | public string date { get; set; }
24 | public string author_name { get; set; }
25 | public string thumbnail_pic_s { get; set; }
26 | public string thumbnail_pic_s02 { get; set; }
27 | public string thumbnail_pic_s03 { get; set; }
28 | public string url { get; set; }
29 | public string uniquekey { get; set; }
30 | public string type { get; set; }
31 | public string realtype { get; set; }
32 | }
33 |
34 | public class NewsItem : NewsData
35 | {
36 | public string pagecontent { get; set; }
37 | }
38 |
39 | public class NewsComments
40 | {
41 | public ObjectId Id { get; set; }
42 | public string uniquekey { get; set; }
43 | public int UserId { get; set; }
44 | public string UserName { get; set; }
45 | public string UserFace { get; set; }
46 | public string Comments { get; set; }
47 | public string datetime { get; set; }
48 | }
49 | }
--------------------------------------------------------------------------------
/NewsAPI/Web.config:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
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 |
--------------------------------------------------------------------------------
/NewsAPI.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.24720.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewsAPI", "NewsAPI\NewsAPI.csproj", "{660997E7-EDC6-433F-9E5B-1CE33BC50166}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewsAPI.MongoDB", "NewsAPI.MongoDB\NewsAPI.MongoDB.csproj", "{9A86B2C0-D251-41DE-9A7F-AA7473B77180}"
9 | EndProject
10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.Helper", "Common.Helper\Common.Helper.csproj", "{F96545A9-0FAC-45AB-8362-0FF5B1D52E63}"
11 | EndProject
12 | Global
13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
14 | Debug|Any CPU = Debug|Any CPU
15 | Release|Any CPU = Release|Any CPU
16 | EndGlobalSection
17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
18 | {660997E7-EDC6-433F-9E5B-1CE33BC50166}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {660997E7-EDC6-433F-9E5B-1CE33BC50166}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {660997E7-EDC6-433F-9E5B-1CE33BC50166}.Release|Any CPU.ActiveCfg = Release|Any CPU
21 | {660997E7-EDC6-433F-9E5B-1CE33BC50166}.Release|Any CPU.Build.0 = Release|Any CPU
22 | {9A86B2C0-D251-41DE-9A7F-AA7473B77180}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23 | {9A86B2C0-D251-41DE-9A7F-AA7473B77180}.Debug|Any CPU.Build.0 = Debug|Any CPU
24 | {9A86B2C0-D251-41DE-9A7F-AA7473B77180}.Release|Any CPU.ActiveCfg = Release|Any CPU
25 | {9A86B2C0-D251-41DE-9A7F-AA7473B77180}.Release|Any CPU.Build.0 = Release|Any CPU
26 | {F96545A9-0FAC-45AB-8362-0FF5B1D52E63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 | {F96545A9-0FAC-45AB-8362-0FF5B1D52E63}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 | {F96545A9-0FAC-45AB-8362-0FF5B1D52E63}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 | {F96545A9-0FAC-45AB-8362-0FF5B1D52E63}.Release|Any CPU.Build.0 = Release|Any CPU
30 | EndGlobalSection
31 | GlobalSection(SolutionProperties) = preSolution
32 | HideSolutionNode = FALSE
33 | EndGlobalSection
34 | EndGlobal
35 |
--------------------------------------------------------------------------------
/NewsAPI.MongoDB/NewsAPI.MongoDB.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {9A86B2C0-D251-41DE-9A7F-AA7473B77180}
8 | Library
9 | Properties
10 | NewsAPI.MongoDB
11 | NewsAPI.MongoDB
12 | v4.5
13 | 512
14 |
15 |
16 |
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 | false
25 |
26 |
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 | false
34 |
35 |
36 |
37 | ..\dll\MongoDB.Bson.dll
38 |
39 |
40 | ..\dll\MongoDB.Driver.dll
41 |
42 |
43 | ..\dll\MongoDB.Driver.Core.dll
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | {f96545a9-0fac-45ab-8362-0ff5b1d52e63}
63 | Common.Helper
64 |
65 |
66 |
67 |
74 |
--------------------------------------------------------------------------------
/NewsAPI/Properties/PublishProfiles/2.pubxml.user:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 | <_PublishTargetUrl>C:\Users\Parry\Desktop\2
10 |
11 |
12 |
13 | 09/10/2016 08:26:56
14 |
15 |
16 | 09/10/2016 08:26:56
17 |
18 |
19 | 06/15/2015 11:27:20
20 |
21 |
22 | 07/30/2016 14:49:16
23 |
24 |
25 | 09/10/2016 08:26:51
26 |
27 |
28 | 09/10/2016 08:26:52
29 |
30 |
31 | 09/10/2016 08:26:52
32 |
33 |
34 | 09/10/2016 08:26:53
35 |
36 |
37 | 09/10/2016 08:26:55
38 |
39 |
40 | 09/10/2016 08:26:53
41 |
42 |
43 | 09/10/2016 08:26:50
44 |
45 |
46 | 09/10/2016 08:26:50
47 |
48 |
49 | 09/10/2016 08:26:47
50 |
51 |
52 | 09/10/2016 08:26:47
53 |
54 |
55 | 09/10/2016 08:26:47
56 |
57 |
58 | 03/31/2016 14:31:42
59 |
60 |
61 | 03/31/2016 14:31:43
62 |
63 |
64 | 03/31/2016 14:31:45
65 |
66 |
67 | 09/20/2016 01:05:57
68 |
69 |
70 | 09/20/2016 01:05:56
71 |
72 |
73 | 10/26/2015 08:45:33
74 |
75 |
76 | 10/26/2015 08:45:33
77 |
78 |
79 | 09/10/2016 08:26:45
80 |
81 |
82 | 09/10/2016 08:26:45
83 |
84 |
85 | 09/10/2016 08:26:45
86 |
87 |
88 | 09/10/2016 08:30:53
89 |
90 |
91 | 09/10/2016 08:26:58
92 |
93 |
94 | 09/15/2016 10:54:31
95 |
96 |
97 |
--------------------------------------------------------------------------------
/NewsAPI/Handler.ashx.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Net.NetworkInformation;
5 | using System.Text;
6 | using System.Web;
7 | using System.Web.Script.Serialization;
8 | using Common.Helper;
9 | using NewsAPI.MongoDB;
10 |
11 | namespace NewsAPI
12 | {
13 | ///
14 | /// Handler 的摘要说明
15 | ///
16 | public class Handler : IHttpHandler
17 | {
18 | public void ProcessRequest(HttpContext context)
19 | {
20 | var javaScriptSerializer = new JavaScriptSerializer();
21 | var result = string.Empty;
22 |
23 | switch (context.Request.QueryString["action"])
24 | {
25 | case "getnews":
26 | var type = context.Request.QueryString["type"];
27 | //var url = string.Format("http://v.juhe.cn/toutiao/index?type={0}&key=755e6a8cb5eb455e702c60fedc0529bf", type);
28 | int count = int.Parse(context.Request.QueryString["count"]);
29 | //因为接口到期的问题,不再从接口抓取新的新闻入库,直接返回已有的新闻数据供测试使用即可。
30 | //var content = NetworkHelper.GetHtmlFromGet(url, Encoding.UTF8);
31 | //var newsBase = javaScriptSerializer.Deserialize(content);
32 | //循环所有的数据,判断是否已经在数据库中存在了,如果有那么跳过,如果没有那么就存储起来。
33 | //NewsAPI.MongoDB.BLL.Save(newsBase.result.data);
34 |
35 | //然后从数据库中取数据即可。
36 | List listNewsData = NewsAPI.MongoDB.BLL.GetNews(count, type);
37 | result = javaScriptSerializer.Serialize(listNewsData);
38 | break;
39 |
40 | case "getnewsitem":
41 | var articleId = context.Request.QueryString["uniquekey"];
42 | var newsItem = BLL.GetNewsItem(articleId);
43 | result = javaScriptSerializer.Serialize(newsItem);
44 | break;
45 |
46 | case "login":
47 | var username = context.Request.QueryString["username"];
48 | var password = context.Request.QueryString["password"];
49 | var loginUser = BLL.Login(username, password);
50 | result = javaScriptSerializer.Serialize(loginUser);
51 | break;
52 |
53 | case "register":
54 | var r_userName = context.Request.QueryString["r_userName"];
55 | var r_password = context.Request.QueryString["r_password"];
56 | var user = new User();
57 | user.UserPassword = r_password;
58 | user.NickUserName = r_userName;
59 | BLL.Register(user);
60 | result = javaScriptSerializer.Serialize(true);
61 | break;
62 |
63 | case "getcomments":
64 | var uniquekeygetcomments = context.Request.QueryString["uniquekey"];
65 | List collectionComments = BLL.GetComments(uniquekeygetcomments);
66 | result = javaScriptSerializer.Serialize(collectionComments);
67 | break;
68 |
69 | case "getusercomments":
70 | var useridcomment2 = context.Request.QueryString["userid"];
71 | List collectionComments2 = BLL.GetComments(int.Parse(useridcomment2));
72 | result = javaScriptSerializer.Serialize(collectionComments2);
73 | break;
74 |
75 | case "comment":
76 | var useridcomment = context.Request.QueryString["userid"];
77 | var uniquekeycomment = context.Request.QueryString["uniquekey"];
78 | var commnet = context.Request.QueryString["commnet"];
79 | BLL.SaveComment(useridcomment, uniquekeycomment, commnet);
80 | result = javaScriptSerializer.Serialize(true);
81 | break;
82 |
83 | case "getuc":
84 | var useridgetuc = context.Request.QueryString["userid"];
85 | List collection = BLL.GetUserCollection(useridgetuc);
86 | result = javaScriptSerializer.Serialize(collection);
87 | break;
88 |
89 | case "uc":
90 | var useriduc = context.Request.QueryString["userid"];
91 | var uniquekeyuc = context.Request.QueryString["uniquekey"];
92 | BLL.SaveUserCollection(useriduc, uniquekeyuc);
93 | result = javaScriptSerializer.Serialize(true);
94 | break;
95 | }
96 |
97 | context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
98 | context.Response.ContentType = "text/plain";
99 | context.Response.Write(result);
100 | }
101 |
102 | public bool IsReusable
103 | {
104 | get
105 | {
106 | return false;
107 | }
108 | }
109 | }
110 | }
--------------------------------------------------------------------------------
/NewsAPI/NewsAPI.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 |
8 |
9 | 2.0
10 | {660997E7-EDC6-433F-9E5B-1CE33BC50166}
11 | {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}
12 | Library
13 | Properties
14 | NewsAPI
15 | NewsAPI
16 | v4.5
17 | true
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 | true
27 | full
28 | false
29 | bin\
30 | DEBUG;TRACE
31 | prompt
32 | 4
33 | false
34 |
35 |
36 | pdbonly
37 | true
38 | bin\
39 | TRACE
40 | prompt
41 | 4
42 | false
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | Designer
66 |
67 |
68 |
69 |
70 | Handler.ashx
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | Web.config
79 |
80 |
81 | Web.config
82 |
83 |
84 |
85 |
86 | {f96545a9-0fac-45ab-8362-0ff5b1d52e63}
87 | Common.Helper
88 |
89 |
90 | {9a86b2c0-d251-41de-9a7f-aa7473b77180}
91 | NewsAPI.MongoDB
92 |
93 |
94 |
95 | 10.0
96 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 | True
106 | True
107 | 35950
108 | /
109 | http://localhost:35950/
110 | False
111 | False
112 |
113 |
114 | False
115 |
116 |
117 |
118 |
119 |
126 |
--------------------------------------------------------------------------------
/NewsAPI.MongoDB/BLL.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Text.RegularExpressions;
6 | using System.Threading.Tasks;
7 | using MongoDB.Driver;
8 |
9 | namespace NewsAPI.MongoDB
10 | {
11 | public class BLL
12 | {
13 | public static void Save(List newsData)
14 | {
15 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("NewsData");
16 | foreach (NewsData data in newsData)
17 | {
18 | if (dbWebSites.Count(x => x.uniquekey == data.uniquekey) == 0)
19 | {
20 | dbWebSites.InsertOne(data);
21 | }
22 | }
23 | }
24 |
25 | public static void Register(User user)
26 | {
27 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("User");
28 | var collection = dbWebSites.Find(Builders.Filter.Empty).Sort(Builders.Sort.Descending("UserId")).Limit(1).ToList();
29 | var userid = 1;
30 | if (collection.Count > 0)
31 | {
32 | userid = collection[0].UserId + 1;
33 | }
34 | user.UserId = userid;
35 | dbWebSites.InsertOne(user);
36 | }
37 |
38 | public static User Login(string username, string password)
39 | {
40 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("User");
41 | var collection = dbWebSites.Find(x => x.NickUserName == username && x.UserPassword == password).ToList();
42 | if (collection.Count > 0)
43 | {
44 | return collection[0];
45 | }
46 | else
47 | {
48 | return null;
49 | }
50 | }
51 |
52 | public static User GetUser(string userid)
53 | {
54 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("User");
55 | var collection = dbWebSites.Find(x => x.UserId == int.Parse(userid)).ToList();
56 | if (collection.Count > 0)
57 | {
58 | return collection[0];
59 | }
60 | else
61 | {
62 | return null;
63 | }
64 | }
65 |
66 |
67 | public static List GetNews(int count, string type)
68 | {
69 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("NewsData");
70 | var chineseType = string.Empty;
71 | switch (type)
72 | {
73 | case "top":
74 | chineseType = "头条";
75 | break;
76 | case "shehui":
77 | chineseType = "社会";
78 | break;
79 | case "guonei":
80 | chineseType = "国内";
81 | break;
82 | case "guoji":
83 | chineseType = "国际";
84 | break;
85 | case "yule":
86 | chineseType = "娱乐";
87 | break;
88 | case "tiyu":
89 | chineseType = "体育";
90 | break;
91 | case "junshi":
92 | chineseType = "军事";
93 | break;
94 | case "keji":
95 | chineseType = "科技";
96 | break;
97 | case "caijing":
98 | chineseType = "财经";
99 | break;
100 | case "shishang":
101 | chineseType = "时尚";
102 | break;
103 | }
104 | if (type == "top")
105 | {
106 | return dbWebSites.Find(x => x.type == chineseType).Sort(Builders.Sort.Descending("date")).Limit(count).ToList();
107 | }
108 | else
109 | {
110 | return dbWebSites.Find(x => x.realtype == chineseType).Sort(Builders.Sort.Descending("date")).Limit(count).ToList();
111 | }
112 | }
113 |
114 | public static List GetComments(int useridcomment2)
115 | {
116 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("NewsComments");
117 | var coll = dbWebSites.Find(x => x.UserId == useridcomment2).Sort(Builders.Sort.Ascending("datetime")).ToList();
118 | return coll;
119 | }
120 |
121 | public static NewsItem GetNewsItem(string articleId)
122 | {
123 | var newsItem = new NewsItem();
124 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("NewsData");
125 | var collection = dbWebSites.Find(x => x.uniquekey == articleId).ToList();
126 | if (collection.Count > 0)
127 | {
128 | newsItem.date = collection[0].date;
129 | newsItem.author_name = collection[0].author_name;
130 | newsItem.thumbnail_pic_s = collection[0].thumbnail_pic_s;
131 | newsItem.thumbnail_pic_s02 = collection[0].thumbnail_pic_s02;
132 | newsItem.thumbnail_pic_s03 = collection[0].thumbnail_pic_s03;
133 | newsItem.url = collection[0].url;
134 | newsItem.uniquekey = collection[0].uniquekey;
135 | newsItem.type = collection[0].type;
136 | newsItem.title = collection[0].title;
137 | newsItem.realtype = collection[0].realtype;
138 | Regex regex = new Regex(@"(?)", RegexOptions.Singleline | RegexOptions.Multiline);
139 | var content = Common.Helper.NetworkHelper.GetPageContent(newsItem.url);
140 | var match = regex.Match(content);
141 | if (match.Success)
142 | {
143 | newsItem.pagecontent = match.Groups["article"].ToString();
144 | }
145 | return newsItem;
146 | }
147 | return null;
148 | }
149 |
150 | public static void SaveComment(string useridcomment, string uniquekeycomment, string commnet)
151 | {
152 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("NewsComments");
153 | var commentEntity = new NewsComments();
154 | var user = GetUser(useridcomment);
155 | commentEntity.uniquekey = uniquekeycomment;
156 | commentEntity.UserId = int.Parse(useridcomment);
157 | commentEntity.Comments = commnet;
158 | commentEntity.UserName = user.NickUserName;
159 | commentEntity.datetime = DateTime.Now.ToString("yyyy-MM-dd");
160 | dbWebSites.InsertOne(commentEntity);
161 | }
162 |
163 | public static List GetComments(string uniquekeygetcomments)
164 | {
165 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("NewsComments");
166 | var coll = dbWebSites.Find(x => x.uniquekey == uniquekeygetcomments).Sort(Builders.Sort.Ascending("datetime")).ToList();
167 | return coll;
168 | }
169 |
170 | public static List GetUserCollection(string useridgetuc)
171 | {
172 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("UserCollections");
173 | var coll = dbWebSites.Find(x => x.UserId == int.Parse(useridgetuc)).ToList();
174 | return coll;
175 | }
176 |
177 | public static void SaveUserCollection(string useriduc, string uniquekeyuc)
178 | {
179 | var dbWebSites = MongoDBClient.GetMongoClient().GetCollection("UserCollections");
180 | var uc = new UserCollections();
181 | uc.uniquekey = uniquekeyuc;
182 | uc.UserId = int.Parse(useriduc);
183 | var news = GetNewsItem(uc.uniquekey);
184 | uc.Title = news.title;
185 | dbWebSites.InsertOne(uc);
186 | }
187 | }
188 | }
189 |
--------------------------------------------------------------------------------