package com.sdut.labex.controller;

import com.alibaba.fastjson.JSONObject;
import com.sdut.labex.entity.TimeOption;
import com.sdut.labex.service.TimeOptionService;
import com.sdut.labex.utils.ResVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * File: TimeOptionController
 * Created: 2023/6/12
 * Author: springforest
 * Description:
 */
@RestController
@CrossOrigin
public class TimeOptionController {

    @Resource
    private TimeOptionService timeOptionService;

    @PostMapping("/timeOption/{name}")
    public ResVo addTimeOption(@PathVariable("name") String name) {
        TimeOption timeOption = new TimeOption();
        timeOption.setName(name);
        return timeOptionService.addTimeOption(timeOption);
    }

    @PutMapping("/timeOption")
    public ResVo updateTimeOption(@RequestBody JSONObject jsonObject) {
        TimeOption timeOption = new TimeOption();
        timeOption.setId(jsonObject.getInteger("id"));
        timeOption.setName(jsonObject.getString("name"));
        return timeOptionService.updateTimeOption(timeOption);
    }

    @DeleteMapping("/timeOption/{id}")
    public ResVo deleteTimeOption(@PathVariable("id") Integer id) {
        return timeOptionService.deleteTimeOption(id);
    }

    @GetMapping("/timeOption")
    public ResVo getTimeOptions() {
        return timeOptionService.getTimeOptions();
    }
}
