forked from Elevationacademy/full-stack-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivityModel.js
76 lines (67 loc) · 2.05 KB
/
ActivityModel.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
class ActivityModel {
async getUserUniversity(userId) {
try {
return await $.get(`activities/university/${userId}`)
} catch (error) {
throw error
}
}
async getAllActivities(userId,filtered, filterData) {
try {
const universityName = await this.getUserUniversity(userId)
if(filtered){
filterData.universityName = universityName
return await $.get(`/activities/${userId}?${$.param(filterData)}` )
}
else {
return await $.get(`/activities/${userId}`)
}
} catch (error) {
throw error
}
}
async addActivity(userId, activityData) {
try {
await $.post(`/activities/${userId}`, activityData)
} catch (error) {
throw error
}
}
async showMyActivities(userId) {
try {
const data = await $.get(`/activities/myActivities/${userId}`)
return data
} catch (error) {
throw error
}
}
async deleteMyActivity(userId, activityId) {
await $.ajax({
url: `/activities/${activityId}`,
type: 'DELETE',
data: { userId },
success: function() {
return true
},
message: 'Activity removed successfully',
error: function(xhr, status, error) {
console.error(error)
}
})
}
async editCapacity(userId, activityId, newCapacity) {
console.log({ userId, newCapacity })
return await $.ajax({
url: `/activities/${activityId}`,
type: 'PATCH',
contentType: 'application/json',
data: JSON.stringify({ userId,"capacity": newCapacity }),
success: function(response) {
alert("Your capacity has successfully changed.")
},
error: function(error) {
alert("Couldn't change the capacity.")
}
})
}
}