File size: 1,402 Bytes
3153dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel, ValidationError, validator

from pytezos import pytezos


app = FastAPI()



pytezos = pytezos.using(shell = 'https://rpc.tzkt.io/ghostnet', key='edsk3MrRkoidY2SjEgufvi44orvyjxgZoy4LhaJNTNcddWykW6SssL')
contract = pytezos.contract('KT1KvCVKiZhkPG8s9CCoxW3r135phk2HhZUV')


# Define the request body model
class Patient(BaseModel):
    name: str
    email: str
    contact: int    
    age: int    
    gender: str
    number: int
    #is_offer: bool = None
    
    
    @validator('name')
    def name_must_not_be_empty(cls, v):
        if v == ' ':
            raise ValueError('must contain a space')
        return v 

    @validator('email')
    def name_must_contain_at(cls, v):
        if '@' not in v:
            raise ValueError('must contain @ ')
        return v         

@app.get("/")
async def read_root():
    return {"Hello": "World"}




# Define the POST route handler
@app.post("/patient/")
async def create_patient(patient: Patient):
    # patient object is validated automatically by FastAPI
    patient_dict = patient.dict()
    contract.addUser(email = patient.email, name = patient.name, age = patient.age, gender = patient.gender,  number = patient.number).with_amount(0).as_transaction().fill().sign().inject()  
    return {"message": f"User {patient.name} with email {patient.email} created successfully"}