points added; rating still isn't fixed

This commit is contained in:
2023-08-08 19:00:06 +03:00
parent 5bdad31dae
commit bd863bc911
6 changed files with 69 additions and 20 deletions

View File

@ -135,7 +135,7 @@ def create_user(nickname: Annotated[str, Form()], password: Annotated[str, Form(
if database.query(models.User).filter(models.User.nickname == nickname).first() == None:
new_user = models.User(nickname=nickname, hashed_password=utils.get_password_hash(password),
name=name, surname=surname)
name=name, surname=surname, reg_date=datetime.date.today())
database.add(new_user)
database.commit()
database.refresh(new_user) # обновляем состояние объекта
@ -173,26 +173,27 @@ async def read_own_items(
return [{"Current user name": current_user.name, "Current user surname": current_user.surname}]
# начисляем баллы пользователю
# изменяем рейтинг пользователя
@app.post("/api/user/rating")
def add_points(data: schemas.AddPoints, current_user: Annotated[schemas.User, Depends(utils.get_current_user)]):
def add_points(data: schemas.AddRating, current_user: Annotated[schemas.User, Depends(utils.get_current_user)]):
if current_user.id != data.user_id:
user = utils.get_user(data.user_id)
user = utils.get_user_by_id(db=database, user_id=data.user_id)
if not user:
raise HTTPException(status_code=404, detail="Item not found")
user.rating = (user.rating*user.num_of_ratings + data.rate)/(user.num_of_ratings + 1)
user.num_of_ratings += 1
user.rating = (user.rating + data.rate)/user.num_of_ratings
return {"Success": True}
# получаем данные о баллах пользователя
# получаем рейтинг пользователя
@app.get("/api/user/rating")
def add_points(user_id: int):
user = utils.get_user(user_id)
user = utils.get_user_by_id(db=database, user_id=user_id)
if not user:
raise HTTPException(status_code=404, detail="Item not found")
return {"rating": user.rating}
# Отправляем стихи
@app.get("/api/user/poem") # пока не работает
def poems_to_front(): # db: Annotated[Session, Depends(utils.get_db)]
@ -244,5 +245,11 @@ async def react_app(req: Request, rest_of_path: str):
@app.post("api/announcement/dispose")
def dispose(despose_data: schemas.DisposeData, current_user: Annotated[schemas.User, Depends(utils.get_current_user)]):
current_user.rating += 60
def dispose(data: schemas.TrashboxResponse, current_user: Annotated[schemas.User, Depends(utils.get_current_user)]):
current_user.points += 60
new_trashox = models.Trashbox(user_id=current_user.id, date_of_chose=datetime.date.today(), name=data.Name,
latitude=data.Lat, longtitude=data.Lng, address=data.Address, category=data.Categories)
database.add(new_trashox)
database.commit()
database.refresh(new_trashox) # обновляем состояние объекта