-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feat/test achievements #56
Changes from all commits
884eba7
dc1c710
ba91414
fc7629d
0b414f2
fc9c8e2
a363ac8
afae6f4
4481485
6d1e2e3
2a4c024
24d418e
879aae1
9e084c3
ead1366
5afb149
0e4842d
da5e065
35d5af5
7a023c4
204482f
587c5c8
b658d8b
3bd713c
4a55710
bca4e5f
b504d47
30643a1
46b3a66
8da75b4
2a49ebe
b166e52
c4c94a7
b7862d0
e9e7f70
771eee7
5c24126
1321f84
fc0098d
1988797
5bb1292
e8f0910
496f6f2
fd61fa5
9774e9c
c9a23a7
f0eed70
1da61dd
8717665
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,83 @@ | ||
from fastapi import APIRouter, Depends | ||
from fastapi import APIRouter, Depends, HTTPException, Response, status | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from app.api.validators import check_name_duplicate | ||
from app.core.db import get_async_session | ||
from app.core.user import current_superuser, current_user | ||
from app.crud import achievement_crud | ||
from app.schemas.achievement import AchievementCreate, AchievementRead | ||
from app.models import Achievement, User | ||
from app.schemas.achievement import (AchievementCreate, AchievementRead, | ||
AchievementUpdate) | ||
from app.services.endpoints_services import delete_obj | ||
from app.services.utils import (Pagination, add_response_headers, | ||
get_pagination_params, paginated) | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get( | ||
"/", | ||
response_model=list[AchievementRead], | ||
dependencies=[Depends(current_user)] | ||
dependencies=[Depends(current_superuser)] | ||
) | ||
async def get_all_achievements( | ||
response: Response, | ||
pagination: Pagination = Depends(get_pagination_params), | ||
session: AsyncSession = Depends(get_async_session), | ||
) -> list[AchievementRead]: | ||
"""Возвращает все achievement.""" | ||
return await achievement_crud.get_multi(session) | ||
achievements = await achievement_crud.get_multi(session) | ||
response = add_response_headers( | ||
response, achievements, pagination | ||
) | ||
return paginated(achievements, pagination) | ||
|
||
|
||
@router.get( | ||
'/me', | ||
response_model=list[AchievementRead], | ||
dependencies=[Depends(current_user)] | ||
) | ||
async def get_self_achievements( | ||
user: User = Depends(current_user), | ||
session: AsyncSession = Depends(get_async_session) | ||
): | ||
"""Возвращает ачивментс юзера.""" | ||
return await achievement_crud.get_users_obj(user.id, session) | ||
|
||
|
||
@router.get( | ||
'/me/{achievement_id}', | ||
response_model=AchievementRead, | ||
dependencies=[Depends(current_user)] | ||
) | ||
async def get_self_achievement_by_id( | ||
achievement_id: int, | ||
user: User = Depends(current_user), | ||
session: AsyncSession = Depends(get_async_session) | ||
): | ||
"""Возвращает ачивмент юзера по id.""" | ||
achievement: Achievement = await achievement_crud.get( | ||
achievement_id, session | ||
) | ||
if achievement is None: | ||
raise HTTPException( | ||
status_code=404, | ||
detail='Achievement не существует.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 404 заменить на fastapi status |
||
) | ||
if user.id not in [_.id for _ in achievement.profiles]: | ||
raise HTTPException( | ||
status_code=403, | ||
detail='У выс нет этого achievement.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fastapi status |
||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Небольшая опечатка, заменить на 'вас' |
||
return achievement | ||
|
||
|
||
@router.post( | ||
"/", | ||
response_model=AchievementRead, | ||
dependencies=[Depends(current_superuser)] | ||
dependencies=[Depends(current_superuser)], | ||
status_code=status.HTTP_201_CREATED | ||
) | ||
async def create_achievement( | ||
achievement: AchievementCreate, | ||
|
@@ -37,7 +88,25 @@ async def create_achievement( | |
return await achievement_crud.create(obj_in=achievement, session=session) | ||
|
||
|
||
@router.delete("/{obj_id}", dependencies=[Depends(current_superuser)]) | ||
@router.patch( | ||
'/{achievement_id}', | ||
response_model=AchievementRead, | ||
dependencies=[Depends(current_superuser)] | ||
) | ||
async def update_achievement( | ||
achievement_id: int, | ||
data: AchievementUpdate, | ||
session: AsyncSession = Depends(get_async_session) | ||
): | ||
"""Апдейт ачивмент.""" | ||
_achievement = await achievement_crud.get(achievement_id, session) | ||
return await achievement_crud.update( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо при апдейте добавить check_obj_exists для проверки, есть ли объект в бд |
||
_achievement, data, session | ||
) | ||
|
||
|
||
@router.delete("/{obj_id}", dependencies=[Depends(current_superuser)], | ||
status_code=status.HTTP_204_NO_CONTENT) | ||
async def delete_achievement( | ||
obj_id: int, | ||
session: AsyncSession = Depends(get_async_session), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,33 @@ | ||
from sqlalchemy import select | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy.orm import selectinload | ||
|
||
from app.crud.base import CRUDBase | ||
from app.models import Achievement | ||
from app.models import Achievement, Profile | ||
|
||
|
||
class CRUDAchievement(CRUDBase): | ||
pass | ||
async def get(self, obj_id: int, session: AsyncSession): | ||
stmt = ( | ||
select(Achievement) | ||
.where(Achievement.id == obj_id) | ||
.options( | ||
selectinload(Achievement.profiles) | ||
) | ||
) | ||
achievement = await session.execute(stmt) | ||
achievement = achievement.scalars().first() | ||
return achievement | ||
|
||
async def get_users_obj(self, user_id: int, session: AsyncSession): | ||
stmt = ( | ||
select(Achievement) | ||
.options( | ||
selectinload(Achievement.profiles) | ||
).where(Achievement.profiles.any(Profile.user_id == user_id)) | ||
) | ||
db_obj = await session.execute(stmt) | ||
return db_obj.scalars().all() | ||
|
||
|
||
achievement_crud = CRUDAchievement(Achievement) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
from sqlalchemy import select | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy.orm import selectinload | ||
|
||
from app.crud.base import CRUDBase | ||
from app.models import Group | ||
from app.models import Group, User | ||
|
||
|
||
class CRUDGroup(CRUDBase): | ||
pass | ||
async def get(self, group_id: int, session: AsyncSession): | ||
stmt = ( | ||
select(Group) | ||
.where(Group.id == group_id) | ||
.options( | ||
selectinload(Group.users) | ||
) | ||
) | ||
group = await session.execute(stmt) | ||
group = group.scalars().first() | ||
return group | ||
|
||
async def get_users_obj(self, user_id: int, session: AsyncSession): | ||
stmt = ( | ||
select(Group) | ||
# .where(Group.users == user_id) | ||
.options( | ||
selectinload(Group.users) | ||
).where(Group.users.any(User.id == user_id)) | ||
) | ||
db_obj = await session.execute(stmt) | ||
return db_obj.scalars().all() | ||
|
||
|
||
group_crud = CRUDGroup(Group) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Думаю здесь тоже потребуется пагинация