package com.test.controller;

import com.test.bean.Result;
import com.test.bean.RoomType;
import com.test.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/roomType")
public class RoomTypeController {

    @Autowired
    RoomTypeService roomTypeService;

//    @GetMapping("/")
    public Result getAllRoomType(){
        return Result.success();
    }

    @GetMapping("/getRoomTypeByPage")
    public Result getRoomTypeByPage(@RequestParam(defaultValue = "1")  int page,
                                    @RequestParam(defaultValue = "5") int pageSize){
        return Result.success(roomTypeService.getRoomTypeByPage(page, pageSize));

    }
    @GetMapping
    public Result getRoomTypeById(@RequestParam Long id){
        RoomType roomType = roomTypeService.getRoomTypeById(id);
        if (roomType != null && roomType.getTypeId() == null ){
            return Result.error("此房型不存在!");
        }
        return Result.success(roomType);
    }
    @PostMapping
    public Result addRoomType(@RequestBody RoomType roomType){
        if (roomTypeService.addRoomType(roomType)){
            return Result.success();
        }
        return Result.error("添加失败!");
    }
    @PutMapping
    public Result updateRoomType(@RequestBody RoomType roomType){
        if (roomTypeService.updateRoomType(roomType)){
            return Result.success(roomTypeService.getRoomTypeById(roomType.getTypeId()));
        }
        return Result.error("修改失败!");

    }
    @DeleteMapping
    public Result deleteRoomType(@RequestParam Long id){
        if (roomTypeService.deleteRoomType(id)){
            return Result.success();
        }
        return Result.error("删除失败!");
    }

}
