add I to interfaces. some formatting
parent
020ef2c4a3
commit
531db9fd02
|
|
@ -7,9 +7,9 @@ from fastapi.responses import HTMLResponse
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
#dependency imports
|
#dependency imports
|
||||||
from entities import Product
|
from entities.product import Product
|
||||||
from use_cases.interactors import ViewProduct, ViewAllProducts
|
from use_cases.interactors import ViewProduct, ViewAllProducts
|
||||||
from use_cases.interfaces.ports import ViewProductInputPort, ViewAllProductsInputPort, ViewProductOutputPort, ViewAllProductsOutputPort
|
from use_cases.interfaces.ports import IViewProductInputPort, IViewAllProductsInputPort, IViewProductOutputPort, IViewAllProductsOutputPort
|
||||||
from interface_adapters.dtos import ProductDTO
|
from interface_adapters.dtos import ProductDTO
|
||||||
from interface_adapters.repositories import SQLProductRepository
|
from interface_adapters.repositories import SQLProductRepository
|
||||||
|
|
||||||
|
|
@ -21,15 +21,19 @@ class ViewProductResponse(BaseModel):
|
||||||
status: str
|
status: str
|
||||||
data: ProductDTO
|
data: ProductDTO
|
||||||
|
|
||||||
class ViewProductPresenter(ViewProductOutputPort):
|
class ViewProductPresenter(IViewProductOutputPort):
|
||||||
def present(self, product:Product) -> ViewProductResponse:
|
def present(self, product:Product) -> ViewProductResponse:
|
||||||
"""Format the response data for API."""
|
"""Format the response data for API."""
|
||||||
product_dto=ProductDTO(id=product.id, name=product.name, description=product.description, price=product.price)
|
product_dto = ProductDTO(
|
||||||
|
id = product.id,
|
||||||
|
name = product.name,
|
||||||
|
description = product.description,
|
||||||
|
price = product.price)
|
||||||
response = ViewProductResponse(status="success",data=product_dto)
|
response = ViewProductResponse(status="success",data=product_dto)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
class ViewProductController:
|
class ViewProductController:
|
||||||
def __init__(self, input_port: ViewProductInputPort) -> None:
|
def __init__(self, input_port: IViewProductInputPort) -> None:
|
||||||
self.input_port = input_port
|
self.input_port = input_port
|
||||||
|
|
||||||
def handle_request(self, request:ViewProductRequest):
|
def handle_request(self, request:ViewProductRequest):
|
||||||
|
|
@ -46,7 +50,7 @@ class ViewAllProductsResponse(BaseModel):
|
||||||
status: str
|
status: str
|
||||||
data: List[ProductDTO]
|
data: List[ProductDTO]
|
||||||
|
|
||||||
class ViewAllProductsPresenter(ViewAllProductsOutputPort):
|
class ViewAllProductsPresenter(IViewAllProductsOutputPort):
|
||||||
def present(self, products:List[Product]) -> ViewAllProductsResponse:
|
def present(self, products:List[Product]) -> ViewAllProductsResponse:
|
||||||
"""Format the response data for API."""
|
"""Format the response data for API."""
|
||||||
products_dto=[ProductDTO(id=product.id, name=product.name, description=product.description, price=product.price) for product in products]
|
products_dto=[ProductDTO(id=product.id, name=product.name, description=product.description, price=product.price) for product in products]
|
||||||
|
|
@ -54,7 +58,7 @@ class ViewAllProductsPresenter(ViewAllProductsOutputPort):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
class ViewAllProductsController:
|
class ViewAllProductsController:
|
||||||
def __init__(self, input_port: ViewAllProductsInputPort) -> None:
|
def __init__(self, input_port: IViewAllProductsInputPort) -> None:
|
||||||
self.input_port = input_port
|
self.input_port = input_port
|
||||||
|
|
||||||
def handle_request(self):
|
def handle_request(self):
|
||||||
|
|
|
||||||
|
|
@ -12,17 +12,6 @@ class SQLProductRepository(IProductRepository):
|
||||||
self.conn = sqlite3.connect(db_file)
|
self.conn = sqlite3.connect(db_file)
|
||||||
self.conn.row_factory = sqlite3.Row
|
self.conn.row_factory = sqlite3.Row
|
||||||
|
|
||||||
def add_product(self, product):
|
|
||||||
"""Add a new product to the products table."""
|
|
||||||
sql = "INSERT INTO products (name, price, description, quantity) VALUES (?, ?, ?, ?)"
|
|
||||||
try:
|
|
||||||
c = self.conn.cursor()
|
|
||||||
c.execute(sql, product)
|
|
||||||
self.conn.commit()
|
|
||||||
return c.lastrowid
|
|
||||||
except sqlite3.Error as e:
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
def get_product_by_id(self, product_id) -> Optional[Product]:
|
def get_product_by_id(self, product_id) -> Optional[Product]:
|
||||||
"""Retrieve a product by its ID."""
|
"""Retrieve a product by its ID."""
|
||||||
sql = "SELECT * FROM products WHERE id = ?"
|
sql = "SELECT * FROM products WHERE id = ?"
|
||||||
|
|
@ -35,6 +24,17 @@ class SQLProductRepository(IProductRepository):
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def add_product(self, product):
|
||||||
|
"""Add a new product to the products table."""
|
||||||
|
sql = "INSERT INTO products (name, price, description, quantity) VALUES (?, ?, ?, ?)"
|
||||||
|
try:
|
||||||
|
c = self.conn.cursor()
|
||||||
|
c.execute(sql, product)
|
||||||
|
self.conn.commit()
|
||||||
|
return c.lastrowid
|
||||||
|
except sqlite3.Error as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
def get_all_products(self) -> Optional[List[Product]]:
|
def get_all_products(self) -> Optional[List[Product]]:
|
||||||
"""Retrieve all products."""
|
"""Retrieve all products."""
|
||||||
sql = "SELECT * FROM products"
|
sql = "SELECT * FROM products"
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ from typing import Optional, List
|
||||||
#dependency imports
|
#dependency imports
|
||||||
from entities import Product
|
from entities import Product
|
||||||
from use_cases.interfaces.repositories import IProductRepository
|
from use_cases.interfaces.repositories import IProductRepository
|
||||||
from use_cases.interfaces.ports import ViewAllProductsInputPort, ViewAllProductsOutputPort
|
from use_cases.interfaces.ports import IViewAllProductsInputPort, IViewAllProductsOutputPort
|
||||||
|
|
||||||
class ViewAllProducts(ViewAllProductsInputPort):
|
class ViewAllProducts(IViewAllProductsInputPort):
|
||||||
def __init__(self, product_repository: IProductRepository, output_port: ViewAllProductsOutputPort):
|
def __init__(self, product_repository: IProductRepository, output_port: IViewAllProductsOutputPort):
|
||||||
self.product_repository = product_repository
|
self.product_repository = product_repository
|
||||||
self.output_port = output_port
|
self.output_port = output_port
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ from typing import Optional
|
||||||
#dependency imports
|
#dependency imports
|
||||||
from entities import Product
|
from entities import Product
|
||||||
from use_cases.interfaces.repositories import IProductRepository
|
from use_cases.interfaces.repositories import IProductRepository
|
||||||
from use_cases.interfaces.ports import ViewProductInputPort, ViewProductOutputPort
|
from use_cases.interfaces.ports import IViewProductInputPort, IViewProductOutputPort
|
||||||
|
|
||||||
class ViewProduct(ViewProductInputPort):
|
class ViewProduct(IViewProductInputPort):
|
||||||
def __init__(self, product_repository: IProductRepository, output_port: ViewProductOutputPort):
|
def __init__(self, product_repository: IProductRepository, output_port: IViewProductOutputPort):
|
||||||
self.product_repository = product_repository
|
self.product_repository = product_repository
|
||||||
self.output_port = output_port
|
self.output_port = output_port
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
class ViewAllProductsInputPort:
|
class IViewAllProductsInputPort:
|
||||||
def execute(self):
|
def execute(self):
|
||||||
"""
|
"""
|
||||||
Abstract method to execute use case.
|
Abstract method to execute use case.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
from entities import Product
|
from entities import Product
|
||||||
from typing import List
|
from typing import List
|
||||||
class ViewAllProductsOutputPort:
|
class IViewAllProductsOutputPort:
|
||||||
def present(self, product_list:List[Product]):
|
def present(self, product_list:List[Product]):
|
||||||
"""
|
"""
|
||||||
Abstract method to execute use case.
|
Abstract method to execute use case.
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
class ViewProductInputPort:
|
class IViewProductInputPort:
|
||||||
def execute(self, product_id:int):
|
def execute(self, product_id:int):
|
||||||
"""
|
"""
|
||||||
Abstract method to execute use case.
|
Abstract method to execute use case.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
from entities import Product
|
from entities import Product
|
||||||
class ViewProductOutputPort:
|
class IViewProductOutputPort:
|
||||||
def present(self, product:Product):
|
def present(self, product:Product):
|
||||||
"""
|
"""
|
||||||
Abstract method to execute use case.
|
Abstract method to execute use case.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue