Calendar + NLP modules implemented
This commit is contained in:
BIN
backend/modules/calendar/__pycache__/api.cpython-312.pyc
Normal file
BIN
backend/modules/calendar/__pycache__/api.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/modules/calendar/__pycache__/models.cpython-312.pyc
Normal file
BIN
backend/modules/calendar/__pycache__/models.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/modules/calendar/__pycache__/schemas.cpython-312.pyc
Normal file
BIN
backend/modules/calendar/__pycache__/schemas.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/modules/calendar/__pycache__/service.cpython-312.pyc
Normal file
BIN
backend/modules/calendar/__pycache__/service.cpython-312.pyc
Normal file
Binary file not shown.
46
backend/modules/calendar/api.py
Normal file
46
backend/modules/calendar/api.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# modules/calendar/api.py
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
from modules.auth.dependencies import get_current_user
|
||||
from core.database import get_db
|
||||
from modules.auth.models import User
|
||||
from modules.calendar.schemas import CalendarEventCreate, CalendarEventResponse
|
||||
from modules.calendar.service import create_calendar_event, get_calendar_events, update_calendar_event, delete_calendar_event
|
||||
|
||||
router = APIRouter(prefix="/calendar", tags=["calendar"])
|
||||
|
||||
@router.post("/events", response_model=CalendarEventResponse)
|
||||
def create_event(
|
||||
event: CalendarEventCreate,
|
||||
user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
return create_calendar_event(db, user.id, event)
|
||||
|
||||
@router.get("/events", response_model=list[CalendarEventResponse])
|
||||
def get_events(
|
||||
user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
start: datetime | None = None,
|
||||
end: datetime | None = None
|
||||
):
|
||||
return get_calendar_events(db, user.id, start, end)
|
||||
|
||||
@router.put("/events/{event_id}", response_model=CalendarEventResponse)
|
||||
def update_event(
|
||||
event_id: int,
|
||||
event: CalendarEventCreate,
|
||||
user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
return update_calendar_event(db, user.id, event_id, event)
|
||||
|
||||
@router.delete("/events/{event_id}")
|
||||
def delete_event(
|
||||
event_id: int,
|
||||
user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
delete_calendar_event(db, user.id, event_id)
|
||||
return {"message": "Event deleted"}
|
||||
18
backend/modules/calendar/models.py
Normal file
18
backend/modules/calendar/models.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# modules/calendar/models.py
|
||||
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from core.database import Base
|
||||
|
||||
class CalendarEvent(Base):
|
||||
__tablename__ = "calendar_events"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
title = Column(String, nullable=False)
|
||||
description = Column(String)
|
||||
start = Column(DateTime, nullable=False)
|
||||
end = Column(DateTime)
|
||||
location = Column(String)
|
||||
user_id = Column(Integer, ForeignKey("users.id"), nullable=False) # <-- Relationship
|
||||
|
||||
# Bi-directional relationship (for eager loading)
|
||||
user = relationship("User", back_populates="calendar_events")
|
||||
17
backend/modules/calendar/schemas.py
Normal file
17
backend/modules/calendar/schemas.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# modules/calendar/schemas.py
|
||||
from datetime import datetime
|
||||
from pydantic import BaseModel
|
||||
|
||||
class CalendarEventCreate(BaseModel):
|
||||
title: str
|
||||
description: str | None = None
|
||||
start: datetime
|
||||
end: datetime | None = None
|
||||
location: str | None = None
|
||||
|
||||
class CalendarEventResponse(CalendarEventCreate):
|
||||
id: int
|
||||
user_id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
45
backend/modules/calendar/service.py
Normal file
45
backend/modules/calendar/service.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# modules/calendar/service.py
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
from modules.calendar.models import CalendarEvent
|
||||
from core.exceptions import not_found_exception
|
||||
|
||||
def create_calendar_event(db: Session, user_id: int, event_data):
|
||||
event = CalendarEvent(**event_data.dict(), user_id=user_id)
|
||||
db.add(event)
|
||||
db.commit()
|
||||
db.refresh(event)
|
||||
return event
|
||||
|
||||
def get_calendar_events(db: Session, user_id: int, start: datetime, end: datetime):
|
||||
query = db.query(CalendarEvent).filter(
|
||||
CalendarEvent.user_id == user_id
|
||||
)
|
||||
if start:
|
||||
query = query.filter(CalendarEvent.start_time >= start)
|
||||
if end:
|
||||
query = query.filter(CalendarEvent.end_time <= end)
|
||||
return query.all()
|
||||
|
||||
def update_calendar_event(db: Session, user_id: int, event_id: int, event_data):
|
||||
event = db.query(CalendarEvent).filter(
|
||||
CalendarEvent.id == event_id,
|
||||
CalendarEvent.user_id == user_id
|
||||
).first()
|
||||
if not event:
|
||||
raise not_found_exception()
|
||||
for key, value in event_data.dict().items():
|
||||
setattr(event, key, value)
|
||||
db.commit()
|
||||
db.refresh(event)
|
||||
return event
|
||||
|
||||
def delete_calendar_event(db: Session, user_id: int, event_id: int):
|
||||
event = db.query(CalendarEvent).filter(
|
||||
CalendarEvent.id == event_id,
|
||||
CalendarEvent.user_id == user_id
|
||||
).first()
|
||||
if not event:
|
||||
raise not_found_exception()
|
||||
db.delete(event)
|
||||
db.commit()
|
||||
Reference in New Issue
Block a user