diff --git a/src/backend/app/app/api/api_v1/endpoints/items.py b/src/backend/app/app/api/api_v1/endpoints/items.py index c72a28fd7f..e254cda368 100644 --- a/src/backend/app/app/api/api_v1/endpoints/items.py +++ b/src/backend/app/app/api/api_v1/endpoints/items.py @@ -4,7 +4,7 @@ from sqlmodel import select from app.api.deps import CurrentUser, SessionDep -from app.models import Item, ItemCreate, ItemOut, ItemUpdate +from app.models import Item, ItemCreate, ItemOut, ItemUpdate, Message router = APIRouter() @@ -78,8 +78,8 @@ def update_item( return item -@router.delete("/{id}", response_model=ItemOut) -def delete_item(session: SessionDep, current_user: CurrentUser, id: int) -> Any: +@router.delete("/{id}") +def delete_item(session: SessionDep, current_user: CurrentUser, id: int) -> Message: """ Delete an item. """ @@ -90,4 +90,4 @@ def delete_item(session: SessionDep, current_user: CurrentUser, id: int) -> Any: raise HTTPException(status_code=400, detail="Not enough permissions") session.delete(item) session.commit() - return item + return Message(message="Item deleted successfully") diff --git a/src/backend/app/app/api/api_v1/endpoints/users.py b/src/backend/app/app/api/api_v1/endpoints/users.py index f16a77d385..e3ec0fd34f 100644 --- a/src/backend/app/app/api/api_v1/endpoints/users.py +++ b/src/backend/app/app/api/api_v1/endpoints/users.py @@ -11,6 +11,7 @@ ) from app.core.config import settings from app.models import ( + Message, User, UserCreate, UserCreateOpen, @@ -151,3 +152,24 @@ def update_user( # ) # user = crud.user.update(session, db_obj=user, obj_in=user_in) # return user + + +@router.delete("/{user_id}") +def delete_user( + session: SessionDep, current_user: CurrentUser, user_id: int +) -> Message: + """ + Delete a user. + """ + user = session.get(User, user_id) + if not user: + raise HTTPException(status_code=404, detail="User not found") + if not current_user.is_superuser: + raise HTTPException(status_code=400, detail="Not enough permissions") + if user == current_user: + raise HTTPException( + status_code=400, detail="Users are not allowed to delete themselves" + ) + session.delete(user) + session.commit() + return Message(message="User deleted successfully") diff --git a/src/new-frontend/src/client/services/ItemsService.ts b/src/new-frontend/src/client/services/ItemsService.ts index 2f9ed3cad7..0f46422dd3 100644 --- a/src/new-frontend/src/client/services/ItemsService.ts +++ b/src/new-frontend/src/client/services/ItemsService.ts @@ -5,6 +5,7 @@ import type { ItemCreate } from '../models/ItemCreate'; import type { ItemOut } from '../models/ItemOut'; import type { ItemUpdate } from '../models/ItemUpdate'; +import type { Message } from '../models/Message'; import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; @@ -113,14 +114,14 @@ requestBody: ItemUpdate, /** * Delete Item * Delete an item. - * @returns ItemOut Successful Response + * @returns Message Successful Response * @throws ApiError */ public static deleteItem({ id, }: { id: number, -}): CancelablePromise { +}): CancelablePromise { return __request(OpenAPI, { method: 'DELETE', url: '/api/v1/items/{id}', diff --git a/src/new-frontend/src/client/services/UsersService.ts b/src/new-frontend/src/client/services/UsersService.ts index 759f94279f..b5911d69f6 100644 --- a/src/new-frontend/src/client/services/UsersService.ts +++ b/src/new-frontend/src/client/services/UsersService.ts @@ -2,6 +2,7 @@ /* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ +import type { Message } from '../models/Message'; import type { UserCreate } from '../models/UserCreate'; import type { UserCreateOpen } from '../models/UserCreateOpen'; import type { UserOut } from '../models/UserOut'; @@ -169,4 +170,27 @@ requestBody: UserUpdate, }); } + /** + * Delete User + * Delete a user. + * @returns Message Successful Response + * @throws ApiError + */ + public static deleteUser({ +userId, +}: { +userId: number, +}): CancelablePromise { + return __request(OpenAPI, { + method: 'DELETE', + url: '/api/v1/users/{user_id}', + path: { + 'user_id': userId, + }, + errors: { + 422: `Validation Error`, + }, + }); + } + } diff --git a/src/new-frontend/src/store/users-store.tsx b/src/new-frontend/src/store/users-store.tsx index 5a0af29e9b..eb3ee61e76 100644 --- a/src/new-frontend/src/store/users-store.tsx +++ b/src/new-frontend/src/store/users-store.tsx @@ -5,6 +5,7 @@ interface UsersStore { users: UserOut[]; getUsers: () => Promise; addUser: (user: UserCreate) => Promise; + deleteUser: (id: number) => Promise; } export const useUsersStore = create((set) => ({ @@ -17,5 +18,8 @@ export const useUsersStore = create((set) => ({ const userResponse = await UsersService.createUser({ requestBody: user }); set((state) => ({ users: [...state.users, userResponse] })); }, - // TODO: Add delete user + deleteUser: async (id: number) => { + await UsersService.deleteUser({ userId: id }); + set((state) => ({ users: state.users.filter((user) => user.id !== id) })); + } })) \ No newline at end of file