1. add donate setting api

This commit is contained in:
Jay 2018-08-17 00:50:13 +08:00
parent bebb970ef5
commit cb545db298
1 changed files with 33 additions and 2 deletions

View File

@ -8,6 +8,7 @@ const {
} = require('@libs/route-utils')
const r = new Router()
const evt = require('@root/event')
const koaBody = require('koa-body')
const typeTwitch = async (c, n) => {
let text = `select * from "public"."twitch_channel" where "id" = $1`
@ -94,12 +95,14 @@ r.put('/channel/:chid/opay', checkSession, getChannelList, hasChannel('chid'), a
c.obj = resObject('Success')
})
r.get('/channel/:chid/donation', checkSession, getChannelList, hasChannel('chid'), async (c, n) => {
r.get('/channel/:chid/opay/setting', checkSession, getChannelList, hasChannel('chid'), async (c, n) => {
let text = `select ds.* from "public"."donate_setting" ds
left join "public"."twitch_channel" tw
on tw.id = ds.twitch
where
tw.id = $1`
tw.id = $1
and tw.opayid != ''
limit 1`
let values = [c.state.channel.id]
let result = await c.db.query({
text,
@ -110,4 +113,32 @@ r.get('/channel/:chid/donation', checkSession, getChannelList, hasChannel('chid'
})
})
r.put('/channel/:chid/opay/setting', checkSession, getChannelList, hasChannel('chid'), koaBody(), async (c, n) => {
if (!c.chkBody('start', 'number') || !c.chkBody('end', 'number') || !c.chkBody('title', 'string') || !c.chkBody('amount', 'number')) throw genError('DataFormat')
let sts = toInt(c.request.body.start, 0, 0)
let ets = toInt(c.request.body.end, 0, 0)
let amount = toInt(c.request.body.amount, 0, 0)
let sd = new Date(sts)
let ed = new Date(ets)
let text = `insert into "public"."donate_setting" ("twitch", "start_date", "end_date", "target_amount", "title") values
($1, $2, $3, $4, $5) returning *
on CONFLICT ("twitch") DO UPDATE
set
"start_date" = $2,
"end_date" = $3,
"target_amount" = $4,
"title" = $5`
let values = [c.state.channel.id, sd, ed, amount, c.request.body.title]
let result = await c.db.query({
text,
values
})
console.log(result)
c.obj = resObject('Success')
})
module.exports = r