Spaces:
Sleeping
Sleeping
Create app/services/suggestion_service.py
Browse files
app/services/suggestion_service.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
|
| 4 |
+
from app.models.suggestion import Suggestion
|
| 5 |
+
from app.schemas.suggestion import SuggestionCreate
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def create_suggestion(db: Session, data: SuggestionCreate) -> Suggestion:
|
| 9 |
+
translations_str = ",".join(data.translations) if data.translations else ""
|
| 10 |
+
suggestion = Suggestion(
|
| 11 |
+
headword_zomi=data.headwordZomi,
|
| 12 |
+
headword_english=data.headwordEnglish,
|
| 13 |
+
translations=translations_str,
|
| 14 |
+
part_of_speech=data.partOfSpeech,
|
| 15 |
+
definition=data.definition,
|
| 16 |
+
contributor_id=data.contributorId,
|
| 17 |
+
)
|
| 18 |
+
db.add(suggestion)
|
| 19 |
+
db.commit()
|
| 20 |
+
db.refresh(suggestion)
|
| 21 |
+
return suggestion
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def get_suggestions_by_user(db: Session, user_id: str) -> List[Suggestion]:
|
| 25 |
+
return db.query(Suggestion).filter(Suggestion.contributor_id == user_id).all()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def vote_on_suggestion(db: Session, suggestion_id: int, is_upvote: bool) -> Suggestion | None:
|
| 29 |
+
suggestion = db.query(Suggestion).filter(Suggestion.id == suggestion_id).first()
|
| 30 |
+
if not suggestion:
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
if is_upvote:
|
| 34 |
+
suggestion.upvotes += 1
|
| 35 |
+
else:
|
| 36 |
+
suggestion.downvotes += 1
|
| 37 |
+
|
| 38 |
+
db.commit()
|
| 39 |
+
db.refresh(suggestion)
|
| 40 |
+
return suggestion
|