-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
39 lines (27 loc) · 974 Bytes
/
main.py
File metadata and controls
39 lines (27 loc) · 974 Bytes
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, EmailStr, field_validator
products = []
class Products(BaseModel):
name: str
price: float
stock: float
@field_validator('name')
def check_name(cls, value):
if len(value) < 3:
raise HTTPException(status_code=400, detail="Nome inválido, no mínimo 3 caracteres")
return value
@field_validator('price')
def check_price(cls, value):
if value <= 0:
raise HTTPException(status_code=400, detail='Valor inválido, coloque um valor maior que zero')
return value
@field_validator('stock')
def check_stock(cls, value):
if value <= 0:
raise HTTPException(status_code=400, detail='Valor inválido, coloque um valor maior que zero')
return value
app = FastAPI()
@app.post('/products')
def create_student(product: Products):
products.append(product)
return products