-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #52 created missing repositories
- update also endpoints - change api welcome message
- Loading branch information
Showing
10 changed files
with
424 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# app/api/endpoints/activity.py | ||
|
||
from fastapi import ( | ||
APIRouter, | ||
Depends, | ||
HTTPException, | ||
) | ||
from sqlalchemy.orm import Session | ||
from app.database.session import get_db | ||
from app.schemas.activity import ( | ||
ActivityCreate, | ||
ActivityUpdate, | ||
ActivityInDBBase, | ||
) | ||
from app.repositories.activity_repository import ( | ||
create_activity, | ||
get_activity, | ||
get_activities, | ||
update_activity, | ||
delete_activity, | ||
) | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/activities/", response_model=ActivityInDBBase) | ||
def create_activity_endpoint( | ||
activity: ActivityCreate, db: Session = Depends(get_db) | ||
) -> ActivityInDBBase: | ||
return create_activity(db, activity) | ||
|
||
|
||
@router.get("/activities/{activity_id}", response_model=ActivityInDBBase) | ||
def read_activity(activity_id: int, db: Session = Depends(get_db)) -> ActivityInDBBase: | ||
activity = get_activity(db, activity_id) | ||
if activity is None: | ||
raise HTTPException(status_code=404, detail="Activity not found") | ||
return activity | ||
|
||
|
||
@router.get("/activities/", response_model=list[ActivityInDBBase]) | ||
def read_activities( | ||
skip: int = 0, limit: int = 10, db: Session = Depends(get_db) | ||
) -> list[ActivityInDBBase]: | ||
return get_activities(db, skip, limit) | ||
|
||
|
||
@router.put("/activities/{activity_id}", response_model=ActivityInDBBase) | ||
def update_activity_endpoint( | ||
activity_id: int, activity: ActivityUpdate, db: Session = Depends(get_db) | ||
) -> ActivityInDBBase: | ||
db_activity = get_activity(db, activity_id) | ||
if db_activity is None: | ||
raise HTTPException(status_code=404, detail="Activity not found") | ||
return update_activity(db, activity_id, activity.model_dump()) | ||
|
||
|
||
@router.delete("/activities/{activity_id}", response_model=ActivityInDBBase) | ||
def delete_activity_endpoint( | ||
activity_id: int, db: Session = Depends(get_db) | ||
) -> ActivityInDBBase: | ||
activity = delete_activity(db, activity_id) | ||
if activity is None: | ||
raise HTTPException(status_code=404, detail="Activity not found") | ||
return activity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# app/api/endpoints/answer.py | ||
|
||
from fastapi import ( | ||
APIRouter, | ||
Depends, | ||
HTTPException, | ||
) | ||
from sqlalchemy.orm import Session | ||
from app.database.session import get_db | ||
from app.schemas.answer import ( | ||
AnswerCreate, | ||
AnswerUpdate, | ||
AnswerInDBBase, | ||
) | ||
from app.repositories.answer_repository import ( | ||
create_answer, | ||
get_answer, | ||
get_answers, | ||
update_answer, | ||
delete_answer, | ||
) | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/answers/", response_model=AnswerInDBBase) | ||
def create_answer_endpoint( | ||
answer: AnswerCreate, db: Session = Depends(get_db) | ||
) -> AnswerInDBBase: | ||
return create_answer(db, answer) | ||
|
||
|
||
@router.get("/answers/{answer_id}", response_model=AnswerInDBBase) | ||
def read_answer(answer_id: int, db: Session = Depends(get_db)) -> AnswerInDBBase: | ||
answer = get_answer(db, answer_id) | ||
if answer is None: | ||
raise HTTPException(status_code=404, detail="Answer not found") | ||
return answer | ||
|
||
|
||
@router.get("/answers/", response_model=list[AnswerInDBBase]) | ||
def read_answers( | ||
skip: int = 0, limit: int = 10, db: Session = Depends(get_db) | ||
) -> list[AnswerInDBBase]: | ||
return get_answers(db, skip, limit) | ||
|
||
|
||
@router.put("/answers/{answer_id}", response_model=AnswerInDBBase) | ||
def update_answer_endpoint( | ||
answer_id: int, answer: AnswerUpdate, db: Session = Depends(get_db) | ||
) -> AnswerInDBBase: | ||
db_answer = get_answer(db, answer_id) | ||
if db_answer is None: | ||
raise HTTPException(status_code=404, detail="Answer not found") | ||
return update_answer(db, answer_id, answer.model_dump()) | ||
|
||
|
||
@router.delete("/answers/{answer_id}", response_model=AnswerInDBBase) | ||
def delete_answer_endpoint( | ||
answer_id: int, db: Session = Depends(get_db) | ||
) -> AnswerInDBBase: | ||
answer = delete_answer(db, answer_id) | ||
if answer is None: | ||
raise HTTPException(status_code=404, detail="Answer not found") | ||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# app/api/endpoints/question.py | ||
|
||
from fastapi import ( | ||
APIRouter, | ||
Depends, | ||
HTTPException, | ||
) | ||
from sqlalchemy.orm import Session | ||
from app.database.session import get_db | ||
from app.schemas.question import ( | ||
QuestionCreate, | ||
QuestionUpdate, | ||
QuestionInDBBase, | ||
) | ||
from app.repositories.question_repository import ( | ||
create_question, | ||
get_question, | ||
get_questions, | ||
update_question, | ||
delete_question, | ||
) | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/questions/", response_model=QuestionInDBBase) | ||
def create_question_endpoint( | ||
question: QuestionCreate, db: Session = Depends(get_db) | ||
) -> QuestionInDBBase: | ||
return create_question(db, question) | ||
|
||
|
||
@router.get("/questions/{question_id}", response_model=QuestionInDBBase) | ||
def read_question(question_id: int, db: Session = Depends(get_db)) -> QuestionInDBBase: | ||
question = get_question(db, question_id) | ||
if question is None: | ||
raise HTTPException(status_code=404, detail="Question not found") | ||
return question | ||
|
||
|
||
@router.get("/questions/", response_model=list[QuestionInDBBase]) | ||
def read_questions( | ||
skip: int = 0, limit: int = 10, db: Session = Depends(get_db) | ||
) -> list[QuestionInDBBase]: | ||
return get_questions(db, skip, limit) | ||
|
||
|
||
@router.put("/questions/{question_id}", response_model=QuestionInDBBase) | ||
def update_question_endpoint( | ||
question_id: int, question: QuestionUpdate, db: Session = Depends(get_db) | ||
) -> QuestionInDBBase: | ||
db_question = get_question(db, question_id) | ||
if db_question is None: | ||
raise HTTPException(status_code=404, detail="Question not found") | ||
return update_question(db, question_id, question.model_dump()) | ||
|
||
|
||
@router.delete("/questions/{question_id}", response_model=QuestionInDBBase) | ||
def delete_question_endpoint( | ||
question_id: int, db: Session = Depends(get_db) | ||
) -> QuestionInDBBase: | ||
question = delete_question(db, question_id) | ||
if question is None: | ||
raise HTTPException(status_code=404, detail="Question not found") | ||
return question |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# app/api/endpoints/study_session.py | ||
|
||
from fastapi import ( | ||
APIRouter, | ||
Depends, | ||
HTTPException, | ||
) | ||
from sqlalchemy.orm import Session | ||
from app.database.session import get_db | ||
from app.schemas.study_session import ( | ||
StudySessionCreate, | ||
StudySessionUpdate, | ||
StudySessionInDBBase, | ||
) | ||
from app.repositories.study_session_repository import ( | ||
create_study_session, | ||
get_study_session, | ||
get_study_sessions, | ||
update_study_session, | ||
delete_study_session, | ||
) | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/study_sessions/", response_model=StudySessionInDBBase) | ||
def create_study_session_endpoint( | ||
study_session: StudySessionCreate, db: Session = Depends(get_db) | ||
) -> StudySessionInDBBase: | ||
return create_study_session(db, study_session) | ||
|
||
|
||
@router.get("/study_sessions/{study_session_id}", response_model=StudySessionInDBBase) | ||
def read_study_session( | ||
study_session_id: int, db: Session = Depends(get_db) | ||
) -> StudySessionInDBBase: | ||
study_session = get_study_session(db, study_session_id) | ||
if study_session is None: | ||
raise HTTPException(status_code=404, detail="StudySession not found") | ||
return study_session | ||
|
||
|
||
@router.get("/study_sessions/", response_model=list[StudySessionInDBBase]) | ||
def read_study_sessions( | ||
skip: int = 0, limit: int = 10, db: Session = Depends(get_db) | ||
) -> list[StudySessionInDBBase]: | ||
return get_study_sessions(db, skip, limit) | ||
|
||
|
||
@router.put("/study_sessions/{study_session_id}", response_model=StudySessionInDBBase) | ||
def update_study_session_endpoint( | ||
study_session_id: int, | ||
study_session: StudySessionUpdate, | ||
db: Session = Depends(get_db), | ||
) -> StudySessionInDBBase: | ||
db_study_session = get_study_session(db, study_session_id) | ||
if db_study_session is None: | ||
raise HTTPException(status_code=404, detail="StudySession not found") | ||
return update_study_session(db, study_session_id, study_session.model_dump()) | ||
|
||
|
||
@router.delete( | ||
"/study_sessions/{study_session_id}", response_model=StudySessionInDBBase | ||
) | ||
def delete_study_session_endpoint( | ||
study_session_id: int, db: Session = Depends(get_db) | ||
) -> StudySessionInDBBase: | ||
study_session = delete_study_session(db, study_session_id) | ||
if study_session is None: | ||
raise HTTPException(status_code=404, detail="StudySession not found") | ||
return study_session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# app/repositories/activity_repository.py | ||
|
||
from sqlalchemy.orm import Session | ||
from app.database.models import Activity | ||
from app.schemas.activity import ActivityCreate, ActivityUpdate | ||
|
||
|
||
def create_activity(db: Session, activity: ActivityCreate) -> Activity: | ||
db_activity = Activity(**activity.model_dump()) | ||
db.add(db_activity) | ||
db.commit() | ||
db.refresh(db_activity) | ||
return db_activity | ||
|
||
|
||
def get_activity(db: Session, activity_id: int) -> Activity: | ||
return db.query(Activity).filter(Activity.id == activity_id).first() | ||
|
||
|
||
def get_activities(db: Session, skip: int = 0, limit: int = 10) -> list[Activity]: | ||
return db.query(Activity).offset(skip).limit(limit).all() | ||
|
||
|
||
def update_activity( | ||
db: Session, activity_id: int, activity: ActivityUpdate | ||
) -> Activity: | ||
db.query(Activity).filter(Activity.id == activity_id).update(activity.model_dump()) | ||
db.commit() | ||
return db.query(Activity).filter(Activity.id == activity_id).first() | ||
|
||
|
||
def delete_activity(db: Session, activity_id: int) -> Activity: | ||
activity = db.query(Activity).filter(Activity.id == activity_id).first() | ||
if activity: | ||
db.delete(activity) | ||
db.commit() | ||
return activity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# app/repositories/answer_repository.py | ||
|
||
from sqlalchemy.orm import Session | ||
from app.database.models import Answer | ||
from app.schemas.answer import AnswerCreate, AnswerUpdate | ||
|
||
|
||
def create_answer(db: Session, answer: AnswerCreate) -> Answer: | ||
db_answer = Answer(**answer.model_dump()) | ||
db.add(db_answer) | ||
db.commit() | ||
db.refresh(db_answer) | ||
return db_answer | ||
|
||
|
||
def get_answer(db: Session, answer_id: int) -> Answer: | ||
return db.query(Answer).filter(Answer.id == answer_id).first() | ||
|
||
|
||
def get_answers(db: Session, skip: int = 0, limit: int = 10) -> list[Answer]: | ||
return db.query(Answer).offset(skip).limit(limit).all() | ||
|
||
|
||
def update_answer(db: Session, answer_id: int, answer: AnswerUpdate) -> Answer: | ||
db.query(Answer).filter(Answer.id == answer_id).update(answer.model_dump()) | ||
db.commit() | ||
return db.query(Answer).filter(Answer.id == answer_id).first() | ||
|
||
|
||
def delete_answer(db: Session, answer_id: int) -> Answer: | ||
answer = db.query(Answer).filter(Answer.id == answer_id).first() | ||
if answer: | ||
db.delete(answer) | ||
db.commit() | ||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# app/repositories/question_repository.py | ||
|
||
from sqlalchemy.orm import Session | ||
from app.database.models import Question | ||
from app.schemas.question import QuestionCreate, QuestionUpdate | ||
|
||
|
||
def create_question(db: Session, question: QuestionCreate) -> Question: | ||
db_question = Question(**question.model_dump()) | ||
db.add(db_question) | ||
db.commit() | ||
db.refresh(db_question) | ||
return db_question | ||
|
||
|
||
def get_question(db: Session, question_id: int) -> Question: | ||
return db.query(Question).filter(Question.id == question_id).first() | ||
|
||
|
||
def get_questions(db: Session, skip: int = 0, limit: int = 10) -> list[Question]: | ||
return db.query(Question).offset(skip).limit(limit).all() | ||
|
||
|
||
def update_question( | ||
db: Session, question_id: int, question: QuestionUpdate | ||
) -> Question: | ||
db.query(Question).filter(Question.id == question_id).update(question.model_dump()) | ||
db.commit() | ||
return db.query(Question).filter(Question.id == question_id).first() | ||
|
||
|
||
def delete_question(db: Session, question_id: int) -> Question: | ||
question = db.query(Question).filter(Question.id == question_id).first() | ||
if question: | ||
db.delete(question) | ||
db.commit() | ||
return question |
Oops, something went wrong.