Skip to content

Commit

Permalink
Merge pull request #2 from woodnext/user_id
Browse files Browse the repository at this point in the history
Fix bug
  • Loading branch information
woodnx authored Aug 28, 2023
2 parents fd1a8a1 + 6977afa commit 8e2bed2
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 72 deletions.
3 changes: 1 addition & 2 deletions src/day.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ function generateRange(basicDate: string, period: ManipulateType){
const start: string = date.subtract(idx, period).startOf(period).format('YYYY-MM-DD HH:mm:ss')
return [ start, end ]
})

console.log(ranges)

return ranges
}

Expand Down
31 changes: 16 additions & 15 deletions src/routes/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const router = express.Router()
router.get('/status/:date/:period', async (req, res) => {
const userId = req.userId
const date = req.params.date
const _period = req.query.period || 'day'
const _period = req.params.period

const period: Period = (_period == 'week' || _period == 'month') ? _period : 'day'

Expand All @@ -23,32 +23,32 @@ router.get('/status/:date/:period', async (req, res) => {
.count('quiz_id', {as: 'count'})
.where('user_id', userId)
.where('judgement', 1)
.whereBetween('practiced', [range[0], range[1]])),
.whereBetween('practiced', [range[0], range[1]]))[0].count,

wrong: (await knex('histories')
.count('quiz_id', {as: 'count'})
.where('user_id', userId)
.where('judgement', 0)
.whereBetween('practiced', [range[0], range[1]])),
.whereBetween('practiced', [range[0], range[1]]))[0].count,

through: (await knex('histories')
.count('quiz_id', {as: 'count'})
.where('user_id', userId)
.where('judgement', 2)
.whereBetween('practiced', [range[0], range[1]])),
.whereBetween('practiced', [range[0], range[1]]))[0].count,
}))
)
res.send(results)
res.status(200).send(results)
} catch(e) {
console.error(e)
res.send('An Error Occured')
}
})

router.get('/ranking/all', async (req, res) => {
const _period = req.query.period || 'day'
const limit = (!!req.query.limit || Number(req.query.limit) > 6) ? Number(req.query.limit) : 6
const now = dayjs().format('YYYY-MM-DD HH:mm:ss')
router.get('/ranking/all/:period', async (req, res) => {
const _period = req.params.period;
const limit = (!!req.query.limit || Number(req.query.limit) > 5) ? Number(req.query.limit) : 5
const now = dayjs().format()

const period: Period = (_period == 'week' || _period == 'month') ? _period : 'day'

Expand All @@ -63,7 +63,7 @@ router.get('/ranking/all', async (req, res) => {
count: knex.raw('COUNT(quiz_id)')
})
.innerJoin('users', 'user_id', 'users.id')
.whereBetween('practiced', [ ranges[6][0], ranges[6][1] ])
.whereBetween('practiced', [ ranges[0][0], ranges[0][1] ])
.groupBy('user_id')
.orderBy('rank')
.limit(limit)
Expand Down Expand Up @@ -91,16 +91,16 @@ router.get('/ranking/all', async (req, res) => {
}
})

res.send(ranking)
res.status(200).send(ranking)
}catch(e) {
console.error(e)
res.send('An Error Occured')
}

})

router.get('/ranking/account/', async (req, res) => {
const _period = req.query.period
router.get('/ranking/personal/:period', async (req, res) => {
const _period = req.params.period
const userId = req.userId
const now = dayjs().format('YYYY-MM-DD HH:mm:ss')

Expand Down Expand Up @@ -147,10 +147,11 @@ router.get('/ranking/account/', async (req, res) => {
rank: nowRank.length,
name: isNodata ? '' : nowRank[0].name,
count: isNodata ? 0 : nowRank[0].count,
compare: nowRank.length - prevRank.length
compare: nowRank.length - prevRank.length,
userId,
}

res.send(ranking)
res.status(200).send(ranking)
}catch(e){
console.error(e)
res.send('An Error Occured')
Expand Down
4 changes: 2 additions & 2 deletions src/routes/answers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ router.get('/', async (req, res) => {
.where('quiz_id', quizId)
.where('user_id', userId)

res.send({
res.status(200).send({
fast,
avg,
myFast,
Expand All @@ -34,7 +34,7 @@ router.get('/', async (req, res) => {

router.post('/', async (req, res) => {
const quizId = req.body.quizId
const userId = req.body.userId
const userId = req.userId
const length = req.body.length

try {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ router.post('/', async (req, res) => {
})

router.delete('/', async (req, res) => {
const userId = req.body.userId
const userId = req.userId
const quizId = req.body.quizId

try {
Expand Down
39 changes: 16 additions & 23 deletions src/routes/histories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,28 @@ interface SendData {

const router: Router = express.Router()

router.get('/:user_id/:start/:end', async (req, res) => {
const start = `${req.params.start} 00:00:00`
const end = `${req.params.end} 23:59:59`
const user_id = Number(req.params.user_id)
router.get('/:since/:until', async (req, res) => {
const since = dayjs(Number(req.params.since)).format('YYYY-MM-DD HH:mm:ss');
const until = dayjs(Number(req.params.until)).format('YYYY-MM-DD HH:mm:ss');
const user_id = req.userId;

try {
const results = await Promise.all(
[0, 1, 2].map(async i => {
const r = await knex('histories')
[0, 1, 2].map(async i => (await knex('histories')
.count('quiz_id', {as: 'count'})
.where('user_id', user_id)
.andWhere('judgement', i)
.andWhereBetween('practiced', [ start, end ])
.first()

return !!r ? r.count : 0
}))
.andWhereBetween('practiced', [ since, until ])
.first())?.count
));

const data: SendData = {
right: results[1],
wrong: results[0],
through: results[2],
}
right: results[1] || 0,
wrong: results[0] || 0,
through: results[2] || 0,
};

res.send(data)
res.status(200).send(data);
} catch(e) {
console.error(e)
}
Expand All @@ -45,20 +42,16 @@ router.post('/', async (req, res) => {
const judgement: number = req.body.judgement
const practiced: string = dayjs().format('YYYY-MM-DD HH:mm:ss')

if (!quiz_id || !judgement) {
if (!quiz_id || judgement >= 3 || judgement < 0) {
res.status(400).send('Undefined list name or judgement')
return
}

try {
const uid = req.user.uid
const user_id = req.userId

await knex.transaction(async trx => {
const user_id: number = await trx('users')
.select('id')
.where("uid", uid)
.first()


const data = {
quiz_id,
user_id,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/levels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const router: Router = express.Router()
router.get('/', async (req, res) => {
try {
const result = await knex('levels').select('*')
res.send(result)
res.status(200).send(result)
} catch(err) {
console.error(err)
}
Expand Down
32 changes: 14 additions & 18 deletions src/routes/mylists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import dayjs from '../day'

const router: Router = express.Router()

router.get('/:userId', async (req, res) => {
router.get('/', async (req, res) => {
const userId = req.userId;
try {
const all = await knex('mylist_informations')
.select('name', 'id')
.where('user_id', req.query.userId)
.where('user_id', userId);

res.status(200).send(all)
res.status(200).send(all);
} catch(e) {
console.error(e)
}
})

router.post('/', async (req, res) => {
const listName = req.body.listName
const userId = req.userId
const now = dayjs().format('YYYY-MM-DD HH:mm:ss')

if (!listName) {
Expand All @@ -26,25 +28,20 @@ router.post('/', async (req, res) => {
}

try {
const uid = req.user.uid

await knex.transaction(async trx => {
const user_id: number = await trx('users')
.select('id')
.where("uid", uid)
.first()

const data = {
user_id,
user_id: userId,
name: listName,
created: now,
attr: 100
}
};

const inserts = await trx('mylist_informations').insert(data)
const message = `${inserts.length} new mylists saved (user: ${user_id})`
const inserts = await trx('mylist_informations').insert(data);
const newList = await trx('mylist_informations').select().where('name', listName).first();
const message = `${inserts.length} new mylists saved (user: ${userId})`;

res.status(201).send(message)
res.status(201).send(newList);
console.log(message)
})
} catch(e) {
Expand Down Expand Up @@ -84,7 +81,7 @@ router.put('/quiz', async (req, res) => {

router.put('/rename', async (req, res) => {
const mylistId = req.body.mylistId
const userId = req.body.userId
const userId = req.userId
const newName = req.body.newName

if (!userId || !mylistId || !newName) {
Expand Down Expand Up @@ -131,8 +128,7 @@ router.delete('/quiz', async (req, res) => {

router.delete('/list', async (req, res) => {
const mylistId = req.body.mylistId
const userId = req.body.userId

const userId = req.userId

try {
await knex.transaction(async trx => {
Expand All @@ -142,7 +138,7 @@ router.delete('/list', async (req, res) => {

await trx('mylist_informations')
.del()
.where('mylist_id', mylistId)
.where('id', mylistId)

const allList = await trx('mylist_informations')
.select('name', 'id')
Expand Down
24 changes: 16 additions & 8 deletions src/routes/quizzes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express, { Router, Request } from 'express'
import knex from '../knex'
import dayjs from 'dayjs'

const router: Router = express.Router()

Expand All @@ -15,9 +16,10 @@ interface QuizRequest extends Request {
keyword: string,
keywordOption: KeywordOption,
crctAnsRatio?: [string, string],
sinceDate?: string,
endDate?: string,
since?: string,
until?: string,
judgement?: string[],
mylistId?: string,
}
}

Expand All @@ -39,9 +41,10 @@ router.get('/:listName?', async (req: QuizRequest, res) => {
const keyword = req.query.keyword
const keywordOption = Number(req.query.keywordOption) || undefined
const crctAnsRatio = req.query.crctAnsRatio
const since = req.query.sinceDate
const until = req.query.endDate
const since = req.query.since
const until = req.query.until
const judgement = req.query.judgement
const mylistId = req.query.mylistId

const userId = req.userId
const listName = req.params.listName
Expand Down Expand Up @@ -115,19 +118,24 @@ router.get('/:listName?', async (req: QuizRequest, res) => {
.where('favorites.user_id', userId)
.orderBy('favorites.registered', 'desc')
}else if (listName === 'history' && !!since && !!until) {
const s = dayjs(Number(since)).format('YYYY-MM-DD HH:mm:ss');
const u = dayjs(Number(until)).format('YYYY-MM-DD HH:mm:ss');
builder.column(
{ judgement: 'histories.judgement' },
{ practiced: 'histories.practiced' },
)
.innerJoin('histories', 'histories.quiz_id', 'quizzes.id')
.where('histories.user_id', userId)
.whereBetween('histories.practiced', [since, until])
.whereBetween('histories.practiced', [s, u])
.orderBy('histories.practiced', 'desc')

if (!!judgement) builder.whereIn('histories.judgement', judgement)
}else {
}else if (listName === 'mylist') {
builder
.innerJoin('mylists_quizzes', 'mylists_quizzes.quiz_id', 'quizzes.id')
//.innerJoin('mylist_informations', 'mylist_informations.')
.innerJoin('mylist_informations', 'mylist_informations.id', 'mylists_quizzes.mylist_id')
.where('mylist_informations.id', mylistId)
.orderBy('mylists_quizzes.registered', 'desc');
}

//console.log(builder.toSQL())
Expand Down Expand Up @@ -171,7 +179,7 @@ router.get('/:listName?', async (req: QuizRequest, res) => {

//console.log(data)

res.send(data)
res.status(200).send(data)
} catch(e) {
console.log('An Error Occurred')
console.error(e)
Expand Down
4 changes: 2 additions & 2 deletions src/routes/workbooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const router: Router = express.Router()
router.get('/', async (req, res) => {
try {
const results = await knex('workbooks').select('*')
res.send(results)
res.status(200).send(results)
} catch(err) {
console.error(err)
}
Expand All @@ -22,7 +22,7 @@ router.get('/color', async (req, res) => {
)
.innerJoin('levels', 'workbooks.level_id', 'levels.id')

res.send(results)
res.status(200).send(results)
} catch(err) {
console.error(err)
}
Expand Down

0 comments on commit 8e2bed2

Please sign in to comment.