File size: 2,530 Bytes
f3aae92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9fe174
 
 
 
 
 
 
 
 
 
 
f3aae92
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Endpoints for managing definition of examples.."""
from typing import  List
from uuid import UUID

from fastapi import APIRouter, HTTPException


from db.models import Example

from database import get_mongo_client
from pydantic import BaseModel, Field
router = APIRouter(
    prefix="/examples",
    tags=["example definitions"],
    responses={404: {"description": "Not found"}},
)


class CreateExample(BaseModel):
    """A request to create examples for a specific report type."""
    examples: List[Example] = Field(..., description="A list of examples associated with the report type.")

class CreateExampleResponse(BaseModel):
    """Response for creating an example."""

    result: int


@router.post("", response_model=CreateExampleResponse)
def create(
    create_request: List[Example],
   
) -> CreateExampleResponse:
    """Endpoint to create an example."""
    instance = [example.model_dump() for example in create_request]
   
    
    db = get_mongo_client().Cygnus

    example_collection=db["examples"]

    result=example_collection.insert_many(instance)
    return { "result":len(result.inserted_ids)}

    
    

@router.get("")
def list_examples(
    report_type: str,
    *,
    limit: int = 10,
    offset: int = 0,
    
) -> List[Example]:
    """Endpoint to get all examples."""
    db = get_mongo_client().Cygnus
    example_collection = db["examples"]
    
    query = {"report_type": report_type}
    
    examples_cursor = example_collection.find(query).skip(offset).limit(limit)
    examples = [Example(**record) for record in examples_cursor]
    return examples
 


# deleete all must be declared before delet uuid
@router.delete("/all")
def delete_all() -> int:
    """Endpoint to delete all examples."""
    db = get_mongo_client().Cygnus
    example_collection = db["examples"]
    result = example_collection.delete_many({})
    if result.deleted_count == 0:
        raise HTTPException(status_code=404, detail="No examples found to delete.")
    return result.deleted_count
    
@router.delete("/{uuid}")
def delete(
    uuid: UUID    
) -> None:
    """Endpoint to delete an example."""
    db = get_mongo_client().Cygnus

    example_collection = db["examples"]
    example = example_collection.find_one({"uuid": uuid})
    if example is None:
        raise HTTPException(status_code=404, detail="Example not found.")
    result = example_collection.delete_one({"uuid": uuid})
    if result.deleted_count == 0:
        raise HTTPException(status_code=404, detail="Example not found.")