Implement service api method

This commit is contained in:
pmoharana-cmd 2024-04-24 19:57:44 -04:00
parent 34dd4ec48f
commit 3fc8f0e149
6 changed files with 40 additions and 9 deletions

26
backend/api/service.py Normal file
View File

@ -0,0 +1,26 @@
from fastapi import APIRouter, Depends
from ..services import ServiceService, UserService
from ..models.service_model import Service
from typing import List
api = APIRouter(prefix="/api/service")
openapi_tags = {
"name": "Service",
"description": "Service search and related operations.",
}
# TODO: Add security using HTTP Bearer Tokens
# TODO: Enable authorization by passing user uuid to API
# TODO: Create custom exceptions
@api.get("", response_model=List[Service], tags=["Service"])
def get_all(
user_id: str,
service_svc: ServiceService = Depends(),
user_svc: UserService = Depends(),
):
subject = user_svc.get_user_by_uuid(user_id)
return service_svc.get_service_by_user(subject)

View File

@ -2,7 +2,7 @@ from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.gzip import GZipMiddleware
from .api import user, health
from .api import user, health, service
description = """
Welcome to the **COMPASS** RESTful Application Programming Interface.
@ -12,12 +12,12 @@ app = FastAPI(
title="Compass API",
version="0.0.1",
description=description,
openapi_tags=[user.openapi_tags, health.openapi_tags],
openapi_tags=[user.openapi_tags, health.openapi_tags, service.openapi_tags],
)
app.add_middleware(GZipMiddleware)
feature_apis = [user, health]
feature_apis = [user, health, service]
for feature_api in feature_apis:
app.include_router(feature_api.api)

View File

@ -6,7 +6,7 @@ from ..database import engine, _engine_str
from ..env import getenv
from .. import entities
from ..test.services import user_test_data
from ..test.services import user_test_data, service_test_data
database = getenv("POSTGRES_DATABASE")
@ -22,3 +22,6 @@ entities.EntityBase.metadata.create_all(engine)
with Session(engine) as session:
user_test_data.insert_test_data(session)
service_test_data.insert_fake_data(session)
session.commit()

View File

@ -58,9 +58,11 @@ class ServiceService:
services = []
for program in programs:
query = select(ServiceEntity).filter(ServiceEntity.program == program)
entities = self._session.scalars(query)
services.append(entities)
return [service.to_model() for service in entities]
entities = self._session.scalars(query).all()
for entity in entities:
services.append(entity)
return [service.to_model() for service in services]
def get_all(self, subject: User) -> list[Service]:
"""Service method retrieving all of the services in the table."""

View File

@ -35,7 +35,7 @@ def test_get_service_by_user_admin(service_svc: ServiceService):
def test_get_service_by_user_volun(service_svc: ServiceService):
service = service_svc.get_service_by_user(user_test_data.volunteer)
assert len(service) == 3
assert len(service) == 4
def test_get_by_program(service_svc: ServiceService):

View File

@ -18,7 +18,7 @@ volunteer = User(
email="volunteer@compass.com",
experience=1,
group="volunteers",
program=[programs.COMMUNITY],
program=[programs.COMMUNITY, programs.ECONOMIC],
created_at=datetime.now(),
role=UserTypeEnum.VOLUNTEER,
)