Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add delete_user; refactor delete_item #594

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/backend/app/app/api/api_v1/endpoints/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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.
"""
Expand All @@ -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")
22 changes: 22 additions & 0 deletions src/backend/app/app/api/api_v1/endpoints/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from app.core.config import settings
from app.models import (
Message,
User,
UserCreate,
UserCreateOpen,
Expand Down Expand Up @@ -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")
5 changes: 3 additions & 2 deletions src/new-frontend/src/client/services/ItemsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<ItemOut> {
}): CancelablePromise<Message> {
return __request(OpenAPI, {
method: 'DELETE',
url: '/api/v1/items/{id}',
Expand Down
24 changes: 24 additions & 0 deletions src/new-frontend/src/client/services/UsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -169,4 +170,27 @@ requestBody: UserUpdate,
});
}

/**
* Delete User
* Delete a user.
* @returns Message Successful Response
* @throws ApiError
*/
public static deleteUser({
userId,
}: {
userId: number,
}): CancelablePromise<Message> {
return __request(OpenAPI, {
method: 'DELETE',
url: '/api/v1/users/{user_id}',
path: {
'user_id': userId,
},
errors: {
422: `Validation Error`,
},
});
}

}
6 changes: 5 additions & 1 deletion src/new-frontend/src/store/users-store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface UsersStore {
users: UserOut[];
getUsers: () => Promise<void>;
addUser: (user: UserCreate) => Promise<void>;
deleteUser: (id: number) => Promise<void>;
}

export const useUsersStore = create<UsersStore>((set) => ({
Expand All @@ -17,5 +18,8 @@ export const useUsersStore = create<UsersStore>((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) }));
}
}))