Skip to content

Commit 81334f5

Browse files
wow
1 parent 56968b2 commit 81334f5

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

carrot/app/product/repositories.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
from typing import Annotated, List
1+
from typing import List
22

3-
from fastapi import Depends
4-
from sqlalchemy import select, and_, text, or_
3+
from sqlalchemy import select, or_
54
from sqlalchemy.ext.asyncio import AsyncSession
65

7-
from carrot.app.user.models import LocalAccount, SocialAccount, User
86
from carrot.app.product.models import Product
9-
from carrot.db.connection import get_db_session
7+
from carrot.app.auction.models import Auction
108

119

1210
class ProductRepository:
13-
def __init__(self, session: Annotated[AsyncSession, Depends(get_db_session)]) -> None:
11+
def __init__(self, session: AsyncSession) -> None:
1412
self.session = session
1513

1614
async def create_post(self, product: Product) -> Product:
@@ -19,24 +17,30 @@ async def create_post(self, product: Product) -> Product:
1917
await self.session.refresh(product)
2018
return product
2119

20+
async def create_auction(self, product: Product, auction: Auction) -> Product:
21+
auction.product_id = product.id
22+
self.session.add(auction)
23+
await self.session.commit()
24+
await self.session.refresh(auction)
25+
return product
26+
2227
async def update_post(self, product: Product) -> Product:
2328
merged = await self.session.merge(product)
2429
await self.session.commit()
2530
await self.session.refresh(merged)
2631
return merged
27-
32+
2833
async def get_post_by_product_id(self, product_id: str) -> Product:
2934
query = select(Product).where(Product.id == product_id)
3035
posts = await self.session.execute(query)
31-
3236
return posts.scalars().one_or_none()
33-
37+
3438
async def get_posts_by_query(self, user_id: str, keyword: str, region_id: str) -> List[Product]:
3539
query = select(Product)
36-
40+
3741
if user_id:
3842
query = query.where(Product.owner_id == user_id)
39-
43+
4044
if keyword:
4145
search_pattern = f"%{keyword}%"
4246
query = query.where(
@@ -45,20 +49,18 @@ async def get_posts_by_query(self, user_id: str, keyword: str, region_id: str) -
4549
Product.content.ilike(search_pattern)
4650
)
4751
)
48-
52+
4953
if region_id:
5054
query = query.where(Product.region_id == region_id)
51-
55+
5256
posts = await self.session.execute(query)
53-
5457
return posts.scalars().all()
55-
58+
5659
async def get_posts_all(self) -> List[Product]:
5760
query = select(Product)
5861
posts = await self.session.execute(query)
59-
6062
return posts.scalars().all()
61-
63+
6264
async def remove_post(self, product: Product):
6365
await self.session.delete(product)
64-
await self.session.commit()
66+
await self.session.commit()

carrot/app/product/services.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,32 @@
99
from carrot.app.image.services import ImageService
1010
from carrot.common.exceptions import InvalidFormatException
1111

12+
from carrot.app.product.schemas import ProductPostRequest
13+
from carrot.app.auction.schemas import AuctionCreate
14+
1215
class ProductService:
1316
def __init__(self, product_repository: Annotated[ProductRepository, Depends()], image_service: Annotated[ImageService, Depends()]) -> None:
1417
self.repository = product_repository
1518
self.image_service = image_service
1619

17-
async def create_post(self, user_id: str, title: str, image_ids: list, content: str, price: int, category_id: str, region_id: str) -> Product:
20+
async def create_post(self, user_id: str, product_request: ProductPostRequest, region_id: str, auction_data: AuctionCreate) -> Product:
1821
product = Product(
1922
owner_id = user_id,
20-
title = title,
21-
image_ids = image_ids,
22-
content = content,
23-
price = price,
24-
category_id = category_id,
23+
title = product_request.title,
24+
image_ids = product_request.image_ids,
25+
content = product_request.content,
26+
price = product_request.price,
27+
category_id = product_request.category_id,
2528
region_id = region_id,
2629
)
27-
28-
new = await self.repository.create_post(product)
29-
return new
3030

31+
new_product = await self.repository.create_post(product)
32+
33+
if auction_data is not None:
34+
await self.repository.create_auction(new_product, auction_data)
35+
36+
return new_product
37+
3138
async def update_post(self, user_id: str, id: str, title: str, image_ids: list, content: str, price: int, category_id: str, region_id: str) -> Product:
3239
product = await self.repository.get_post_by_product_id(id)
3340

0 commit comments

Comments
 (0)