blog CRUD 추가
This commit is contained in:
1 parent
f9a30d6b39
commit
eb252307e8
4 files changed
+141
-21
No files matched your search
@@ -106,6 +106,17 @@ async def update_user_phone_from_id(user_id:int, phone_number:str=None):
|
||||
async def delete_user_from_id(user_id:int):
|
||||
async with await get_cnx() as cnx:
|
||||
async with await cnx.cursor() as cur:
|
||||
count_query = '''
|
||||
SELECT count(*)
|
||||
FROM user_table
|
||||
WHERE user_id = %(user_id)s
|
||||
'''
|
||||
await cur.execute(count_query, {'user_id' : user_id})
|
||||
found_rows = await cur.fetchone()
|
||||
if found_rows[0] == 0:
|
||||
await cnx.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
query = '''
|
||||
DELETE
|
||||
FROM user_table
|
||||
@@ -122,5 +133,107 @@ async def delete_user_from_id(user_id:int):
|
||||
await cnx.commit()
|
||||
return
|
||||
|
||||
async def create_blog(blog_owner:int, blog_title:str, blog_content:str = None):
|
||||
async with await get_cnx() as cnx:
|
||||
async with await cnx.cursor() as cur:
|
||||
query = '''
|
||||
INSERT INTO blog_table (blog_title, blog_content, blog_owner)
|
||||
VALUES (%(blog_title)s, %(blog_content)s, %(blog_owner)s)
|
||||
'''
|
||||
data = {
|
||||
"blog_owner" : blog_owner,
|
||||
"blog_title" : blog_title,
|
||||
"blog_content" : blog_content
|
||||
}
|
||||
try:
|
||||
await cur.execute(query, data)
|
||||
except IntegrityError as e:
|
||||
await cnx.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="blog already exist")
|
||||
|
||||
blog_id = cur.lastrowid
|
||||
print(blog_id)
|
||||
await cnx.commit()
|
||||
return blog_id
|
||||
|
||||
async def get_blog_info_from_id(blog_id:int):
|
||||
async with await get_cnx() as cnx:
|
||||
async with await cnx.cursor() as cur:
|
||||
query = '''
|
||||
SELECT blog_owner, blog_title, blog_content FROM blog_table
|
||||
WHERE blog_id=%(blog_id)s
|
||||
'''
|
||||
data = {"blog_id" : blog_id}
|
||||
|
||||
await cur.execute(query, data)
|
||||
user_info = await cur.fetchone()
|
||||
await cnx.commit()
|
||||
if not user_info:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||
return user_info
|
||||
|
||||
async def update_blog_content_from_id(blog_id:int, blog_content:str=None):
|
||||
async with await get_cnx() as cnx:
|
||||
async with await cnx.cursor() as cur:
|
||||
count_query = '''
|
||||
SELECT count(*)
|
||||
FROM blog_table
|
||||
WHERE blog_id = %(blog_id)s
|
||||
'''
|
||||
await cur.execute(count_query, {'blog_id' : blog_id})
|
||||
found_rows = await cur.fetchone()
|
||||
if found_rows[0] == 0:
|
||||
await cnx.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
query = '''
|
||||
UPDATE blog_table
|
||||
SET blog_content = %(blog_content)s
|
||||
WHERE blog_id = %(blog_id)s
|
||||
'''
|
||||
data = {
|
||||
"blog_id" : blog_id,
|
||||
"blog_content" : blog_content
|
||||
}
|
||||
|
||||
await cur.execute(query, data)
|
||||
|
||||
if cur.rowcount == 0:
|
||||
await cnx.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
await cnx.commit()
|
||||
return data
|
||||
|
||||
|
||||
async def delete_blog_from_id(blog_id:int):
|
||||
async with await get_cnx() as cnx:
|
||||
async with await cnx.cursor() as cur:
|
||||
count_query = '''
|
||||
SELECT count(*)
|
||||
FROM blog_table
|
||||
WHERE blog_id = %(blog_id)s
|
||||
'''
|
||||
await cur.execute(count_query, {'blog_id' : blog_id})
|
||||
found_rows = await cur.fetchone()
|
||||
if found_rows[0] == 0:
|
||||
await cnx.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
query = '''
|
||||
DELETE
|
||||
FROM blog_table
|
||||
WHERE blog_id = %(blog_id)s
|
||||
'''
|
||||
data = {
|
||||
"blog_id" : blog_id
|
||||
}
|
||||
try:
|
||||
await cur.execute(query, data)
|
||||
except IntegrityError as e:
|
||||
await cnx.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="already exist")
|
||||
await cnx.commit()
|
||||
return
|
||||
|
||||
|
||||
@@ -16,20 +16,16 @@ class UserDeleteForm(BaseModel):
|
||||
|
||||
|
||||
class BlogCreateForm(BaseModel):
|
||||
owner:int
|
||||
blog_owner:int
|
||||
blog_title:str
|
||||
blog_content:str
|
||||
|
||||
class BlogReadForm(BaseModel):
|
||||
owner:int
|
||||
class BlogReadFromIdForm(BaseModel):
|
||||
blog_id:int
|
||||
|
||||
class BlogUpdateForm(BaseModel):
|
||||
owner:int
|
||||
class BlogUpdateContentFromIdForm(BaseModel):
|
||||
blog_id:int
|
||||
blog_title:str
|
||||
blog_content:str
|
||||
|
||||
class BlogDeleteForm(BaseModel):
|
||||
owner:int
|
||||
blog_id:int
|
||||
Reference in new issue
Block a user