2024-01-07 11:28:49 +00:00
|
|
|
from flask_bcrypt import Bcrypt
|
|
|
|
from models import db, User
|
|
|
|
|
|
|
|
bcrypt = Bcrypt()
|
|
|
|
|
|
|
|
class UserService:
|
|
|
|
@staticmethod
|
|
|
|
def create_user(data):
|
|
|
|
new_user = User(
|
|
|
|
name=data['username'],
|
|
|
|
email=data['email'],
|
|
|
|
location=data['location'],
|
|
|
|
password_hash=bcrypt.generate_password_hash(data['password']).decode('utf-8')
|
|
|
|
)
|
|
|
|
db.session.add(new_user)
|
|
|
|
db.session.commit()
|
2024-01-07 14:21:35 +00:00
|
|
|
return new_user.to_dict()
|
2024-01-07 11:28:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_all_users():
|
|
|
|
users = User.query.all()
|
|
|
|
return [user.to_dict() for user in users]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_user_by_email(email):
|
|
|
|
return User.query.filter_by(email=email).first()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def verify_user(data):
|
|
|
|
user = UserService.get_user_by_email(data['email'])
|
|
|
|
if user and bcrypt.check_password_hash(user.password_hash, data['password']):
|
|
|
|
return user
|
|
|
|
return None
|