-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.py
36 lines (27 loc) · 1.01 KB
/
users.py
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
from typing import List
from pydantic import EmailStr
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import Session
from config.database import Base
from schemas.users import UserSchemaIn
class UserModel(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True, autoincrement=True)
email = Column(String, unique=True, nullable=False)
@classmethod
def create(cls, user: UserSchemaIn, db: Session) -> "UserModel":
user = cls(**user.model_dump())
db.add(user)
db.commit()
db.refresh(user)
return user
@classmethod
def get_all(cls, db: Session) -> List["UserModel"]:
return db.query(cls).order_by(cls.id).all()
@classmethod
def get_by_email(cls, email: EmailStr, db: Session) -> "UserModel":
return db.query(cls).filter_by(email=email).first()
@classmethod
def validate_by_email(cls, email: str, db: Session) -> bool:
user = cls.get_by_email(email, db)
return True if user else False