Add unread indication to subscription cards
This commit is contained in:
@@ -52,15 +52,6 @@ def hello():
|
||||
return {"message": "Welcome to the Project Monitor API"}
|
||||
|
||||
|
||||
# Subscriptions API Endpoints
|
||||
@app.get("/subscriptions")
|
||||
def list_subscriptions():
|
||||
db = SessionLocal()
|
||||
subscriptions = db.query(Subscription).all()
|
||||
db.close()
|
||||
return subscriptions
|
||||
|
||||
|
||||
class SubscriptionCreate(BaseModel):
|
||||
topic: str
|
||||
|
||||
@@ -69,10 +60,33 @@ class SubscriptionResponse(BaseModel):
|
||||
id: int
|
||||
topic: str
|
||||
created_at: datetime
|
||||
has_unread: bool
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
# Subscriptions API Endpoints
|
||||
@app.get("/subscriptions", response_model=list[SubscriptionResponse])
|
||||
def list_subscriptions():
|
||||
db = SessionLocal()
|
||||
subscriptions = db.query(Subscription).all()
|
||||
|
||||
# TODO: find a better way to do this
|
||||
for subscription in subscriptions:
|
||||
not_viewed_count = (
|
||||
db.query(Notification)
|
||||
.filter(
|
||||
Notification.subscription_id == subscription.id,
|
||||
~Notification.viewed,
|
||||
)
|
||||
.count()
|
||||
)
|
||||
subscription.has_unread = not_viewed_count > 0
|
||||
|
||||
db.close()
|
||||
return subscriptions
|
||||
|
||||
|
||||
@app.get("/subscriptions/{subscription_id}", response_model=SubscriptionResponse)
|
||||
def get_subscription(subscription_id: int):
|
||||
db = SessionLocal()
|
||||
@@ -82,6 +96,17 @@ def get_subscription(subscription_id: int):
|
||||
if not subscription:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail="Subscription not found")
|
||||
|
||||
# checking if subscription has unread messages
|
||||
subscription.has_unread = (
|
||||
db.query(Notification)
|
||||
.filter(
|
||||
Notification.subscription_id == subscription_id and not Notification.viewed
|
||||
)
|
||||
.count()
|
||||
> 0
|
||||
)
|
||||
|
||||
db.close()
|
||||
return subscription
|
||||
|
||||
|
||||
Reference in New Issue
Block a user