-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-classes.js
232 lines (187 loc) · 6.58 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
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.*
*/
// 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) {
// 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
// function is called when user submits a story
// post to API
const response = await axios({
method: "POST",
url: `${BASE_URL}/stories`,
data: {
// request body - example of API structure above
token: user.loginToken,
story: newStory // pass newStory to post on API and return response
}
});
// Make story instance of data received
newStory = new Story(response.data.story);
// Add newStory to beginning of stories array
this.stories.unshift(newStory);
// Add newStory to beginning of users own stories array
user.ownStories.unshift(newStory);
// Return object to generateStoryHTML and prepend to $allStoriesList
return newStory;
}
// Delete story from Story List
async deleteStory(user, storyId) {
await axios({
url: `${BASE_URL}/stories/${storyId}`,
method: "DELETE",
params: {
token: user.loginToken
}
});
this.stories = this.stories.filter(story => story.storyId !== storyId);
user.updateUserData(); // Update user's stories
}
}
/**
* 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 = [];
}
static async create(username, password, name) {
const response = await axios.post(`${BASE_URL}/users`, {
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;
}
/* Login in user and return user instance.
*/
static async login(username, password) {
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;
}
/** 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 updateUserData() {
const response = await axios({
url: `${BASE_URL}/users/${this.username}`,
params: {
token: this.loginToken // token needed to access user
}
});
this.name = response.data.user.name; //Update name if any changes
this.createdAt = response.data.createdAt;
this.updatedAt = response.data.updatedAt;
// these are all set to defaults, not passed in by the constructor
this.favorites = response.data.user.favorites.map(s => new Story(s));
this.ownStories = response.data.user.stories.map(s => new Story(s));
return this;
}
// Update User's favorites
async updateFavorites(storyId, method) {
await axios({
url: `${BASE_URL}/users/${this.username}/favorites/${storyId}`,
method: method,
params: {
token: this.loginToken
}
});
await this.updateUserData();
return this;
}
}
/**
* 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;
}
}