Added the sqlite3 database

This commit is contained in:
GotthardG
2024-11-01 14:13:38 +01:00
parent 579e769bb0
commit 48cd233231
11 changed files with 425 additions and 0 deletions

View File

@ -0,0 +1,26 @@
# app/routers/contact.py
from fastapi import APIRouter, HTTPException, status
from typing import List
from app.data.data import contacts
from app.models import ContactPerson
router = APIRouter()
@router.get("/", response_model=List[ContactPerson])
async def get_contacts():
return contacts
@router.post("/", response_model=ContactPerson, status_code=status.HTTP_201_CREATED)
async def create_contact(contact: ContactPerson):
if any(c.email == contact.email for c in contacts):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="This contact already exists."
)
if contacts:
max_id = max(c.id for c in contacts)
contact.id = max_id + 1 if contact.id is None else contact.id
else:
contact.id = 1 if contact.id is None else contact.id
contacts.append(contact)
return contact