Fix duplication and update front end
This commit is contained in:
@@ -20,6 +20,7 @@ class Idea(BaseModel):
|
||||
end_time: Optional[datetime] = None
|
||||
description: str
|
||||
voters: list[str] = Field(default_factory=list)
|
||||
decided: bool = False
|
||||
|
||||
class Tour(BaseModel):
|
||||
id: str
|
||||
@@ -127,6 +128,9 @@ def add_idea(tour_id: str, idea: IdeaCreate):
|
||||
with open(filepath) as f:
|
||||
data = json.load(f)
|
||||
|
||||
if any(i.get("decided") for i in data["ideas"]):
|
||||
raise HTTPException(status_code=403, detail="Idea already decided")
|
||||
|
||||
idea_id = str(uuid.uuid4())
|
||||
new_idea = Idea(
|
||||
id=idea_id,
|
||||
@@ -135,6 +139,7 @@ def add_idea(tour_id: str, idea: IdeaCreate):
|
||||
voters=[],
|
||||
start_time=idea.start_time,
|
||||
end_time=idea.end_time,
|
||||
decided=False,
|
||||
)
|
||||
|
||||
data["ideas"].append(new_idea.model_dump())
|
||||
@@ -159,6 +164,8 @@ def vote_idea(tour_id: str, idea_id: str, vote: Vote):
|
||||
with open(filepath) as f:
|
||||
data = json.load(f)
|
||||
|
||||
if any(i.get("decided") for i in data["ideas"]):
|
||||
raise HTTPException(status_code=403, detail="Idea already decided")
|
||||
for idea in data["ideas"]:
|
||||
if idea["id"] == idea_id:
|
||||
if vote.voterName not in idea["voters"]:
|
||||
@@ -170,6 +177,32 @@ def vote_idea(tour_id: str, idea_id: str, vote: Vote):
|
||||
|
||||
raise HTTPException(status_code=404, detail="Idea not found")
|
||||
|
||||
# ─── ENDPOINT: DECIDE IDEA ────────────────────────────────────────────────────
|
||||
|
||||
@app.post("/tour/v1/tours/{tour_id}/ideas/{idea_id}/decide", response_model=Idea)
|
||||
def decide_idea(tour_id: str, idea_id: str):
|
||||
"""Mark an idea as decided. Only one idea can be decided per tour."""
|
||||
filepath = get_tour_filepath(tour_id)
|
||||
if not os.path.exists(filepath):
|
||||
raise HTTPException(status_code=404, detail="Tour not found")
|
||||
|
||||
lock = FileLock(filepath + ".lock")
|
||||
with lock:
|
||||
with open(filepath) as f:
|
||||
data = json.load(f)
|
||||
|
||||
if any(i.get("decided") for i in data["ideas"]):
|
||||
raise HTTPException(status_code=403, detail="Idea already decided")
|
||||
|
||||
for idea in data["ideas"]:
|
||||
if idea["id"] == idea_id:
|
||||
idea["decided"] = True
|
||||
with open(filepath, "w") as f:
|
||||
json.dump(data, f, default=str)
|
||||
return Idea(**idea)
|
||||
|
||||
raise HTTPException(status_code=404, detail="Idea not found")
|
||||
|
||||
# ─── MAIN: RUN UVIDORN WITH COMMAND-LINE DATA DIR ──────────────────────────────
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user