forked from pruet/DNWS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterAPI.cs
More file actions
180 lines (175 loc) · 6.34 KB
/
Copy pathTwitterAPI.cs
File metadata and controls
180 lines (175 loc) · 6.34 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json;
namespace DNWS
{
class TwitterAPI : TwitterPlugin
{
public string[] Test()
{
return new string[]
{
"Hello,", "Test!!!"
};
}
public List<User> GetAllUsers()
{
using (var context = new TweetContext())
{
try
{
List<User> users = context.Users.Where(b => true).Include(b => b.Following).ToList();
return users;
}
catch (Exception)
{
return null;
}
}
}
public List<Following> GetFollowing(string name)
{
using (var context = new TweetContext())
{
try
{
List<User> followings = context.Users.Where(b => b.Name.Equals(name)).Include(b => b.Following).ToList();
return followings[0].Following;
}
catch (Exception)
{
return null;
}
}
}
public override HTTPResponse GetResponse(HTTPRequest request)//REF.600611030
{
HTTPResponse response = new HTTPResponse(200);
string user = request.getRequestByKey("user");
string password = request.getRequestByKey("password");
string following = request.getRequestByKey("follow");
string message = request.getRequestByKey("message");
string[] path = request.Filename.Split("?");
if (path[0] == "users")
{
if (request.Method == "GET")
{
string json = JsonConvert.SerializeObject(GetAllUsers());
response.body = Encoding.UTF8.GetBytes(json);
}
else if (request.Method == "POST")
{
try
{
Twitter.AddUser(user, password);
response.body = Encoding.UTF8.GetBytes("200 OK");
}
catch (Exception)
{
response.status = 403;
response.body = Encoding.UTF8.GetBytes("403 Forbidden");
}
}
else if (request.Method == "DELETE")
{
try
{
Twitter twitter = new Twitter(user);
twitter.DeleteUser(user);
response.body = Encoding.UTF8.GetBytes("200 OK");
}
catch (Exception)
{
response.status = 404;
response.body = Encoding.UTF8.GetBytes("404 Not Found");
}
}
}
else if (path[0] == "following")
{
if (request.Method == "GET")
{
string json = JsonConvert.SerializeObject(GetFollowing(user));
response.body = Encoding.UTF8.GetBytes(json);
}
else if (request.Method == "POST")
{
if (Twitter.CheckUser(following))
{
Twitter twitter = new Twitter(user);
twitter.AddFollowing(following);
response.body = Encoding.UTF8.GetBytes("200 OK");
}
else
{
response.status = 404;
response.body = Encoding.UTF8.GetBytes("404 Not Found");
}
}
else if (request.Method == "DELETE")
{
try
{
Twitter twitter = new Twitter(user);
twitter.RemoveFollowing(following);
response.body = Encoding.UTF8.GetBytes("200 OK");
}
catch (Exception)
{
response.status = 404;
response.body = Encoding.UTF8.GetBytes("404 Not Found");
}
}
}
else if (path[0] == "tweets")
{
if (request.Method == "GET")
{
try
{
Twitter twitter = new Twitter(user);
string timeline = request.getRequestByKey("timeline");
if (timeline == "following")
{
string json = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
response.body = Encoding.UTF8.GetBytes(json);
}
else
{
Twitter twitter = new Twitter(user);
string json = JsonConvert.SerializeObject(twitter.GetUserTimeline());
response.body = Encoding.UTF8.GetBytes(json);
}
}
catch (Exception)
{
response.status = 404;
response.body = Encoding.UTF8.GetBytes("404 Not Found");
}
}
else if (request.Method == "POST")
{
try
{
Twitter twitter = new Twitter(user);
twitter.PostTweet(message);
response.body = Encoding.UTF8.GetBytes("200 OK");
}
catch (Exception)
{
response.status = 404;
response.body = Encoding.UTF8.GetBytes("404 Not Found");
}
}
}
response.type = "application/json";
return response;
}
}
}