Browse Source

/decrement

closes #4
boyska 1 year ago
parent
commit
cebc87cd38
1 changed files with 14 additions and 0 deletions
  1. 14 0
      pizzicore/pizzicore.py

+ 14 - 0
pizzicore/pizzicore.py

@@ -33,6 +33,10 @@ class BaseStore:
         newval = self.get(key) + 1
         return self.set(key, newval)
 
+    def decr(self, key) -> int:
+        newval = self.get(key) - 1
+        return self.set(key, newval)
+
     def set(self, key, value: int) -> int:
         self.values[key] = value
         return value
@@ -157,6 +161,16 @@ async def increment(cid: int, role: str = Depends(get_current_role)):
         raise HTTPException(status_code=404, detail="Counter not found")
     return Value(counter=cid, value=val)
 
+@app.post("/v1/counter/{cid}/decrement")
+async def increment(cid: int, role: str = Depends(get_current_role)):
+    if role != "admin":
+        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
+    try:
+        val = counter_store.decr(cid)
+    except KeyError:
+        raise HTTPException(status_code=404, detail="Counter not found")
+    return Value(counter=cid, value=val)
+
 
 @app.websocket("/v1/ws/counter/{cid}")
 async def websocket_counter(websocket: WebSocket, cid: int):