From 7a5be4021b85d6e0a70da918285b83cbc3c374c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandra=20S=C3=A1nchez?= Date: Tue, 13 Feb 2024 09:13:40 -0500 Subject: [PATCH 1/4] Add delete_user and refactor delete_item --- .../app/app/api/api_v1/endpoints/items.py | 8 +++---- .../app/app/api/api_v1/endpoints/users.py | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) 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") From 5630321bdc2e08eac890436b7a2439ff8e5422a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandra=20S=C3=A1nchez?= Date: Tue, 13 Feb 2024 09:14:36 -0500 Subject: [PATCH 2/4] Regenerate client for new-frontend --- .../src/client/services/UsersService.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/new-frontend/src/client/services/UsersService.ts b/src/new-frontend/src/client/services/UsersService.ts index 759f94279f..d7a485f50e 100644 --- a/src/new-frontend/src/client/services/UsersService.ts +++ b/src/new-frontend/src/client/services/UsersService.ts @@ -169,4 +169,27 @@ requestBody: UserUpdate, }); } + /** + * Delete User + * Delete a user. + * @returns UserOut 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`, + }, + }); + } + } From 5b0d001e806fef43d085d397b550d2d374d2da02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandra=20S=C3=A1nchez?= Date: Tue, 13 Feb 2024 09:18:26 -0500 Subject: [PATCH 3/4] Add deleteUser to users-store --- src/new-frontend/src/store/users-store.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 From 5841344fbbe2d66fe039fb24c4bd56681509954d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandra=20S=C3=A1nchez?= Date: Tue, 13 Feb 2024 09:25:10 -0500 Subject: [PATCH 4/4] Regerate client for new-frontend --- src/new-frontend/src/client/services/ItemsService.ts | 5 +++-- src/new-frontend/src/client/services/UsersService.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) 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 d7a485f50e..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'; @@ -172,14 +173,14 @@ requestBody: UserUpdate, /** * Delete User * Delete a user. - * @returns UserOut Successful Response + * @returns Message Successful Response * @throws ApiError */ public static deleteUser({ userId, }: { userId: number, -}): CancelablePromise { +}): CancelablePromise { return __request(OpenAPI, { method: 'DELETE', url: '/api/v1/users/{user_id}',