/decrement

closes #4
This commit is contained in:
boyska 2022-08-17 19:18:16 +02:00
parent 428d58f934
commit cebc87cd38

View file

@ -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):