refaktorálás javításai
This commit is contained in:
142
backend/app/api/v1/endpoints/vehicles.py
Normal file
142
backend/app/api/v1/endpoints/vehicles.py
Normal file
@@ -0,0 +1,142 @@
|
||||
"""
|
||||
Jármű értékelési végpontok a Social 1 modulhoz.
|
||||
"""
|
||||
import uuid
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, and_
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.db.session import get_db
|
||||
from app.api.deps import get_current_user
|
||||
from app.models.vehicle import VehicleUserRating
|
||||
from app.models.vehicle_definitions import VehicleModelDefinition
|
||||
from app.models.identity import User
|
||||
from app.schemas.vehicle import VehicleRatingCreate, VehicleRatingResponse
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/{vehicle_id}/ratings", response_model=VehicleRatingResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_vehicle_rating(
|
||||
vehicle_id: int,
|
||||
rating: VehicleRatingCreate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
Értékelés beküldése egy járműhöz.
|
||||
Csak a jármű tulajdonosa (vagy jogosult felhasználó) értékelhet.
|
||||
Egy felhasználó csak egyszer értékelhet egy adott járművet.
|
||||
"""
|
||||
# 1. Ellenőrizzük, hogy a jármű létezik-e
|
||||
vehicle = await db.scalar(
|
||||
select(VehicleModelDefinition).where(VehicleModelDefinition.id == vehicle_id)
|
||||
)
|
||||
if not vehicle:
|
||||
raise HTTPException(status_code=404, detail="Jármű nem található")
|
||||
|
||||
# 2. Ellenőrizzük, hogy a felhasználó jogosult-e értékelni (jelenleg csak tulajdonos)
|
||||
# TODO: Később kibővíthető más jogosultságokkal is
|
||||
# Most feltételezzük, hogy mindenki értékelhet, de csak egyszer
|
||||
|
||||
# 3. Ellenőrizzük, hogy már létezik-e értékelés ettől a felhasználótól ehhez a járműhöz
|
||||
existing_rating = await db.scalar(
|
||||
select(VehicleUserRating).where(
|
||||
and_(
|
||||
VehicleUserRating.vehicle_id == vehicle_id,
|
||||
VehicleUserRating.user_id == current_user.id
|
||||
)
|
||||
)
|
||||
)
|
||||
if existing_rating:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Már értékelted ezt a járművet. Csak egy értékelés engedélyezett felhasználónként."
|
||||
)
|
||||
|
||||
# 4. Hozzuk létre az új értékelést
|
||||
new_rating = VehicleUserRating(
|
||||
vehicle_id=vehicle_id,
|
||||
user_id=current_user.id,
|
||||
driving_experience=rating.driving_experience,
|
||||
reliability=rating.reliability,
|
||||
comfort=rating.comfort,
|
||||
consumption_satisfaction=rating.consumption_satisfaction,
|
||||
comment=rating.comment
|
||||
)
|
||||
|
||||
db.add(new_rating)
|
||||
await db.commit()
|
||||
await db.refresh(new_rating)
|
||||
|
||||
# 5. Átlagpontszám számítása
|
||||
average_score = new_rating.average_score
|
||||
|
||||
# 6. Válasz összeállítása
|
||||
return VehicleRatingResponse(
|
||||
id=new_rating.id,
|
||||
vehicle_id=new_rating.vehicle_id,
|
||||
user_id=new_rating.user_id,
|
||||
driving_experience=new_rating.driving_experience,
|
||||
reliability=new_rating.reliability,
|
||||
comfort=new_rating.comfort,
|
||||
consumption_satisfaction=new_rating.consumption_satisfaction,
|
||||
comment=new_rating.comment,
|
||||
average_score=average_score,
|
||||
created_at=new_rating.created_at,
|
||||
updated_at=new_rating.updated_at
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{vehicle_id}/ratings", response_model=List[VehicleRatingResponse])
|
||||
async def get_vehicle_ratings(
|
||||
vehicle_id: int,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
Az összes értékelés lekérése egy adott járműhöz.
|
||||
"""
|
||||
# Ellenőrizzük, hogy a jármű létezik-e
|
||||
vehicle = await db.scalar(
|
||||
select(VehicleModelDefinition).where(VehicleModelDefinition.id == vehicle_id)
|
||||
)
|
||||
if not vehicle:
|
||||
raise HTTPException(status_code=404, detail="Jármű nem található")
|
||||
|
||||
# Lekérjük az értékeléseket
|
||||
stmt = (
|
||||
select(VehicleUserRating)
|
||||
.where(VehicleUserRating.vehicle_id == vehicle_id)
|
||||
.order_by(VehicleUserRating.created_at.desc())
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
)
|
||||
|
||||
result = await db.scalars(stmt)
|
||||
ratings = result.all()
|
||||
|
||||
# Átalakítás válasz sémává
|
||||
response_ratings = []
|
||||
for rating in ratings:
|
||||
response_ratings.append(
|
||||
VehicleRatingResponse(
|
||||
id=rating.id,
|
||||
vehicle_id=rating.vehicle_id,
|
||||
user_id=rating.user_id,
|
||||
driving_experience=rating.driving_experience,
|
||||
reliability=rating.reliability,
|
||||
comfort=rating.comfort,
|
||||
consumption_satisfaction=rating.consumption_satisfaction,
|
||||
comment=rating.comment,
|
||||
average_score=rating.average_score,
|
||||
created_at=rating.created_at,
|
||||
updated_at=rating.updated_at
|
||||
)
|
||||
)
|
||||
|
||||
return response_ratings
|
||||
Reference in New Issue
Block a user