-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added API objects, endpoints and started work on persistence layer
- Loading branch information
1 parent
d057b20
commit dd43380
Showing
17 changed files
with
846 additions
and
25 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,3 @@ | ||
# Irrigation Pi Backend | ||
|
||
A Python backend application using FastAPI. |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
"""API routers for API version 1.0.""" | ||
from fastapi import APIRouter | ||
|
||
from app.api.v1.endpoints import schedules | ||
from app.api.v1.endpoints import schedule | ||
|
||
api_router = APIRouter() | ||
api_router.include_router(schedules.router, prefix="/schedules", tags=["schedules"]) | ||
api_router.include_router(schedule.router, prefix="/schedule", tags=["schedule"]) |
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,43 @@ | ||
"""API endpoints for Schedule objects.""" | ||
from fastapi import APIRouter | ||
|
||
from app.api.v1.models import ScheduleCreate, ScheduleResponse, ScheduleUpdate | ||
from app.services.schedule import ( | ||
service_create_schedule, | ||
service_delete_schedule, | ||
service_get_schedule, | ||
service_get_schedules, | ||
service_update_schedule, | ||
) | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/{primary_key}") | ||
def get_schedule(primary_key: int) -> ScheduleResponse: | ||
"""Get Schedule.""" | ||
return service_get_schedule(primary_key) | ||
|
||
|
||
@router.get("/") | ||
def get_schedules() -> list[ScheduleResponse]: | ||
"""Get Schedules.""" | ||
return service_get_schedules() | ||
|
||
|
||
@router.post("/") | ||
def create_schedule(schedule_data: ScheduleCreate) -> int: | ||
"""Create Schedule.""" | ||
return service_create_schedule(schedule_data) | ||
|
||
|
||
@router.put("/") | ||
def update_schedule(schedule_data: ScheduleUpdate): | ||
"""Update Schedule.""" | ||
return service_update_schedule(schedule_data) | ||
|
||
|
||
@router.delete("/{primary_key}") | ||
def delete_schedule(primary_key: int): | ||
"""Delete Schedule.""" | ||
return service_delete_schedule(primary_key) |
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
"""API models.""" | ||
from datetime import time | ||
from typing import Union | ||
|
||
from pydantic import BaseModel, Field, PositiveInt | ||
|
||
from app.scheduling import Repeat | ||
|
||
|
||
class ScheduleResponse(BaseModel): | ||
"""Response schema for schedule.""" | ||
|
||
id: PositiveInt = Field(description="Primary key") | ||
start_time: time = Field(description="Start time of the schedule") | ||
duration: PositiveInt = Field(description="Duration in minutes") | ||
repeat: Repeat = Field(description="Specifies how the schedule is repeated") | ||
active: bool = Field(description="Whether the schedule is active") | ||
|
||
|
||
class ScheduleCreate(BaseModel): | ||
"""Creation schema for schedule.""" | ||
|
||
start_time: time = Field(description="Start time of the schedule") | ||
duration: PositiveInt = Field(description="Duration in minutes") | ||
repeat: Repeat = Field(description="Specifies how the schedule is repeated") | ||
active: bool = Field(default=True, description="Whether the schedule is active") | ||
|
||
|
||
class ScheduleUpdate(BaseModel): | ||
"""Update schema for schedule.""" | ||
|
||
start_time: Union[time, None] = Field( | ||
default=None, description="Start time of the schedule" | ||
) | ||
duration: Union[PositiveInt, None] = Field( | ||
default=None, description="Duration in minutes" | ||
) | ||
repeat: Union[Repeat, None] = Field( | ||
default=None, description="Specifies how the schedule is repeated" | ||
) | ||
active: Union[bool, None] = Field( | ||
default=None, description="Whether the schedule is active" | ||
) |
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,20 @@ | ||
"""Application configuration.""" | ||
from pydantic import computed_field | ||
from pydantic_settings import BaseSettings | ||
|
||
|
||
class ApplicationSettings(BaseSettings): | ||
"""Application settings.""" | ||
|
||
database_name: str = "sqlite" | ||
|
||
@computed_field | ||
def database_uri(self) -> str: | ||
"""URI for database connection. | ||
:return: | ||
""" | ||
return f"sqlite:///{self.database_name}.db" | ||
|
||
|
||
application_settings = ApplicationSettings() |
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 @@ | ||
"""Database module.""" |
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,7 @@ | ||
"""Database configuration.""" | ||
from sqlalchemy import Engine | ||
from sqlmodel import create_engine | ||
|
||
from app.config import application_settings | ||
|
||
database_engine: Engine = create_engine(application_settings.database_uri) |
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,22 @@ | ||
"""Database models.""" | ||
from datetime import time | ||
from typing import Optional | ||
|
||
from sqlmodel import Field, SQLModel | ||
|
||
from app.scheduling import Repeat | ||
|
||
|
||
class BaseModel(SQLModel): | ||
"""Base model.""" | ||
|
||
id: Optional[int] = Field(default=None, primary_key=True) | ||
|
||
|
||
class Schedule(BaseModel, table=True): | ||
"""Schedule.""" | ||
|
||
start_time: time | ||
duration: int | ||
repeat: Repeat | ||
active: bool |
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
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
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 @@ | ||
"""Services module.""" |
File renamed without changes.
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,46 @@ | ||
"""Services for handling persistence of Schedule objects.""" | ||
from app.api.v1.models import ScheduleCreate, ScheduleResponse, ScheduleUpdate | ||
|
||
|
||
def service_get_schedule(primary_key: int) -> ScheduleResponse: | ||
"""Service returns a Schedule object. | ||
:param int primary_key: Primary key | ||
:return ScheduleResponse: | ||
""" | ||
pass | ||
|
||
|
||
def service_get_schedules() -> list[ScheduleResponse]: | ||
"""Service returns a list of Schedule objects. | ||
:return list[ScheduleResponse]: | ||
""" | ||
pass | ||
|
||
|
||
def service_create_schedule(data: ScheduleCreate): | ||
"""Services creates and persists a Schedule object. | ||
:param ScheduleCreate data: | ||
:return: | ||
""" | ||
pass | ||
|
||
|
||
def service_update_schedule(data: ScheduleUpdate): | ||
"""Service updates and persists a Schedule object. | ||
:param ScheduleUpdate data: | ||
:return: | ||
""" | ||
pass | ||
|
||
|
||
def service_delete_schedule(primary_key: int): | ||
"""Service deletes a persisted Schedule object. | ||
:param int primary_key: | ||
:return: | ||
""" | ||
pass |
Oops, something went wrong.