Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
Twoje Imię Nazwisko | d87d5bf2be | |
Twoje Imię Nazwisko | de44be2320 | |
Twoje Imię Nazwisko | 391f025e0f |
4
__n1
4
__n1
|
@ -3,6 +3,6 @@ sudo docker run --rm \
|
||||||
--privileged \
|
--privileged \
|
||||||
-p 9999:9999 \
|
-p 9999:9999 \
|
||||||
-p 8888:8888 \
|
-p 8888:8888 \
|
||||||
-v /home/user/work/fapi/fapi/src:/home/user/fapi \
|
-v "$(pwd):/home/user/fapi" \
|
||||||
--name f1 \
|
--name b1 \
|
||||||
fapi bash
|
fapi bash
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -4,7 +4,7 @@ from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
#SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" # Dla SQLite
|
#SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" # Dla SQLite
|
||||||
SQLALCHEMY_DATABASE_URL = "mysql+mysqldb://root:secret@172.19.0.4:3306/test"
|
SQLALCHEMY_DATABASE_URL = "mysql+mysqldb://root:secret@172.18.0.2:3306/test"
|
||||||
|
|
||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
SQLALCHEMY_DATABASE_URL,
|
SQLALCHEMY_DATABASE_URL,
|
||||||
|
|
81
app/main.py
81
app/main.py
|
@ -1,25 +1,15 @@
|
||||||
from fastapi import FastAPI, Depends, HTTPException
|
from fastapi import FastAPI, Depends, HTTPException
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from . import models, schemas
|
||||||
from app.database import SessionLocal, engine
|
from .database import SessionLocal, engine
|
||||||
import app.models as models
|
from datetime import datetime
|
||||||
from app.schemas import Person, PersonCreate
|
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
origins = [
|
# Tworzenie tabel w bazie danych
|
||||||
"http://localhost:8080",
|
models.Base.metadata.create_all(bind=engine)
|
||||||
"http://mpabi.pl:8888",
|
|
||||||
]
|
|
||||||
|
|
||||||
app.add_middleware(
|
|
||||||
CORSMiddleware,
|
|
||||||
allow_origins=origins,
|
|
||||||
allow_credentials=True,
|
|
||||||
allow_methods=["*"],
|
|
||||||
allow_headers=["*"],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# Dependency do sesji z bazą danych
|
||||||
def get_db():
|
def get_db():
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
|
@ -27,31 +17,42 @@ def get_db():
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
# Endpoint do tworzenia nowej osoby (POST)
|
# Endpoint do pobierania ról
|
||||||
@app.post("/persons/", response_model=Person)
|
@app.get("/roles/", response_model=list[schemas.Role])
|
||||||
def create_person(person: PersonCreate, db: Session = Depends(get_db)):
|
def read_roles(db: Session = Depends(get_db)):
|
||||||
db_person = models.Person(
|
return db.query(models.Role).all()
|
||||||
last_name=person.last_name,
|
|
||||||
first_name=person.first_name,
|
# Endpoint do dodania roli
|
||||||
address=person.address,
|
@app.post("/roles/", response_model=schemas.Role)
|
||||||
city=person.city
|
def create_role(role: schemas.RoleCreate, db: Session = Depends(get_db)):
|
||||||
)
|
db_role = models.Role(name=role.name)
|
||||||
db.add(db_person)
|
db.add(db_role)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_person)
|
db.refresh(db_role)
|
||||||
return db_person
|
return db_role
|
||||||
|
|
||||||
# Endpoint do odczytu listy osób (GET)
|
# Endpoint do pobierania zdarzeń
|
||||||
@app.get("/persons/", response_model=list[Person])
|
@app.get("/events/", response_model=list[schemas.Event])
|
||||||
def read_persons(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
def read_events(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
|
||||||
persons = db.query(models.Person).offset(skip).limit(limit).all()
|
return db.query(models.Event).offset(skip).limit(limit).all()
|
||||||
return persons
|
|
||||||
|
|
||||||
# Endpoint do odczytu osoby po ID (GET)
|
# Endpoint do dodania zdarzenia
|
||||||
@app.get("/persons/{person_id}", response_model=Person)
|
@app.post("/events/", response_model=schemas.Event)
|
||||||
def read_person(person_id: int, db: Session = Depends(get_db)):
|
def create_event(event: schemas.EventCreate, db: Session = Depends(get_db)):
|
||||||
person = db.query(models.Person).filter(models.Person.person_id == person_id).first()
|
db_event = models.Event(
|
||||||
if person is None:
|
role_id=event.role_id,
|
||||||
raise HTTPException(status_code=404, detail="Person not found")
|
student_id=event.student_id,
|
||||||
return person
|
criteria_id=event.criteria_id,
|
||||||
|
description=event.description,
|
||||||
|
event_date=event.event_date or datetime.now()
|
||||||
|
)
|
||||||
|
db.add(db_event)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_event)
|
||||||
|
return db_event
|
||||||
|
|
||||||
|
# Endpoint do pobierania informacji o zdarzeniach dla konkretnego ucznia
|
||||||
|
@app.get("/events/student/{student_id}", response_model=list[schemas.Event])
|
||||||
|
def read_student_events(student_id: int, db: Session = Depends(get_db)):
|
||||||
|
return db.query(models.Event).filter(models.Event.student_id == student_id).all()
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,54 @@
|
||||||
from sqlalchemy import Column, Integer, String
|
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.orm import relationship
|
||||||
|
from .database import Base
|
||||||
|
|
||||||
Base = declarative_base()
|
class Criteria(Base):
|
||||||
|
__tablename__ = "criteria"
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String(255))
|
||||||
|
|
||||||
class Person(Base):
|
class Role(Base):
|
||||||
__tablename__ = "Persons"
|
__tablename__ = "roles"
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String(255))
|
||||||
|
|
||||||
person_id = Column("PersonID", Integer, primary_key=True, index=True, autoincrement=True)
|
class Student(Base):
|
||||||
last_name = Column("LastName", String(255), nullable=False)
|
__tablename__ = "students"
|
||||||
first_name = Column("FirstName", String(255), nullable=True)
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
address = Column("Address", String(255), nullable=True)
|
first_name = Column(String(50))
|
||||||
city = Column("City", String(255), nullable=True)
|
last_name = Column(String(50))
|
||||||
|
grade = Column(String(50), default="Brak ocen")
|
||||||
|
points = Column(Integer, nullable=True)
|
||||||
|
status = Column(Integer)
|
||||||
|
|
||||||
"""
|
class ClassInfo(Base):
|
||||||
SQL Insert statements:
|
__tablename__ = "class_info"
|
||||||
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
VALUES
|
school = Column(String(255))
|
||||||
(1, 'Smith', 'John', '123 Maple St', 'New York'),
|
city = Column(String(255))
|
||||||
(2, 'Johnson', 'Emily', '456 Oak Ave', 'Los Angeles'),
|
year = Column(String(50))
|
||||||
(3, 'Williams', 'Michael', '789 Pine Dr', 'Chicago'),
|
semester = Column(String(50))
|
||||||
(4, 'Brown', 'Sarah', '101 Birch Ln', 'Houston'),
|
subject = Column(String(255))
|
||||||
(5, 'Jones', 'David', '202 Cedar Rd', 'Phoenix');
|
level = Column(String(50))
|
||||||
"""
|
publisher = Column(String(255))
|
||||||
|
class_name = Column(String(50))
|
||||||
|
group = Column(String(50))
|
||||||
|
profile = Column(String(255))
|
||||||
|
max_points = Column(Integer)
|
||||||
|
teacher = Column(String(255))
|
||||||
|
file_path = Column(String(255))
|
||||||
|
student_count = Column(Integer)
|
||||||
|
|
||||||
|
class Event(Base):
|
||||||
|
__tablename__ = "events"
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
role_id = Column(Integer, ForeignKey("roles.id"), nullable=False)
|
||||||
|
student_id = Column(Integer, ForeignKey("students.id"), nullable=False)
|
||||||
|
criteria_id = Column(Integer, ForeignKey("criteria.id"), nullable=False)
|
||||||
|
description = Column(String(255))
|
||||||
|
event_date = Column(DateTime)
|
||||||
|
|
||||||
|
role = relationship("Role")
|
||||||
|
student = relationship("Student")
|
||||||
|
criteria = relationship("Criteria")
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,83 @@
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
# Schemat do tworzenia nowej osoby (POST)
|
class CriteriaBase(BaseModel):
|
||||||
class PersonCreate(BaseModel):
|
name: str
|
||||||
last_name: str = Field(..., description="Nazwisko osoby", example="Kowalski")
|
|
||||||
first_name: str = Field(..., description="Imię osoby", example="Jan")
|
|
||||||
address: str | None = None
|
|
||||||
city: str | None = None
|
|
||||||
|
|
||||||
# Schemat reprezentujący osobę (GET)
|
class CriteriaCreate(CriteriaBase):
|
||||||
class Person(BaseModel):
|
pass
|
||||||
person_id: int
|
|
||||||
last_name: str
|
class Criteria(CriteriaBase):
|
||||||
first_name: str
|
id: int
|
||||||
address: str | None = None
|
|
||||||
city: str | None = None
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
class RoleBase(BaseModel):
|
||||||
|
name: str
|
||||||
|
|
||||||
|
class RoleCreate(RoleBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Role(RoleBase):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
class StudentBase(BaseModel):
|
||||||
|
first_name: str
|
||||||
|
last_name: str
|
||||||
|
grade: str
|
||||||
|
points: int
|
||||||
|
status: int
|
||||||
|
|
||||||
|
class StudentCreate(StudentBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Student(StudentBase):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
class ClassInfoBase(BaseModel):
|
||||||
|
school: str
|
||||||
|
city: str
|
||||||
|
year: str
|
||||||
|
semester: str
|
||||||
|
subject: str
|
||||||
|
level: str
|
||||||
|
publisher: str
|
||||||
|
class_name: str
|
||||||
|
group: str
|
||||||
|
profile: str
|
||||||
|
max_points: int
|
||||||
|
teacher: str
|
||||||
|
file_path: str
|
||||||
|
student_count: int
|
||||||
|
|
||||||
|
class ClassInfoCreate(ClassInfoBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ClassInfo(ClassInfoBase):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
class EventBase(BaseModel):
|
||||||
|
role_id: int
|
||||||
|
student_id: int
|
||||||
|
criteria_id: int
|
||||||
|
description: str
|
||||||
|
event_date: datetime
|
||||||
|
|
||||||
|
class EventCreate(EventBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Event(EventBase):
|
||||||
|
id: int
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
orm_mode = True
|
||||||
|
|
Loading…
Reference in New Issue