-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] create market category posts (#2)
## Description - [x] posts create - [x] post data store in mysql - [x] img data store in s3 bucket ~~Post also needs user_id, but there is not code about it(we have to get user_id using jwt_parse). So I will make it later and s3 code too. Because something has changed(docker-compose, api prefix)~~ ## Related Issue Closes #1
- Loading branch information
Showing
16 changed files
with
219 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from fastapi import APIRouter | ||
|
||
from app.api.routes import users | ||
from app.api.routes import markets | ||
|
||
|
||
router = APIRouter() | ||
router.include_router(users.router, tags=["users"], prefix="/user") | ||
router.include_router(markets.router, tags=["markets"], prefix="/market") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging | ||
from typing import Any, List | ||
from fastapi import APIRouter, Depends, HTTPException, UploadFile | ||
from sqlalchemy.orm import Session | ||
from app.api.dependencies import database | ||
from app.models.domain import markets | ||
from app.crud import crud_markets, crud_posts | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/create") | ||
def create_market_posts( | ||
*, | ||
db: Session = Depends(database.get_db), | ||
files: List[UploadFile], | ||
market_in: markets.MarketCreate = Depends() | ||
) -> Any: | ||
""" | ||
Create new user. | ||
""" | ||
# post data create | ||
post_data = crud_posts.create(db=db, obj_in=market_in, files=files) | ||
# room data create | ||
market_data = crud_markets.create(db=db, obj_in=market_in, post_id=post_data.id) | ||
if market_data and post_data: | ||
return {"message": "create success"} | ||
else: | ||
raise HTTPException( | ||
status_code=500, | ||
detail="create failed", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from sqlalchemy.orm import Session | ||
from app.models.domain import markets | ||
from app.models.schemas.markets import Market | ||
|
||
|
||
def create(db: Session, *, obj_in: markets.MarketCreate, post_id: int) -> Market: | ||
db_obj = None | ||
if obj_in.starting_price: | ||
db_obj = Market( | ||
starting_price=obj_in.starting_price, | ||
price=obj_in.price, | ||
auction=obj_in.auction, | ||
deadline=obj_in.deadline, | ||
post_id=post_id, | ||
) | ||
else: | ||
db_obj = Market( | ||
price=obj_in.price, | ||
auction=obj_in.auction, | ||
deadline=obj_in.deadline, | ||
post_id=post_id, | ||
) | ||
db.add(db_obj) | ||
db.commit() | ||
db.refresh(db_obj) | ||
return db_obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from sqlalchemy.orm import Session | ||
from app.models.domain import posts | ||
from app.models.schemas.posts import Post | ||
from app.services.aws import s3_upload | ||
from fastapi import HTTPException | ||
from fastapi import UploadFile | ||
from typing import List | ||
import datetime | ||
|
||
|
||
def create(db: Session, *, obj_in: posts.PostCreate, files: List[UploadFile]) -> Post: | ||
db_obj = Post( | ||
title=obj_in.title, | ||
content=obj_in.content, | ||
status=obj_in.status, | ||
category=obj_in.category, | ||
created_at=datetime.datetime.today(), | ||
) | ||
# image bool value insert | ||
db_obj.image = True if files else False | ||
db.add(db_obj) | ||
db.commit() | ||
db.refresh(db_obj) | ||
# if image exits, do uploading in s3 | ||
if files: | ||
s3_result = s3_upload( | ||
files=files, post_id=db_obj.id, user_email="[email protected]" | ||
) | ||
if not s3_result: | ||
raise HTTPException( | ||
status_code=500, | ||
detail="s3 upload failed", | ||
) | ||
return db_obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import Optional | ||
from pydantic import BaseModel | ||
import datetime | ||
from app.resources.status import Status | ||
|
||
|
||
class MarketCreate(BaseModel): | ||
post_id: Optional[int] = None | ||
starting_price: Optional[int] = None | ||
price: int | ||
auction: bool | ||
deadline: datetime.datetime | ||
title: str | ||
content: str | ||
status: Status | ||
category: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from pydantic import BaseModel | ||
from typing import Optional | ||
from app.resources.status import Status | ||
|
||
|
||
class PostCreate(BaseModel): | ||
title: str | ||
content: str | ||
status: Status | ||
category: str | ||
user_id: Optional[int] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from sqlalchemy import Boolean, Column, Integer, DateTime, ForeignKey | ||
from sqlalchemy.orm import relationship | ||
from typing import TYPE_CHECKING | ||
from app.db.base_class import Base | ||
|
||
if TYPE_CHECKING: | ||
from .posts import Post # noqa: F401 | ||
|
||
|
||
class Market(Base): | ||
id = Column(Integer, primary_key=True, autoincrement=True, index=True) | ||
starting_price = Column(Integer) | ||
price = Column(Integer, nullable=False) | ||
auction = Column(Boolean(), default=False) | ||
deadline = Column(DateTime, nullable=False) | ||
post_id = Column(Integer, ForeignKey("post.id")) | ||
post = relationship("Post", back_populates="market") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from sqlalchemy import Boolean, Column, Integer, String, DateTime, ForeignKey, Enum | ||
from sqlalchemy.orm import relationship | ||
from app.db.base_class import Base | ||
from typing import TYPE_CHECKING | ||
from app.resources.status import Status | ||
|
||
if TYPE_CHECKING: | ||
from .users import User # noqa: F401 | ||
from .markets import Market # noqa: F401 | ||
|
||
|
||
class Post(Base): | ||
id = Column(Integer, primary_key=True, autoincrement=True, index=True) | ||
title = Column(String(255), index=True) | ||
content = Column(String(255), index=True) | ||
user_id = Column(Integer) | ||
user_id = Column(Integer, ForeignKey("user.id")) | ||
user = relationship("User", back_populates="posts") | ||
status = Column(Enum(Status), nullable=False, default=Status.PROCESSING) | ||
created_at = Column(DateTime, nullable=False) | ||
category = Column(String(255), index=True) | ||
image = Column(Boolean(), default=False) | ||
market = relationship("Market", back_populates="post") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
from sqlalchemy import Boolean, Column, Integer, String | ||
|
||
from sqlalchemy import Column, Integer, String | ||
from sqlalchemy.orm import relationship | ||
from app.db.base_class import Base | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from .posts import Post # noqa: F401 | ||
|
||
|
||
class User(Base): | ||
id = Column(Integer, primary_key=True, autoincrement=True, index=True) | ||
username = Column(String(255), index=True) | ||
email = Column(String(255), unique=True, index=True, nullable=False) | ||
posts = relationship("Post", back_populates="user") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import enum | ||
|
||
|
||
class Status(enum.Enum): | ||
COMPLETED = "COMPLETED" | ||
PROCESSING = "PROCESSING" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import boto3 | ||
from botocore.exceptions import ClientError | ||
from fastapi import UploadFile | ||
from app.core.config import settings | ||
from typing import List | ||
|
||
|
||
def s3_upload(files: List[UploadFile], post_id: int, user_email: str) -> bool: | ||
client = boto3.client( | ||
"s3", | ||
region_name="us-east-1", | ||
aws_access_key_id=settings.AWS_ACCESS_KEY, | ||
aws_secret_access_key=settings.AWS_SECRET_KEY, | ||
) | ||
try: | ||
for file in files: | ||
new_filename = f"/{user_email}/{post_id}/{file.filename}" | ||
client.upload_fileobj( | ||
file.file, | ||
settings.BUCKET_NAME, | ||
new_filename, | ||
ExtraArgs={"ContentType": file.content_type}, | ||
) | ||
except ClientError as e: | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
services: | ||
app: | ||
build: . | ||
build: | ||
no_cache: true | ||
context: . | ||
ports: | ||
- "8000:8000" | ||
env_file: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters