-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-classes.js
275 lines (226 loc) · 7.24 KB
/
api-classes.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const BASE_URL = "https://hack-or-snooze-v3.herokuapp.com";
/**
* This class maintains the list of individual Story instances
* It also has some methods for fetching, adding, and removing stories
*/
class StoryList {
constructor(stories) {
this.stories = stories;
}
/**
* This method is designed to be called to generate a new StoryList.
* It:
* - calls the API
* - builds an array of Story instances
* - makes a single StoryList instance out of that
* - returns the StoryList instance.*
*/
// TODO: Note the presence of `static` keyword: this indicates that getStories
// is **not** an instance method. Rather, it is a method that is called on the
// class directly. Why doesn't it make sense for getStories to be an instance method?
static async getStories() {
// query the /stories endpoint (no auth required)
const response = await axios.get(`${BASE_URL}/stories`);
// turn the plain old story objects from the API into instances of the Story class
const stories = response.data.stories.map(story => new Story(story));
// build an instance of our own class using the new array of stories
const storyList = new StoryList(stories);
return storyList;
}
/**
* Method to make a POST request to /stories and add the new story to the list
* - user - the current instance of User who will post the story
* - newStory - a new story object for the API with title, author, and url
*
* Returns the new story object
*/
async addStory(user, newStory) {
// TODO - Implement this functions!
// this function should return the newly created story so it can be used in
// the script.js file where it will be appended to the DOM
const {author, title, url} = newStory;
let request = await axios.post(`${BASE_URL}/stories`, {
token: user.loginToken,
story: {
author,
title,
url
}
})
request = request.data.story;
const story = new Story(request);
return story
}
}
/**
* The User class to primarily represent the current user.
* There are helper methods to signup (create), login, and getLoggedInUser
*/
class User {
constructor(userObj) {
this.username = userObj.username;
this.name = userObj.name;
this.createdAt = userObj.createdAt;
this.updatedAt = userObj.updatedAt;
// these are all set to defaults, not passed in by the constructor
this.loginToken = "";
this.favorites = [];
this.ownStories = [];
}
/* Create and return a new user.
*
* Makes POST request to API and returns newly-created user.
*
* - username: a new username
* - password: a new password
* - name: the user's full name
*/
static async create(username, password, name) {
try {
const response = await axios.post(`${BASE_URL}/signup`, {
user: {
username,
password,
name
}
});
// build a new User instance from the API response
const newUser = new User(response.data.user);
// attach the token to the newUser instance for convenience
newUser.loginToken = response.data.token;
return newUser;
}
catch(e) {
let error = JSON.parse(e.response.request.response)
return error.error.message
}
}
/* Login in user and return user instance.
* - username: an existing user's username
* - password: an existing user's password
*/
static async login(username, password) {
try {
const response = await axios.post(`${BASE_URL}/login`, {
user: {
username,
password
}
});
// build a new User instance from the API response
const existingUser = new User(response.data.user);
// instantiate Story instances for the user's favorites and ownStories
existingUser.favorites = response.data.user.favorites.map(s => new Story(s));
existingUser.ownStories = response.data.user.stories.map(s => new Story(s));
// attach the token to the newUser instance for convenience
existingUser.loginToken = response.data.token;
return existingUser;
}
catch (e) {
let error = JSON.parse(e.response.request.response)
return error.error.message
}
}
/** Get user instance for the logged-in-user.
*
* This function uses the token & username to make an API request to get details
* about the user. Then it creates an instance of user with that info.
*/
static async getLoggedInUser(token, username) {
// if we don't have user info, return null
if (!token || !username) return null;
// call the API
const response = await axios.get(`${BASE_URL}/users/${username}`, {
params: {
token
}
});
// instantiate the user from the API information
const existingUser = new User(response.data.user);
// attach the token to the newUser instance for convenience
existingUser.loginToken = token;
// instantiate Story instances for the user's favorites and ownStories
existingUser.favorites = response.data.user.favorites.map(s => new Story(s));
existingUser.ownStories = response.data.user.stories.map(s => new Story(s));
return existingUser;
}
async favoriteStory(token, username, storyId, method='post') {
const newFavorites = [];
if (method === 'post') {
let response = await axios.post(`${BASE_URL}/users/${username}/favorites/${storyId}`, {token})
response = response.data.user.favorites;
for (let story of response) {
newFavorites.push(new Story(story));
}
return newFavorites
}
else {
let response = await axios.delete(`${BASE_URL}/users/${username}/favorites/${storyId}`, {params: {token}})
response = response.data.user.favorites;
for (let story of response) {
newFavorites.push(new Story(story));
}
return newFavorites
}
}
async deleteStory(token, storyId) {
let response = await axios.delete(`${BASE_URL}/stories/${storyId}`, {params: {token}})
response = response.data.story;
return new Story(response);
}
async updateUser(username, name, token, password=null) {
if (password) {
try {
let response = await axios({
method:'patch',
url: `${BASE_URL}/users/${username}`,
data: {
token,
user: {
name,
password
}
}
})
}
catch (e) {
console.log(e);
}
}
else {
try {
let response = await axios({
method:'patch',
url: `${BASE_URL}/users/${username}`,
data: {
token,
user: {
name,
}
}
})
}
catch (e) {
console.dir(e);
}
}
}
}
/**
* Class to represent a single story.
*/
class Story {
/**
* The constructor is designed to take an object for better readability / flexibility
* - storyObj: an object that has story properties in it
*/
constructor(storyObj) {
this.author = storyObj.author;
this.title = storyObj.title;
this.url = storyObj.url;
this.username = storyObj.username;
this.storyId = storyObj.storyId;
this.createdAt = storyObj.createdAt;
this.updatedAt = storyObj.updatedAt;
}
}