Skip to content

Commit

Permalink
feat: added select delete
Browse files Browse the repository at this point in the history
  • Loading branch information
creyD committed Oct 10, 2024
1 parent cefb48a commit 88e97fa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
41 changes: 37 additions & 4 deletions app/routes/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from fastapi import APIRouter, Depends, Security, HTTPException
from sqlalchemy.orm import Session
from sqlalchemy import select
from sqlalchemy import delete
from app.services.auth import verify
from app.schema.entry import LogIN, LogOUT
from app.models.entry import LogEntry
Expand All @@ -17,6 +18,8 @@
from pydantic.json_schema import SkipJsonSchema
from fastapi_filters import FilterValues, create_filters
from fastapi_filters.ext.sqlalchemy import apply_filters
from app.models.entry import LogType, TransactionType
from datetime import datetime

router = APIRouter(prefix="/log", tags=["logging"])

Expand Down Expand Up @@ -62,10 +65,6 @@ async def get_log(
return LogOUT.model_validate(obj)


from app.models.entry import LogType, TransactionType
from datetime import datetime


@router.get("/")
async def get_logs(
search: str | SkipJsonSchema[None] = None,
Expand Down Expand Up @@ -94,3 +93,37 @@ async def get_logs(
LogEntry.message.ilike(f"%{search}%") | LogEntry.author.ilike(f"%{search}%")
)
return paginate(db, order_by_query(the_select))


@router.delete("/", status_code=200)
async def delete_logs(
application: UUID,
environment: str | SkipJsonSchema[None] = None,
l_type: LogType | SkipJsonSchema[None] = None,
t_type: TransactionType | SkipJsonSchema[None] = None,
object_reference: str | SkipJsonSchema[None] = None,
author: str | SkipJsonSchema[None] = None,
sub: str = Security(verify),
db: Session = Depends(get_db),
) -> int:
filters = {
"application": application,
"created_by_id": sub,
}

if environment is not None:
filters["environment"] = environment
if l_type is not None:
filters["l_type"] = l_type
if t_type is not None:
filters["t_type"] = t_type
if object_reference is not None:
filters["object_reference"] = object_reference
if author is not None:
filters["author"] = author

query = db.query(LogEntry).filter_by(**filters)
the_impact = query.count()
query.delete(synchronize_session=False)
db.commit()
return the_impact
21 changes: 21 additions & 0 deletions app/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,24 @@ def test_logging_filter(self):
re = self.c.get("/log/?application=" + app_id + "&environment=prod")
assert re["total"] == 2
assert len(re["results"]) == 2

def test_logging_delete(self):
with log_examples(self) as app_id:
re = self.c.delete("/log/?application=" + str(app_id) + "&environment=prod", r_code=200)
assert re == 2

re = self.c.get("/log/?application=" + str(app_id) + "&environment=prod")
assert re["total"] == 0

re = self.c.get("/log/?application=" + str(app_id) + "&environment=dev")
assert re["total"] == 3

# clear complete application
re = self.c.get("/log/?application=" + str(app_id))
assert re["total"] == 3

re = self.c.delete("/log/?application=" + str(app_id), r_code=200)
assert re == 3

re = self.c.get("/log/?application=" + str(app_id))
assert re["total"] == 0

0 comments on commit 88e97fa

Please sign in to comment.