Skip to content

Commit

Permalink
feat: add deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
ntindle committed May 23, 2024
1 parent 2d69d8a commit 72fad42
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
79 changes: 79 additions & 0 deletions codex/requirements/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,36 @@ async def add_module_to_specification(
return module


async def delete_module_from_specification(
user_id: str,
app_id: str,
spec_id: str,
module_id: str,
):
# # Get created objects
available_objects = {}
available_objects = await get_object_types_for_spec(
user_id=user_id,
app_id=app_id,
spec_id=spec_id,
available_objects=available_objects,
)

# Get all the routes in the module
module = await prisma.models.Module.prisma().find_first_or_raise(
where={"id": module_id}
)
for route in module.ApiRouteSpecs or []:
await delete_route_from_module(
user_id=user_id,
app_id=app_id,
spec_id=spec_id,
route_id=route.id,
)
# Delete the Module
await prisma.models.Module.prisma().delete(where={"id": module_id})


async def get_modules(spec_id: str) -> list[prisma.models.Module]:
modules = await prisma.models.Module.prisma().find_many(
where={"specificationId": spec_id}
Expand Down Expand Up @@ -374,6 +404,25 @@ async def add_route_to_module(
return route


async def delete_route_from_module(
user_id: str,
app_id: str,
spec_id: str,
route_id: str,
):
# Get created objects
available_objects = {}
available_objects = await get_object_types_for_spec(
user_id=user_id,
app_id=app_id,
spec_id=spec_id,
available_objects=available_objects,
)

# Delete the Route
await prisma.models.APIRouteSpec.prisma().delete(where={"id": route_id})


async def add_parameter_to_route(
user_id: str,
app_id: str,
Expand Down Expand Up @@ -423,6 +472,36 @@ async def add_parameter_to_route(
return field


async def delete_parameter_from_route(
user_id: str,
app_id: str,
spec_id: str,
route_id: str,
param_id: str,
):
# Get created objects
available_objects = {}
available_objects = await get_object_types_for_spec(
user_id=user_id,
app_id=app_id,
spec_id=spec_id,
available_objects=available_objects,
)

# get the existing param, or make one if not found
route = await prisma.models.APIRouteSpec.prisma().find_first_or_raise(
where={"id": route_id}
)

if not route.RequestObject:
raise AssertionError("Route does not have a request object")
if not route.ResponseObject:
raise AssertionError("Route does not have a response object")

# Delete the Field
await prisma.models.ObjectField.prisma().delete(where={"id": param_id})


async def main():
file_path = os.path.join(os.path.dirname(__file__), "spec_holder.pickle")
prisma_client = prisma.Prisma(auto_register=True)
Expand Down
22 changes: 19 additions & 3 deletions codex/requirements/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ async def delete_module(
)

# Delete the module from the spec
# TODO: Delete module from spec
await codex.requirements.database.delete_module_from_specification(
user_id=user_id,
app_id=app_id,
spec_id=spec_id,
module_id=module_id,
)

return JSONResponse(
content={"message": "Module deleted successfully"},
Expand Down Expand Up @@ -205,7 +210,12 @@ async def delete_route(
)

# Delete the route from the spec
# TODO: Delete route from spec
await codex.requirements.database.delete_route_from_module(
user_id=user_id,
app_id=app_id,
spec_id=spec_id,
route_id=route_id,
)

return JSONResponse(
content={"message": "Route deleted successfully"},
Expand Down Expand Up @@ -275,7 +285,13 @@ async def delete_param_from_route(
)

# Delete the param from the route
# TODO: Delete param from route
await codex.requirements.database.delete_parameter_from_route(
user_id=user_id,
app_id=app_id,
spec_id=spec_id,
route_id=route_id,
param_id=param_id,
)

return JSONResponse(
content={"message": "Param deleted successfully"},
Expand Down

0 comments on commit 72fad42

Please sign in to comment.