"""
    encoding: UTF-8
    @author:clx
    @file:items.py
    @time:2023/5/30 6:08
"""
from typing import Dict

from fastapi import APIRouter, HTTPException
from models.model import Item

router = APIRouter()

res = [
    {"id": 1, "name": "jack"},
    {"id": 2, "name": "mark"},
    {"id": 3, "name": "tom"},
    {"id": 4, "name": "sery"}
]

@router.get("/item/res")
async def get_res():
    return res

@router.get("/item/res/{res_id}")
async def get_res_id(res_id:int):
    for data in res:
        if data["id"] == res_id:
            return data

@router.post("/item/res")
async def create_res(item:Item):
    res.append(item.dict())
    return res

@router.put("/item/res/{res_id}")
async def update_res(res_id:int,item:Item):
    for data in res:
        print(data["id"])
        print(res_id)
        if data["id"] == res_id:
            res.remove(data)
            data = item.dict()
            res.append(data)
            return {"status":True}
    else:
        return HTTPException(status_code=404, detail="id:{} not found.".format(res_id))

@router.delete("/item/res/{res_id}")
async def delete_res(res_id:int):
    for data in res:
        if data["id"] == res_id:
            res.remove(data)
    return {"status":True}