Added the sqlite3 database
This commit is contained in:
26
backend/app/routers/contact.py
Normal file
26
backend/app/routers/contact.py
Normal 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
|
Reference in New Issue
Block a user