post.js
1.42 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
import * as auth from '../auth'
import axios from 'axios';
import { SERVER_BASE_URL } from '..';
export const newPost = async (
title,
explain,
example,
testinput,
testoutput,
) => {
if (!auth.validateToken()) {
throw new Error("plz login");
}
const response = await axios.post(`${SERVER_BASE_URL}/post`, {
token: auth.getToken(),
title,
explain,
example,
testinput,
testoutput,
});
if (response.status !== 200 && response.status !== 201) {
throw new Error('Failed to create new post!');
}
return response.data.id;
}
export const getPostbyId = async (id) => {
const response = await axios.get(`${SERVER_BASE_URL}/post/id/${id}`);
if (response.status !== 200) {
throw new Error('Failed to get post!');
}
return response.data;
}
export const getPosts = async (takes) => {
const response = await axios.get(`${SERVER_BASE_URL}/post`, {
params: {
takes,
},
});
if (response.status !== 200) {
throw new Error('Failed to get posts!');
}
return response.data;
}
export const getPostsByDifficulty = async (difficulty) => {
const response = await axios.get(`${SERVER_BASE_URL}/post/difficulty/${difficulty}`);
if (response.status !== 200) {
throw new Error('Failed to get posts!');
}
console.log(response.data);
return response.data;
}