Add Admin add / remove users endpoints

This commit is contained in:
Esa Kataja
2024-11-09 19:13:46 +02:00
parent a98c022795
commit 2a8595b5fd
3 changed files with 77 additions and 9 deletions
+47
View File
@@ -0,0 +1,47 @@
from lib.database.database import db_run
from models import Message
from models.user import UserCredentials
from models.movie import Movie
from models.card import Card
import bcrypt
def add_user(user: UserCredentials):
hashed_password = bcrypt.hashpw(
user.password.encode("utf-8"), bcrypt.gensalt()
).decode("utf-8")
sql = f"INSERT INTO users (username, password) VALUES ('{user.username}', '{hashed_password}')"
try:
db_run(sql)
except:
return Message(message="User already exists", message_type="error")
return Message(message="User added successfully", message_type="success")
def remove_user(user_id: str):
sql = f"DELETE FROM users WHERE id = '{user_id}'"
try:
db_run(sql)
except:
return Message(message="User not found", message_type="error")
return Message(message="User removed successfully", message_type="success")
def add_movie(movie: Movie):
pass
def remove_movie(movie_id: str):
pass
def add_card(card: Card):
pass
def remove_card(card_id: str):
pass
+10 -1
View File
@@ -1,6 +1,7 @@
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
import bcrypt
from lib import settings from lib import settings
@@ -44,9 +45,17 @@ class UserBase(BaseModel):
class UserIn(UserBase): class UserIn(UserBase):
password: Optional[str] = Field( password: Optional[str] = Field(
default=None, description="Password of the user", examples=["password"] default=None,
description="Password of the user",
examples=[bcrypt.hashpw("password".encode("utf-8"), bcrypt.gensalt())],
) )
@property
def hashed_password(self):
return bcrypt.hashpw(self.password.encode("utf-8"), bcrypt.gensalt()).decode(
"utf-8"
)
class UserOut(UserBase): class UserOut(UserBase):
pass pass
+20 -8
View File
@@ -1,5 +1,17 @@
from fastapi import APIRouter from fastapi import APIRouter
from models import Message
from models.user import UserIn, UserCredentials
from models.movie import Movie
from lib.database.admin import (
add_user,
remove_user,
add_movie,
remove_movie,
add_card,
remove_card,
)
router = APIRouter( router = APIRouter(
prefix="/admin", prefix="/admin",
tags=["admin"], tags=["admin"],
@@ -8,36 +20,36 @@ router = APIRouter(
@router.post("/user") @router.post("/user")
async def add_user(user: str): async def add_user_endpoint(user: UserCredentials):
"""Add a user to the database""" """Add a user to the database"""
return {"user": user} return add_user(user)
@router.delete("/user") @router.delete("/user")
async def remove_user(user: str): async def remove_user_endpoint(user_id: str):
"""Remove a user from the database""" """Remove a user from the database"""
return {"user": user} return remove_user(user_id)
@router.post("/movie") @router.post("/movie")
async def add_movie(movie: str): async def add_movie_endpoint(movie: Movie):
"""Add a movie to the database""" """Add a movie to the database"""
return {"movie": movie} return {"movie": movie}
@router.delete("/movie") @router.delete("/movie")
async def remove_movie(movie: str): async def remove_movie_endpoint(movie: str):
"""Remove a movie from the database""" """Remove a movie from the database"""
return {"movie": movie} return {"movie": movie}
@router.post("/card") @router.post("/card")
async def add_card(card: str): async def add_card_endpoint(card: str):
"""Add a card to the database""" """Add a card to the database"""
return {"card": card} return {"card": card}
@router.delete("/card") @router.delete("/card")
async def remove_card(card: str): async def remove_card_endpoint(card: str):
"""Remove a card from the database""" """Remove a card from the database"""
return {"card": card} return {"card": card}