Merge branch 'asynchronous-porridger' of https://git.dm1sh.ru/dm1sh/porridger into asynchronous-porridger

This commit is contained in:
DmitryGantimurov 2023-11-28 23:15:27 +03:00
commit e42293df3f
8 changed files with 17 additions and 13 deletions

View File

@ -89,7 +89,7 @@ async def put_in_db(name: Annotated[str, Form()], category: Annotated[str, Form(
if f.tell() > 0: if f.tell() > 0:
f.seek(0) f.seek(0)
destination = pathlib.Path("./uploads/" + str(hash(f)) + pathlib.Path(src.filename).suffix.lower()) destination = pathlib.Path("./uploads/" + str(hash(f)) + pathlib.Path(src.filename).suffix.lower())
async with destination.open('wb') as buffer: with destination.open('wb') as buffer:
shutil.copyfileobj(f, buffer) shutil.copyfileobj(f, buffer)
# изменяем название директории загруженного файла # изменяем название директории загруженного файла
@ -103,6 +103,8 @@ async def put_in_db(name: Annotated[str, Form()], category: Annotated[str, Form(
db.add(temp_ancmt) # добавляем в бд db.add(temp_ancmt) # добавляем в бд
await db.commit() # сохраняем изменения await db.commit() # сохраняем изменения
await db.refresh(temp_ancmt) # обновляем состояние объекта await db.refresh(temp_ancmt) # обновляем состояние объекта
return {"Success": True}
except: except:
raise HTTPException(status_code=500, detail="problem with adding object to db") raise HTTPException(status_code=500, detail="problem with adding object to db")
@ -119,6 +121,8 @@ async def delete_from_db(announcement: pydantic_schemas.DelAnnouncement, db: Ann
try: try:
await db.delete(to_delete) # удаление из БД await db.delete(to_delete) # удаление из БД
await db.commit() # сохраняем изменения await db.commit() # сохраняем изменения
return {"Success": True}
except: except:
raise HTTPException(status_code=500, detail="Problem with adding to database") raise HTTPException(status_code=500, detail="Problem with adding to database")

View File

@ -6,7 +6,7 @@ const composePutAnnouncementURL = () => (
) )
const processPutAnnouncement = (data: PutAnnouncementResponse): PutAnnouncement => { const processPutAnnouncement = (data: PutAnnouncementResponse): PutAnnouncement => {
return data.Answer return data.Success
} }
export { composePutAnnouncementURL, processPutAnnouncement } export { composePutAnnouncementURL, processPutAnnouncement }

View File

@ -1,12 +1,12 @@
import { isObject } from '../../utils/types' import { isObject } from '../../utils/types'
type PutAnnouncementResponse = { type PutAnnouncementResponse = {
Answer: boolean, Success: boolean,
} }
const isPutAnnouncementResponse = (obj: unknown): obj is PutAnnouncementResponse => ( const isPutAnnouncementResponse = (obj: unknown): obj is PutAnnouncementResponse => (
isObject(obj, { isObject(obj, {
'Answer': 'boolean', 'Success': 'boolean',
}) })
) )

View File

@ -6,11 +6,11 @@ const composeRemoveAnnouncementURL = () => (
) )
function processRemoveAnnouncement(data: RemoveAnnouncementResponse): RemoveAnnouncement { function processRemoveAnnouncement(data: RemoveAnnouncementResponse): RemoveAnnouncement {
if (!data.Answer) { if (!data.Success) {
throw new Error('Не удалось закрыть объявление') throw new Error('Не удалось закрыть объявление')
} }
return data.Answer return data.Success
} }
export { composeRemoveAnnouncementURL, processRemoveAnnouncement } export { composeRemoveAnnouncementURL, processRemoveAnnouncement }

View File

@ -1,12 +1,12 @@
import { isObject } from '../../utils/types' import { isObject } from '../../utils/types'
type RemoveAnnouncementResponse = { type RemoveAnnouncementResponse = {
Answer: boolean, Success: boolean,
} }
const isRemoveAnnouncementResponse = (obj: unknown): obj is RemoveAnnouncementResponse => ( const isRemoveAnnouncementResponse = (obj: unknown): obj is RemoveAnnouncementResponse => (
isObject(obj, { isObject(obj, {
'Answer': 'boolean', 'Success': 'boolean',
}) })
) )

View File

@ -14,8 +14,8 @@ function useAddAnnouncement() {
processPutAnnouncement processPutAnnouncement
) )
function handleAdd(formData: FormData) { async function handleAdd(formData: FormData) {
void doSend({}, { await doSend({}, {
body: formData, body: formData,
}) })
} }

View File

@ -15,8 +15,8 @@ function useBook() {
processBook, processBook,
) )
const handleBook = useCallback((id: number) => { const handleBook = useCallback(async (id: number) => {
void doSend({}, { await doSend({}, {
body: JSON.stringify({ body: JSON.stringify({
id, id,
}), }),

View File

@ -12,7 +12,7 @@ function useSendButtonCaption(
const [title, setTitle] = useState(initial) const [title, setTitle] = useState(initial)
const update = useCallback(<T extends NonNullable<unknown>>(data: T | null | undefined) => { const update = useCallback(<T extends NonNullable<unknown>>(data: T | null | undefined) => {
if (data !== undefined) { // not loading if (data !== undefined && data !== null) { // not loading or error
setCaption(result) setCaption(result)
setTitle('Отправить ещё раз') setTitle('Отправить ещё раз')