news & things
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
|
||||
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import relationship
|
||||
import uuid
|
||||
import datetime
|
||||
|
||||
from .database import Base
|
||||
|
||||
@ -18,6 +19,15 @@ class User(Base):
|
||||
owns_queues = relationship("Queue", backref="owner", lazy="dynamic")
|
||||
|
||||
|
||||
class News(Base):
|
||||
__tablename__ = "news"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
title = Column(String)
|
||||
content = Column(String)
|
||||
created = Column(DateTime, default=datetime.datetime.utcnow)
|
||||
|
||||
|
||||
class AnonymousUser(Base):
|
||||
__tablename__ = "anonymoususers"
|
||||
|
||||
|
||||
@ -7,12 +7,14 @@ from .dependencies import get_db
|
||||
|
||||
from .views.auth.api import router as auth_router
|
||||
from .views.queue.api import router as queue_router
|
||||
from .views.news.api import router as news_router
|
||||
|
||||
app = FastAPI(dependencies=[Depends(get_db)])
|
||||
models.Base.metadata.create_all(bind=engine)
|
||||
|
||||
app.include_router(queue_router)
|
||||
app.include_router(auth_router)
|
||||
app.include_router(news_router)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
|
||||
0
backend/app/views/news/__init__.py
Normal file
0
backend/app/views/news/__init__.py
Normal file
41
backend/app/views/news/api.py
Normal file
41
backend/app/views/news/api.py
Normal file
@ -0,0 +1,41 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Annotated, Union
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ...config import jwt_config
|
||||
from ...dependencies import get_db
|
||||
from . import schemas
|
||||
from . import services
|
||||
|
||||
from ..auth import services as auth_services
|
||||
from ..auth import schemas as auth_schemas
|
||||
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/news",
|
||||
tags=["news"],
|
||||
dependencies=[Depends(get_db)],
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def get_news(
|
||||
news: Annotated[schemas.NewsInDb, Depends(services.get_news)],
|
||||
) -> list[schemas.NewsInDb]:
|
||||
return news
|
||||
|
||||
|
||||
@router.post("/")
|
||||
async def create_news(
|
||||
news: schemas.CreateNews,
|
||||
current_user: Annotated[auth_schemas.User, Depends(auth_services.get_current_user)],
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
) -> schemas.NewsInDb:
|
||||
return services.create_news(news=news, current_user=current_user, db=db)
|
||||
22
backend/app/views/news/schemas.py
Normal file
22
backend/app/views/news/schemas.py
Normal file
@ -0,0 +1,22 @@
|
||||
from typing import Union
|
||||
from pydantic import BaseModel
|
||||
from uuid import UUID
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class CreateNews(BaseModel):
|
||||
title: str
|
||||
content: str
|
||||
|
||||
|
||||
class News(BaseModel):
|
||||
title: str
|
||||
content: str
|
||||
created: datetime
|
||||
|
||||
|
||||
class NewsInDb(News):
|
||||
id: UUID
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
37
backend/app/views/news/services.py
Normal file
37
backend/app/views/news/services.py
Normal file
@ -0,0 +1,37 @@
|
||||
from fastapi import Depends, HTTPException
|
||||
from typing import Annotated
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...dependencies import get_db
|
||||
from ...db import models
|
||||
|
||||
from ..auth import services as auth_services
|
||||
from ..auth import schemas as auth_schemas
|
||||
|
||||
from . import schemas
|
||||
|
||||
|
||||
def get_news(
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
) -> list[schemas.NewsInDb]:
|
||||
return [
|
||||
schemas.NewsInDb.model_validate(n)
|
||||
for n in db.query(models.News).order_by(models.News.created.desc()).all()
|
||||
]
|
||||
|
||||
|
||||
def create_news(
|
||||
news: schemas.CreateNews,
|
||||
current_user: auth_schemas.UserInDB,
|
||||
db: Session,
|
||||
) -> schemas.NewsInDb:
|
||||
if current_user.username == "admin":
|
||||
n = models.News(title=news.title, content=news.content)
|
||||
db.add(n)
|
||||
db.commit()
|
||||
return schemas.NewsInDb.model_validate(n)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate credentials",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
Reference in New Issue
Block a user