|
1 | | -from typing import Dict, Union |
| 1 | +from typing import Dict, Optional, Union |
2 | 2 |
|
3 | | -from fastapi import APIRouter, Body, Depends, Response |
| 3 | +from fastapi import APIRouter, Body, Depends, Query, Response |
4 | 4 |
|
5 | 5 | from diffcalc_API.services import constraints as service |
6 | | -from diffcalc_API.stores.pickling import get_store |
7 | | -from diffcalc_API.stores.protocol import HklCalcStore |
| 6 | +from diffcalc_API.stores.protocol import HklCalcStore, get_store |
8 | 7 |
|
9 | 8 | router = APIRouter(prefix="/constraints", tags=["constraints"]) |
10 | 9 |
|
11 | 10 |
|
12 | 11 | @router.get("/{name}") |
13 | | -async def get_constraints(name: str, store: HklCalcStore = Depends(get_store)): |
14 | | - content = await service.get_constraints(name, store) |
15 | | - |
| 12 | +async def get_constraints( |
| 13 | + name: str, |
| 14 | + store: HklCalcStore = Depends(get_store), |
| 15 | + collection: Optional[str] = Query(default=None, example="B07"), |
| 16 | +): |
| 17 | + content = await service.get_constraints(name, store, collection) |
16 | 18 | return Response(content=content, media_type="application/text") |
17 | 19 |
|
18 | 20 |
|
19 | | -@router.put("/{name}/set") |
| 21 | +@router.post("/{name}") |
20 | 22 | async def set_constraints( |
21 | 23 | name: str, |
22 | 24 | constraints: Dict[str, Union[float, bool]] = Body( |
23 | 25 | example={"qaz": 0, "alpha": 0, "eta": 0} |
24 | 26 | ), |
25 | 27 | store: HklCalcStore = Depends(get_store), |
| 28 | + collection: Optional[str] = Query(default=None, example="B07"), |
26 | 29 | ): |
27 | | - await service.set_constraints(name, constraints, store) |
| 30 | + await service.set_constraints(name, constraints, store, collection) |
28 | 31 |
|
29 | | - return {"message": f"constraints updated (replaced) for crystal {name}"} |
| 32 | + return { |
| 33 | + "message": ( |
| 34 | + f"constraints updated (replaced) for crystal {name} in " |
| 35 | + + f"collection {collection}" |
| 36 | + ) |
| 37 | + } |
30 | 38 |
|
31 | 39 |
|
32 | | -@router.patch("/{name}/unconstrain/{property}") |
| 40 | +@router.delete("/{name}/{property}") |
33 | 41 | async def remove_constraint( |
34 | 42 | name: str, |
35 | 43 | property: str, |
36 | 44 | store: HklCalcStore = Depends(get_store), |
| 45 | + collection: Optional[str] = Query(default=None, example="B07"), |
37 | 46 | ): |
38 | | - await service.remove_constraint(name, property, store) |
| 47 | + await service.remove_constraint(name, property, store, collection) |
39 | 48 |
|
40 | | - return {"message": f"unconstrained {property} for crystal {name}. "} |
| 49 | + return { |
| 50 | + "message": ( |
| 51 | + f"unconstrained {property} for crystal {name} in " |
| 52 | + + f"collection {collection}. " |
| 53 | + ) |
| 54 | + } |
41 | 55 |
|
42 | 56 |
|
43 | | -@router.patch("/{name}/constrain/{property}") |
| 57 | +@router.patch("/{name}/{property}") |
44 | 58 | async def set_constraint( |
45 | 59 | name: str, |
46 | 60 | property: str, |
47 | 61 | value: Union[float, bool] = Body(...), |
48 | 62 | store: HklCalcStore = Depends(get_store), |
| 63 | + collection: Optional[str] = Query(default=None, example="B07"), |
49 | 64 | ): |
50 | | - await service.set_constraint(name, property, value, store) |
| 65 | + await service.set_constraint(name, property, value, store, collection) |
51 | 66 |
|
52 | | - return {"message": f"constrained {property} for crystal {name}. "} |
| 67 | + return { |
| 68 | + "message": ( |
| 69 | + f"constrained {property} for crystal {name} in collection " |
| 70 | + + f"{collection}. " |
| 71 | + ) |
| 72 | + } |
0 commit comments