Spaces:
Sleeping
Sleeping
| """ | |
| Points Service - Gamification and rewards tracking. | |
| Point values: | |
| - Upload content: +10 | |
| - Content verified: +20 | |
| - Content helped someone (worked=True): +5 | |
| - Gave feedback: +2 | |
| """ | |
| from typing import List | |
| from sqlalchemy.orm import Session | |
| from sqlalchemy import func | |
| from uuid import UUID | |
| from app.models.points import PointsHistory | |
| class PointsService: | |
| """Handles points and rewards for teachers.""" | |
| # Point values for different actions | |
| POINTS_UPLOAD = 10 | |
| POINTS_VERIFIED = 20 | |
| POINTS_HELPED = 5 | |
| POINTS_FEEDBACK = 2 | |
| def add_points( | |
| db: Session, | |
| teacher_id: UUID, | |
| points: int, | |
| reason: str | |
| ) -> PointsHistory: | |
| """Add points to a teacher's account.""" | |
| entry = PointsHistory( | |
| teacher_id=teacher_id, | |
| points=points, | |
| reason=reason | |
| ) | |
| db.add(entry) | |
| db.commit() | |
| db.refresh(entry) | |
| return entry | |
| def get_total_points(db: Session, teacher_id: UUID) -> int: | |
| """Get total points for a teacher.""" | |
| total = db.query(func.sum(PointsHistory.points)).filter( | |
| PointsHistory.teacher_id == teacher_id | |
| ).scalar() | |
| return total or 0 | |
| def get_points_history( | |
| db: Session, | |
| teacher_id: UUID, | |
| limit: int = 50 | |
| ) -> List[PointsHistory]: | |
| """Get points history for a teacher.""" | |
| return db.query(PointsHistory).filter( | |
| PointsHistory.teacher_id == teacher_id | |
| ).order_by( | |
| PointsHistory.created_at.desc() | |
| ).limit(limit).all() | |
| def award_upload_points(db: Session, teacher_id: UUID) -> PointsHistory: | |
| """Award points for uploading content.""" | |
| return PointsService.add_points( | |
| db, teacher_id, PointsService.POINTS_UPLOAD, "upload" | |
| ) | |
| def award_verification_points(db: Session, teacher_id: UUID) -> PointsHistory: | |
| """Award points when content is verified.""" | |
| return PointsService.add_points( | |
| db, teacher_id, PointsService.POINTS_VERIFIED, "verification" | |
| ) | |
| def award_helped_points(db: Session, teacher_id: UUID) -> PointsHistory: | |
| """Award points when content helped someone.""" | |
| return PointsService.add_points( | |
| db, teacher_id, PointsService.POINTS_HELPED, "helped" | |
| ) | |
| def award_feedback_points(db: Session, teacher_id: UUID) -> PointsHistory: | |
| """Award points for giving feedback.""" | |
| return PointsService.add_points( | |
| db, teacher_id, PointsService.POINTS_FEEDBACK, "feedback" | |
| ) | |