dh_demo

DreamHanks demo project
git clone git://git.lair.cx/dh_demo
Log | Files | Refs | README

wiki_talk.ts (1788B)


      1 import { modelBehaviour } from '@/lib/model_helpers'
      2 import { createThread, Thread } from '@/lib/models/thread'
      3 import { OkPacket, RowDataPacket } from 'mysql2'
      4 
      5 const SQL_COUNT_WIKI_TALKS = `
      6     select count(*)
      7     from wiki_talks
      8     where page_id = ?
      9 `
     10 
     11 export const countWikiTalks = modelBehaviour<
     12   [pageId: number],
     13   number
     14 >(async (conn, args) => {
     15   const [rows] = await conn.query<RowDataPacket[]>({
     16     sql: SQL_COUNT_WIKI_TALKS,
     17   }, args)
     18 
     19   return rows[0][0]
     20 })
     21 
     22 const SQL_LIST_WIKI_TALKS = `
     23     select t.id,
     24            t.author_id,
     25            u.nickname,
     26            t.title,
     27            (select content
     28             from thread_comments
     29             where thread_id = t.id
     30             order by id
     31             limit 1) as preview,
     32            t.created_at
     33     from threads t
     34          inner join wiki_talks wt on t.id = wt.thread_id
     35          left join user_profiles u on u.login_id = t.author_id
     36     where wt.page_id = ?
     37     order by t.created_at desc
     38     limit ? offset ?
     39 `
     40 
     41 export const listWikiTalks = modelBehaviour<
     42   [pageId: number, limit: number, offset: number],
     43   Thread[]
     44 >(async (conn, args) => {
     45   const [rows] = await conn.query<RowDataPacket[]>({
     46     sql: SQL_LIST_WIKI_TALKS,
     47   }, args)
     48 
     49   return rows.map(row => ({
     50     id: row[0],
     51     authorId: row[1],
     52     authorName: row[2],
     53     title: row[3],
     54     preview: row[4],
     55     createdAt: row[5],
     56   }))
     57 })
     58 
     59 const SQL_CREATE_WIKI_TALK = `
     60     insert into wiki_talks (page_id, thread_id)
     61     values (?, ?)
     62 `
     63 
     64 export const createWikiTalk = modelBehaviour<
     65   [pageId: number, authorId: number, title: string],
     66   number
     67 >(async (conn, args) => {
     68   const threadId = await createThread(conn, [args[1], args[2]])
     69 
     70   await conn.query<OkPacket>({
     71     sql: SQL_CREATE_WIKI_TALK,
     72   }, [args[0], threadId])
     73 
     74   return threadId
     75 })