parent
428d58f934
commit
cebc87cd38
1 changed files with 14 additions and 0 deletions
|
@ -33,6 +33,10 @@ class BaseStore:
|
||||||
newval = self.get(key) + 1
|
newval = self.get(key) + 1
|
||||||
return self.set(key, newval)
|
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:
|
def set(self, key, value: int) -> int:
|
||||||
self.values[key] = value
|
self.values[key] = value
|
||||||
return 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")
|
raise HTTPException(status_code=404, detail="Counter not found")
|
||||||
return Value(counter=cid, value=val)
|
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}")
|
@app.websocket("/v1/ws/counter/{cid}")
|
||||||
async def websocket_counter(websocket: WebSocket, cid: int):
|
async def websocket_counter(websocket: WebSocket, cid: int):
|
||||||
|
|
Loading…
Reference in a new issue