{"size": 3083, "ext": "js", "max_stars_repo_path": "FrontEnd/src/components/Api.js", "max_stars_repo_name": "ShenJiahuan/SE228", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "FrontEnd/src/components/Api.js", "max_issues_repo_name": "ShenJiahuan/SE228", "max_issues_repo_issues_event_min_datetime": "2021-03-10T14:24:08.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-19T00:31:16.000Z", "max_forks_repo_path": "FrontEnd/src/components/Api.js", "max_forks_repo_name": "ShenJiahuan/SE228", "max_forks_repo_forks_event_min_datetime": "2020-06-09T02:54:24.000Z", "max_forks_repo_forks_event_max_datetime": "2020-06-09T02:54:24.000Z"}
{"max_stars_count": null, "max_issues_count": 4, "max_forks_count": 1, "avg_line_length": 22.3405797101, "max_line_length": 81, "alphanum_fraction": 0.556925073}
import axios from "axios";
import { Loading } from "element-ui";
import store from "../store";

axios.defaults.baseURL = store.state.config.backendServer;
axios.defaults.withCredentials = true;

let loadingInstance = null;

axios.interceptors.request.use(
    config => {
        loadingInstance = Loading.service({ fullscreen: true });
        return config;
    },
    error =>{
        loadingInstance.close();
        return Promise.reject(error);
});

axios.interceptors.response.use(
    response => {
        loadingInstance.close();
        return response;
    },
    error => {
        loadingInstance.close();
        return Promise.reject(error);
});

export default {
    GetBookInfo(id) {
        return axios.get("/books/" + id);
    },

    GetBookList(keyword) {
        return axios.get("/books/", {params: {keyword: keyword}});
    },

    GetAllBooks() {
        return axios.get("/books/all");
    },

    GetRecommendList(limit) {
        return axios.get("/books/top/recommend",  {params: {limit: limit}});
    },

    GetHotList(limit) {
        return axios.get("/books/top/hot", {params: {limit: limit}} );
    },

    CreateBook(info) {
        return axios.post("/books", info);
    },

    UpdateBook(info) {
        return axios.put("/books", info);
    },

    DeleteBook(id) {
        return axios.delete("/books/" + id);
    },

    Login(email, password) {
        var params = new URLSearchParams();
        params.append("email", email);
        params.append("password", password);
        return axios.post("/user/login", params);
    },

    Register(email, username, password) {
        var params = new URLSearchParams();
        params.append("email", email);
        params.append("username", username);
        params.append("password", password);
        return axios.post("/user/register", params);
    },

    Logout() {
        return axios.get("/user/logout");
    },

    GetUsername() {
        return axios.get("/user");
    },

    GetOrder() {
        return axios.get("/orders");
    },

    CreateOrder(order) {
        return axios.post("/orders", order);
    },

    CartToOrder(order) {
        return axios.put("/orders", order);
    },

    GetOrderStatus(from, to) {
        return axios.get("/order_stat", {params: {from: from, to: to}});
    },

    GetUserPurchase(from, to) {
        return axios.get("/purchase_stat", {params: {from: from, to: to}});
    },

    GetAllOrders() {
        return axios.get("/orders/all");
    },

    GetCart() {
        return axios.get("/cart");
    },

    CreateCart(order) {
        return axios.post("/cart", order);
    },

    UpdateCart(order) {
        return axios.put("/cart", order);
    },

    DeleteCart(order) {
        return axios.delete("/cart", {data: order});
    },

    GetAllUser() {
        return axios.get("/user/list");
    },

    BanUser(uid, ban) {
        return axios.post("/user/ban", {}, {params: {uid: uid, ban: ban}});
    },

    AdminUser(uid, admin) {
        return axios.post("/user/admin", {}, {params: {uid: uid, admin: admin}});
    }
};
