File size: 1,376 Bytes
8821488
0030454
1ebd440
5fa8a0c
 
ac6ba74
5fa8a0c
 
 
 
 
 
 
 
0030454
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb1def4
 
a77f6c5
 
eb1def4
0030454
 
 
 
 
 
 
5fa8a0c
 
 
0030454
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from fastapi import FastAPI
from typing import List, Dict, Any
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()
# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class Database:
    def __init__(self):
        self.store = {}

    def insert(self, collection_name: str, document: Dict[str, Any]):
        if collection_name not in self.store:
            self.store[collection_name] = []
        
        self.store[collection_name].append(document)
        return document

    def find(self, collection_name: str, query: Dict[str, Any] = None):
        if collection_name not in self.store:
            return []

        if not query:
            return self.store[collection_name]
        
        return [doc for doc in self.store[collection_name] if all(item in doc.items() for item in query.items())]

db = Database()

@app.get("/")
def home():
    home = "Hello, Welcome to LinDB!"
    return home

@app.get("/items", response_model=List[Dict[str, Any]])
async def get_items():
    return db.find('items')

@app.post("/items", response_model=Dict[str, Any])
async def add_item(item: Dict[str, Any]):
    return db.insert('items', item)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=3000)