import {client, processResponse} from '../client';
import {apiUrls} from "app/src/api/api-urls";
import qs from "qs";

const majorManager = {
  list, add, getInfo, update, deleteMajor
}

export default majorManager

async function list() {
  console.log('request list major')
  const response = await client.get(apiUrls.major.list)
  return processResponse(response)
}

async function getInfo(id) {
  const response = await client.get(apiUrls.major.info, {
    params: {id: id}
  })

  return processResponse(response)
}

async function add(data) {
  const request = {
    name: data
  }

  const response = await client.post(apiUrls.admin.major.add, qs.stringify(request))

  return processResponse(response)
}

async function update(id, data) {
  const request = {
    name: data.name === '' ? undefined : data.name
  }

  const response = await client.post(
    apiUrls.admin.major.update,
    qs.stringify(request), {
      params: {id: id}
    })

  return processResponse(response)
}


async function deleteMajor(id) {
  const response = await client.post(apiUrls.admin.major.delete, undefined, {
    params: {id: id}
  })

  return processResponse(response)
}
