import m from "mithril"

export const Question = {
    list: [],
    current: {},
    choices: [],
    answers: [],
    message: "",
    error: "",
    fetchList: function() {
        m.request({
            method: "GET",
            url: "/questions"
        })
        .then(function(res) {
            Question.list = res.data
        })
    },
    fetchCurrent: function() {
        m.request({
            method: "GET",
            url: "/questions/:id",
            data: Question.current
        })
        .then(function(res) {
            Question.current = res.data
        })
    },
    fetchChoices: function() {
        m.request({
            method: "GET",
            url: "/questions/:id/choices",
            data: Question.current
        })
        .then(function(res) {
            Question.choices = res.data
        })
    },
    fetchAnswers: function() {
        m.request({
            method: "GET",
            url: "/questions/:id/answers",
            data: Question.current
        })
        .then(function(res) {
            Question.answers = res.data
        })
    },
    upload: function() {
        m.request({
            method: "POST",
            url: "/sections/:sId/questions",
            data: _.assign(Question.current, {sId: Question.current.section.id})
        })
    },
    uploadChoice: function() {

    },
    uploadAnswer: function() {

    },
    update: function() {
        m.request({
            method: "PUT",
            url: "/questions/:id",
            data: Question.current
        })
    },
    remove: function() {
        m.request({
            method: "DELETE",
            url: "/questions/:id",
            data: Question.current
        })
    }
}
