feat: simulasi repository pattern
This commit is contained in:
parent
ddcf4d0c90
commit
a89279ee65
|
@ -0,0 +1,2 @@
|
||||||
|
from .auth import auth_blueprint
|
||||||
|
from .user import user_blueprint
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,22 @@
|
||||||
|
from flask import Blueprint
|
||||||
|
from app.controllers import AuthController
|
||||||
|
|
||||||
|
# Inisialisasi blueprint
|
||||||
|
auth_blueprint = Blueprint("auth", __name__)
|
||||||
|
auth_controller = AuthController()
|
||||||
|
|
||||||
|
|
||||||
|
# Daftarkan rute ke controller
|
||||||
|
@auth_blueprint.route("/register", methods=["POST"])
|
||||||
|
def register():
|
||||||
|
return auth_controller.register()
|
||||||
|
|
||||||
|
|
||||||
|
@auth_blueprint.route("/login", methods=["POST"])
|
||||||
|
def login():
|
||||||
|
return auth_controller.login()
|
||||||
|
|
||||||
|
|
||||||
|
@auth_blueprint.route("/logout", methods=["DELETE"])
|
||||||
|
def logout():
|
||||||
|
return auth_controller.logout()
|
|
@ -1,7 +1,7 @@
|
||||||
# /blueprints/user.py
|
# /blueprints/user.py
|
||||||
|
|
||||||
from flask import Blueprint, jsonify
|
from flask import Blueprint
|
||||||
from controllers.user_controller import UserController
|
from app.controllers import UserController
|
||||||
|
|
||||||
user_blueprint = Blueprint("user", __name__)
|
user_blueprint = Blueprint("user", __name__)
|
||||||
user_controller = UserController()
|
user_controller = UserController()
|
|
@ -0,0 +1,2 @@
|
||||||
|
from .auth_controller import AuthController
|
||||||
|
from .user_controller import UserController
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,19 @@
|
||||||
|
from flask import jsonify, request
|
||||||
|
from app.services import UserService
|
||||||
|
from app.services import AuthService
|
||||||
|
|
||||||
|
|
||||||
|
class AuthController:
|
||||||
|
def __init__(self):
|
||||||
|
self.user_service = UserService()
|
||||||
|
self.auth_service = AuthService()
|
||||||
|
|
||||||
|
def login(self):
|
||||||
|
data = request.get_json()
|
||||||
|
users = self.auth_service.login(data)
|
||||||
|
response = {
|
||||||
|
"status": True,
|
||||||
|
"message": "success retrive data",
|
||||||
|
"data": users,
|
||||||
|
}
|
||||||
|
return jsonify(response)
|
|
@ -1,6 +1,6 @@
|
||||||
# /controllers/user_controller.py
|
# /controllers/user_controller.py
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
from services.user_service import UserService
|
from app.services import UserService
|
||||||
|
|
||||||
|
|
||||||
class UserController:
|
class UserController:
|
|
@ -0,0 +1 @@
|
||||||
|
from .user_repository import UserRepository
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,24 @@
|
||||||
|
class UserRepository:
|
||||||
|
users = [
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "akhdan",
|
||||||
|
"email": "akhdanre@gmail.com",
|
||||||
|
"password": "password123",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"name": "Bob",
|
||||||
|
"email": "bob@example.com",
|
||||||
|
"password": "password123",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_all_users(self):
|
||||||
|
return self.users
|
||||||
|
|
||||||
|
def get_user_by_email(self, email):
|
||||||
|
for user in self.users:
|
||||||
|
if user.get("email") == email:
|
||||||
|
return user
|
||||||
|
return None
|
|
@ -0,0 +1,2 @@
|
||||||
|
from .auth_service import AuthService
|
||||||
|
from .user_service import UserService
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,21 @@
|
||||||
|
from app.repositories import UserRepository
|
||||||
|
|
||||||
|
|
||||||
|
class AuthService:
|
||||||
|
def __init__(self):
|
||||||
|
self.user_repository = UserRepository()
|
||||||
|
|
||||||
|
def login(self, data):
|
||||||
|
email = data.get("email")
|
||||||
|
password = data.get("password")
|
||||||
|
|
||||||
|
user_data = self.user_repository.get_user_by_email(email)
|
||||||
|
|
||||||
|
if user_data == None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if user_data["password"] == password:
|
||||||
|
del user_data["password"]
|
||||||
|
return user_data
|
||||||
|
|
||||||
|
return None
|
|
@ -1,6 +1,4 @@
|
||||||
# /services/user_service.py
|
from app.repositories import UserRepository
|
||||||
|
|
||||||
from repositories.user_repository import UserRepository
|
|
||||||
|
|
||||||
|
|
||||||
class UserService:
|
class UserService:
|
Binary file not shown.
Binary file not shown.
3
main.py
3
main.py
|
@ -1,8 +1,9 @@
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from blueprints.user import user_blueprint
|
from app.blueprints import auth_blueprint, user_blueprint
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
app.register_blueprint(auth_blueprint, url_prefix="/api")
|
||||||
app.register_blueprint(user_blueprint, url_prefix="/api")
|
app.register_blueprint(user_blueprint, url_prefix="/api")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,3 +0,0 @@
|
||||||
class UserRepository:
|
|
||||||
def get_all_users(self):
|
|
||||||
return [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue