-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-classes.js
257 lines (209 loc) · 7.66 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
const BASE_URL = "https://hack-or-snooze-v2.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, and then returns the StoryList instance.
*
* Note the presence of the `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 $.getJSON(`${BASE_URL}/stories`);
// turn the plain old story objects from the API into instances of the Story class
const stories = response.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;
}
/**
* getMoreStories
* Pretty much same as getStories but passes a parameter to skip X number
* of stories to enable infinite-scroll
* @param {*} numToSkip
*/
static async getMoreStories(numToSkip) {
const response = await $.getJSON(`${BASE_URL}/stories?skip=${numToSkip}`);
const stories = response.stories.map(story => new Story(story));
const storyList = new StoryList(stories);
return storyList;
}
/**
* Method to make a POST request to /stories and add the new story to the list
The function should accept the current instance of User who will post the story
It should also accept an object which with a title, author, and url
*/
async addStory(user, newStory) {
// Note - payload works like this...didn't work with double quotes around everything!
// Also use pojo rather than a "Story" object!
let payload =
{
token: user.loginToken,
story: newStory
};
// Do post request to api to write new story:
const response = await $.post(`${BASE_URL}/stories`, payload);
// Create a Story object from the returned story
let returnStory = new Story(response.story);
return returnStory;
}
}
/**
* 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 = [];
}
/**
* This adds a favorite story to the user class and returns the newly updated favorite list
* @param {*} storyId A string that specifies the id of the story
*/
async addFavoriteToUser(storyId){
// Need the token:
let payload =
{
token: this.loginToken
};
// Then just pass the story id in with the url and the payload (just token in this case):
const response = await $.post(`${BASE_URL}/users/${this.username}/favorites/${storyId}`, payload);
// Reassigning favorites with newly updated favorites list
this.favorites = response.user.favorites;
return this.favorites;
}
/**
* This removes a favorite story to the user class and returns the newly updated favorite list
* @param {*} storyId A string that specifies the id of the story
*/
async removeFavoriteToUser(storyId){
// Need the token:
let payload =
{
token: this.loginToken
};
// "DELETE" requires us to use 'ajax' (and we need to add the payload here in data)
let ajaxSettings = {
type: 'DELETE',
data: payload
}
// Then just pass the story id in with the url and the ajax settings:
const response = await $.ajax(`${BASE_URL}/users/${this.username}/favorites/${storyId}`, ajaxSettings);
// Reassigning favorites with newly updated favorites list
this.favorites = response.user.favorites;
return this.favorites;
}
// Returns True if the story is favorited by the user else false:
hasFavorited(storyId){
return this.favorites.some(obj => obj.storyId === storyId);
}
/** Deletes a story with the API and returns nothing*/
async deleteStory(storyId) {
let payload = {
token: this.loginToken
};
// "DELETE" requires us to use 'ajax' (and we need to add the payload here in data)
let ajaxSettings = {
type: 'DELETE',
data: payload
}
// Then just pass the story id in with the url and the ajax settings:
await $.ajax(`${BASE_URL}/stories/${storyId}`, ajaxSettings);
// Filter out the removed storyId
this.ownStories = this.ownStories.filter( (story) => story.storyId !== storyId);
}
/*
A class method to create a new user - it accepts a username, password and name
It makes a POST request to the API and returns the newly created User as well as a token
*/
static async create(username, password, name) {
const response = await $.post(`${BASE_URL}/signup`, {
user: {
username,
password,
name
}
});
// build a new User instance from the API response
const newUser = new User(response.user);
// attach the token to the newUser instance for convenience
newUser.loginToken = response.token;
return newUser;
}
/*
A class method to log in a user. It returns the user
*/
static async login(username, password) {
const response = await $.post(`${BASE_URL}/login`, {
user: {
username,
password
}
});
// build a new User instance from the API response
const existingUser = new User(response.user);
// instantiate Story instances for the user's favorites and ownStories
existingUser.favorites = response.user.favorites.map(story => new Story(story));
existingUser.ownStories = response.user.stories.map(story => new Story(story));
// attach the token to the newUser instance for convenience
existingUser.loginToken = response.token;
return existingUser;
}
/**
* 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 inf function.
*/
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 $.getJSON(`${BASE_URL}/users/${username}`, {
token
});
// instantiate the user from the API information
const existingUser = new User(response.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.user.favorites.map(
story => new Story(story)
);
existingUser.ownStories = response.user.stories.map(
story => new Story(story)
);
return existingUser;
}
}
/**
* Class to represent a single story. Has one method to update.
*/
class Story {
/*
* The constructor is designed to take an object for better readability / flexibility
*/
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;
}
}