-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-classes.js
More file actions
232 lines (193 loc) · 7.29 KB
/
Copy pathapi-classes.js
File metadata and controls
232 lines (193 loc) · 7.29 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
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.*
*/
// 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?
// get story is a static method to call on the class because we do not need to access its instances or object instances.
static async getStories() {
// query the /stories endpoint (no auth required)
const response = await axios.get(`${BASE_URL}/stories`); // gets all json data from 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)); //takes each story using map and creates a new array of Story instances from story class
//story class just creates a new object of author, title, url, username, storyId, createdAt, updatedAt
// build an instance of our own class using the new array of stories
const storyList = new StoryList(stories); // This creates a class of Story class
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
*/
static 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
//check if user
if (!user.loginToken || !user) return null
//post to data base story info
const response = await axios.post(`${BASE_URL}/stories`, newStory);
//creates a new story from response from database
const story = new Story(response.data.story);
return story
}
static async removeStory(user, newStory) {
//check if user
if (!user.loginToken || !user) return null
const response = await axios.delete(`${BASE_URL}/stories/${newStory}`,{data:{
token: user.loginToken
}
})
}
}
// create story from hack or snooze
// {
// "token": "YOUR_TOKEN_HERE",
// "story": {
// "author": "Matt Lane",
// "title": "The best story ever",
// "url": "http://google.com"
// }
// }
/**
* 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) {
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;
}
/* Login in user and return user instance.
* - username: an existing user's username
* - password: an existing user's password
*/
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;
}
// Insert a storyId and make it a favorite
static async addFavoriteStory(user, storyId){
//sends storyID to database and creates a post request response
const response = await axios.post(`${BASE_URL}/users/${user.username}/favorites/${storyId}`, {
token : user.loginToken
})
//favorite data user to existing user variable
const existingUser = new User(response.data.user);
//update favorites variable
existingUser.favorites = response.data.user.favorites.map(s => new Story(s));
return existingUser.favorites;
}
//delete the favorite story
static async deleteFavoriteStory(user, storyId){
//sends storyID to database and creates a delete request response
const response = await axios.delete(`${BASE_URL}/users/${user.username}/favorites/${storyId}`,
{data:{
token : user.loginToken
}
})
//favorite data user to existing user variable
const existingUser = new User(response.data.user);
//update favorites variable
existingUser.favorites = response.data.user.favorites.map(s => new Story(s));
return existingUser.favorites;
}
}
/**
* 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;
}
}