Add post user with airline role

This commit is contained in:
bsquillari 2023-12-04 20:51:13 +00:00
parent 1ef259a7b4
commit 00f016f229
3 changed files with 33 additions and 2 deletions

View File

@ -1,5 +1,5 @@
from src import db from src import db
from src.api.models.users import User from src.api.models.users import Roles, User
def get_all_users(): def get_all_users():
@ -21,6 +21,13 @@ def add_user(username, email, password):
return user return user
def add_airline(username, email, password):
user = User(username=username, email=email, password=password, role=Roles.airline)
db.session.add(user)
db.session.commit()
return user
def update_user(user, username, email): def update_user(user, username, email):
user.username = username user.username = username
user.email = email user.email = email

View File

@ -10,6 +10,7 @@ from src.api.cruds.users import ( # isort:skip
get_user_by_id, get_user_by_id,
update_user, update_user,
delete_user, delete_user,
add_airline,
) )
NAMESPACE = "users" NAMESPACE = "users"
@ -34,6 +35,7 @@ class UsersList(Resource):
username = post_data.get("username") username = post_data.get("username")
email = post_data.get("email") email = post_data.get("email")
password = post_data.get("password") password = post_data.get("password")
role = post_data.get("role")
response_object = {} response_object = {}
user = get_user_by_email(email) user = get_user_by_email(email)
@ -41,7 +43,10 @@ class UsersList(Resource):
response_object["message"] = "Sorry. That email already exists." response_object["message"] = "Sorry. That email already exists."
return response_object, 400 return response_object, 400
user = add_user(username, email, password) if role == "airline":
user = add_airline(username, email, password)
else:
user = add_user(username, email, password)
response_object = { response_object = {
"message": f"{user.email} was added!", "message": f"{user.email} was added!",

View File

@ -22,6 +22,25 @@ async def create_users(user: UserRegister, req: Request):
return response return response
@router.post("/airline", response_model=UserMin)
async def create_airline(
user: UserRegister,
req: Request,
authorization: Annotated[str | None, Header()] = None,
):
await checkAuth(req, authorization, roles=["admin"])
request_id = req.state.request_id
header = {"x-api-request-id": request_id}
data = user.model_dump()
data["role"] = "airline"
(response, status, _) = await request(
f"{API_USERS}", "POST", json=data, headers=header
)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@router.get("/{id}", response_model=User) @router.get("/{id}", response_model=User)
async def get_user( async def get_user(
id: str, req: Request, authorization: Annotated[str | None, Header()] = None id: str, req: Request, authorization: Annotated[str | None, Header()] = None