-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#123 Модуль API переведен на TypeScript
- Loading branch information
1 parent
8ae1ffc
commit 991bb43
Showing
15 changed files
with
184 additions
and
132 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,36 @@ | ||
import express, {Request, Response} from 'express'; | ||
import {config} from 'dotenv'; | ||
|
||
import {router as customReactionsRouter} from './custom-reactions'; | ||
import {router as settingsRouter} from './settings'; | ||
import {router as wordsRouter} from './words'; | ||
|
||
const router = express.Router(); | ||
config(); | ||
|
||
function checkToken(req: Request, res: Response, next: () => void): void { | ||
const receivedToken = req.body.token || req.query.token; | ||
const actualToken = process.env.VK_ACCESS_TOKEN; | ||
if (receivedToken === actualToken) { | ||
req.body.demo = false; | ||
next(); | ||
} else if (receivedToken && receivedToken.toUpperCase() === 'DEMO') { | ||
req.body.demo = true; | ||
next(); | ||
} else { | ||
res.json({ error: 'token' }); | ||
} | ||
} | ||
|
||
router.use(checkToken); | ||
|
||
router.use('/words', wordsRouter); | ||
router.use('/settings', settingsRouter); | ||
router.use('/customReactions', customReactionsRouter); | ||
|
||
router.post('/', (req, res) => { | ||
res.json({ success: true, demo: req.body.demo }); | ||
}); | ||
|
||
export { router }; | ||
|
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,4 @@ | ||
export enum ErrorType { | ||
INTERNAL = 'internal', | ||
DUPLICATE = 'duplicate', | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/api/model/custom-reactions-dto/CustomReactionDto.ts
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,12 @@ | ||
import {CustomReactionPhraseDto } from './CustomReactionPhraseDto'; | ||
import {CustomReactionStickerDto} from './CustomReactionStickerDto'; | ||
import {CustomReactionResponseDto} from './CustomReactionResponseDto'; | ||
|
||
|
||
export type CustomReactionDto = { | ||
id: number, | ||
probability: number, | ||
phrases: CustomReactionPhraseDto[], | ||
stickers: CustomReactionStickerDto[], | ||
responses: CustomReactionResponseDto[], | ||
} |
4 changes: 4 additions & 0 deletions
4
app/src/api/model/custom-reactions-dto/CustomReactionPhraseDto.ts
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,4 @@ | ||
export type CustomReactionPhraseDto = { | ||
id: number; | ||
text: string; | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/api/model/custom-reactions-dto/CustomReactionPhraseUpdate.ts
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,5 @@ | ||
import {CustomReactionPhraseDto} from './CustomReactionPhraseDto'; | ||
|
||
export type CustomReactionPhraseUpdate = CustomReactionPhraseDto & { | ||
deleted: boolean; | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/api/model/custom-reactions-dto/CustomReactionResponseDto.ts
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,5 @@ | ||
export type CustomReactionResponseDto = { | ||
id: number; | ||
type: number; | ||
content: string; | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/api/model/custom-reactions-dto/CustomReactionResponseUpdate.ts
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,5 @@ | ||
import {CustomReactionResponseDto} from './CustomReactionResponseDto'; | ||
|
||
export type CustomReactionResponseUpdate = CustomReactionResponseDto & { | ||
deleted: boolean; | ||
} |
4 changes: 4 additions & 0 deletions
4
app/src/api/model/custom-reactions-dto/CustomReactionStickerDto.ts
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,4 @@ | ||
export type CustomReactionStickerDto = { | ||
id: number; | ||
stickerId: number; | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/api/model/custom-reactions-dto/CustomReactionStickerUpdate.ts
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,5 @@ | ||
import {CustomReactionStickerDto} from './CustomReactionStickerDto'; | ||
|
||
export type CustomReactionStickerUpdate = CustomReactionStickerDto & { | ||
deleted: boolean; | ||
} |
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,5 @@ | ||
export type WordDto = { | ||
id: number; | ||
name: string; | ||
isApproved: boolean; | ||
} |
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
import express from 'express'; | ||
import db from '../db'; | ||
import {ErrorType} from './model/ErrorType'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/', async (req, res) => { | ||
|
||
try { | ||
const stateRows = await db.query<{key: string; value: string}>('SELECT * FROM friends_vk_bot.state;'); | ||
const ads = stateRows.rows.find(row => row.key === 'ads')!.value; | ||
const absentHolidaysPhrases = stateRows.rows.find(row => row.key === 'absent_holidays_phrases')!.value; | ||
res.json({ ads, absentHolidaysPhrases }); | ||
} catch(error) { | ||
console.log(error); | ||
res.json({ error: ErrorType.INTERNAL }); | ||
} | ||
}); | ||
|
||
router.post('/', async (req, res) => { | ||
if (req.body.demo) { | ||
res.json({ success: true }); | ||
return; | ||
} | ||
|
||
const newAds = req.body.ads; | ||
const newAbsentHolidaysPhrases = req.body.absentHolidaysPhrases; | ||
const query = ` | ||
UPDATE friends_vk_bot.state SET value = '${ newAds }' WHERE key = 'ads'; | ||
UPDATE friends_vk_bot.state SET value = '${ newAbsentHolidaysPhrases }' WHERE key = 'absent_holidays_phrases'; | ||
`; | ||
|
||
try { | ||
await db.query(query); | ||
res.json({ success: true }); | ||
} catch(error) { | ||
console.log(error); | ||
res.json({ error: ErrorType.INTERNAL }); | ||
} | ||
}); | ||
|
||
export { router }; |
Oops, something went wrong.