Add post user with airline role
This commit is contained in:
parent
1ef259a7b4
commit
00f016f229
|
@ -1,5 +1,5 @@
|
|||
from src import db
|
||||
from src.api.models.users import User
|
||||
from src.api.models.users import Roles, User
|
||||
|
||||
|
||||
def get_all_users():
|
||||
|
@ -21,6 +21,13 @@ def add_user(username, email, password):
|
|||
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):
|
||||
user.username = username
|
||||
user.email = email
|
||||
|
|
|
@ -10,6 +10,7 @@ from src.api.cruds.users import ( # isort:skip
|
|||
get_user_by_id,
|
||||
update_user,
|
||||
delete_user,
|
||||
add_airline,
|
||||
)
|
||||
|
||||
NAMESPACE = "users"
|
||||
|
@ -34,6 +35,7 @@ class UsersList(Resource):
|
|||
username = post_data.get("username")
|
||||
email = post_data.get("email")
|
||||
password = post_data.get("password")
|
||||
role = post_data.get("role")
|
||||
response_object = {}
|
||||
|
||||
user = get_user_by_email(email)
|
||||
|
@ -41,7 +43,10 @@ class UsersList(Resource):
|
|||
response_object["message"] = "Sorry. That email already exists."
|
||||
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 = {
|
||||
"message": f"{user.email} was added!",
|
||||
|
|
|
@ -22,6 +22,25 @@ async def create_users(user: UserRegister, req: Request):
|
|||
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)
|
||||
async def get_user(
|
||||
id: str, req: Request, authorization: Annotated[str | None, Header()] = None
|
||||
|
|
Loading…
Reference in New Issue