This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
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.
Showing
7 changed files
with
132 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import datetime | ||
|
||
from fastapi import UploadFile | ||
from pydantic import BaseModel, field_validator | ||
|
||
|
||
class Recipe(BaseModel): | ||
id: int | ||
set_id: int | ||
user_id: int | ||
title: str | ||
description: str | ||
image_url: str | ||
create_time: datetime.datetime | ||
|
||
|
||
class RecipeCreate(BaseModel): | ||
set_id: int | ||
title: str | ||
description: str | ||
|
||
@field_validator('title', 'description') | ||
def not_empty(cls, v): | ||
if not v or not v.strip(): | ||
raise ValueError('빈 값은 허용되지 않습니다.') | ||
return v | ||
|
||
|
||
class RecipeUsePerfume(BaseModel): | ||
id: int | ||
recipe_id: int | ||
cartridge_id: int | ||
count: int | ||
|
||
|
||
class RecipeUsePerfumeCreate(BaseModel): | ||
recipe_id: int | ||
cartridge_id: int | ||
count: int |
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,22 @@ | ||
from passlib.context import CryptContext | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy import func | ||
|
||
from core.models import models | ||
from core.models.models import UserRecipe, UserRecipeUsePerfume | ||
from core.schemas.recipe import RecipeCreate | ||
from datetime import datetime | ||
|
||
|
||
def create_recipe(db: Session, image_url: str, recipe_create: RecipeCreate, current_user: models.User | None = None): | ||
db_recipe = UserRecipe( | ||
user_id=1, | ||
set_id=recipe_create.set_id, | ||
title=recipe_create.title, | ||
description=recipe_create.description, | ||
image_url=image_url, | ||
create_time=datetime.now(), | ||
) | ||
db.add(db_recipe) | ||
db.commit() | ||
|
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,59 @@ | ||
from datetime import timedelta, datetime | ||
from typing import Optional, Annotated | ||
|
||
import aiofiles | ||
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Form | ||
from fastapi.security import OAuth2PasswordRequestForm, OAuth2PasswordBearer | ||
from jose import jwt | ||
from sqlalchemy.orm import Session | ||
from starlette import status | ||
from starlette.config import Config | ||
import hashlib | ||
import json | ||
|
||
from core.database import get_db | ||
from core.models import models | ||
from core.models.models import UserRecipe, UserRecipeUsePerfume | ||
from core.schemas import recipe | ||
from core.schemas import tag | ||
from dependencies import get_current_user | ||
import routers.recipe.recipe_crud as recipe_crud | ||
|
||
config = Config('.env') | ||
|
||
router = APIRouter( | ||
prefix="/api/recipe", | ||
tags=["Recipe"] | ||
) | ||
|
||
# Todo 계정 관련하여 추가하여야 함. | ||
# Create | ||
@router.post("/", status_code=status.HTTP_204_NO_CONTENT, | ||
description='new_recipe는 다음과 같이 입력해주세요. new_recipe={"set_id": 0, "title": "string", "description": "string"}') | ||
async def create_recipe(new_recipe: str = Form(...), | ||
file: UploadFile = File(None), | ||
db: Session = Depends(get_db) | ||
): | ||
data = json.loads(new_recipe.replace("new_recipe=", "")) | ||
create_recipe = recipe.RecipeCreate(**data) | ||
|
||
if file: | ||
file_name = (hashlib.sha256( | ||
f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}{file.filename}".encode()) | ||
.hexdigest() + "." + file.filename.split('.')[-1]) | ||
|
||
file_location = f"./static/img/recipe/" | ||
|
||
# 파일 경로가 없다면 생성 | ||
import os | ||
if not os.path.exists(os.path.dirname(file_location)): | ||
os.makedirs(file_location) | ||
|
||
file_location = os.path.join(file_location,file_name) | ||
async with aiofiles.open(file_location, 'wb+') as f: | ||
while content := await file.read(1024): | ||
await f.write(content) | ||
else: | ||
file_location = None | ||
|
||
recipe_crud.create_recipe(db, file_location, create_recipe) |
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