package com.sdut.labex.controller;

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

import javax.annotation.Resource;

/**
 * File: RoomController
 * Created: 2023/6/12
 * Author: springforest
 * Description:
 */
@RestController
@CrossOrigin
public class RoomController {
    @Resource
    private RoomService roomService;

    @GetMapping("/room")
    public ResVo getAllRoom() {
        return roomService.getAllRoom();
    }

    @PostMapping("/room")
    public ResVo addRoom(@RequestBody JSONObject jsonObject) {
        Room room = new Room();
        room.setName(jsonObject.getString("name"));
        room.setDescription(jsonObject.getString("description"));
        room.setStatus(jsonObject.getInteger("status"));
        return roomService.addRoom(room);
    }

    @PutMapping("/room")
    public ResVo updateRoom(@RequestBody JSONObject jsonObject) {
        Room room = new Room();
        room.setId(jsonObject.getInteger("id"));
        room.setName(jsonObject.getString("name"));
        room.setDescription(jsonObject.getString("description"));
        room.setStatus(jsonObject.getInteger("status"));
        return roomService.updateRoom(room);
    }

    @DeleteMapping("/room/{id}")
    public ResVo removeRoom(@PathVariable("id") int id) {
        return roomService.deleteRoomById(id);
    }
}
