news & things
This commit is contained in:
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