|
1 | 1 | from typing import Optional |
2 | 2 | from fastapi import APIRouter, HTTPException, Query |
3 | 3 | from sqlmodel import func, or_, select, delete as sqlmodel_delete |
4 | | -from apps.system.crud.user import check_account_exists, get_db_user, single_delete, user_ws_options |
| 4 | +from apps.system.crud.user import check_account_exists, check_email_exists, get_db_user, single_delete, user_ws_options |
5 | 5 | from apps.system.models.system_model import UserWsModel |
6 | 6 | from apps.system.models.user import UserModel |
7 | 7 | from apps.system.schemas.auth import CacheName, CacheNamespace |
@@ -116,9 +116,10 @@ async def query(session: SessionDep, trans: Trans, id: int) -> UserEditor: |
116 | 116 |
|
117 | 117 | @router.post("") |
118 | 118 | async def create(session: SessionDep, creator: UserCreator): |
119 | | - count = check_account_exists(session=session, account=creator.account) |
120 | | - if count > 0: |
| 119 | + if check_account_exists(session=session, account=creator.account): |
121 | 120 | raise Exception(f"Account [{creator.account}] already exists!") |
| 121 | + if check_email_exists(session=session, email=creator.email): |
| 122 | + raise Exception(f"Email [{creator.email}] already exists!") |
122 | 123 | data = creator.model_dump(exclude_unset=True) |
123 | 124 | user_model = UserModel.model_validate(data) |
124 | 125 | #user_model.create_time = get_timestamp() |
@@ -147,6 +148,8 @@ async def update(session: SessionDep, editor: UserEditor): |
147 | 148 | raise Exception(f"User with id [{editor.id}] not found!") |
148 | 149 | if editor.account != user_model.account: |
149 | 150 | raise Exception(f"account cannot be changed!") |
| 151 | + if editor.email != user_model.email and check_email_exists(session=session, account=editor.email): |
| 152 | + raise Exception(f"Email [{editor.email}] already exists!") |
150 | 153 | origin_oid: int = user_model.oid |
151 | 154 | del_stmt = sqlmodel_delete(UserWsModel).where(UserWsModel.uid == editor.id) |
152 | 155 | session.exec(del_stmt) |
|
0 commit comments