REST Service for POPCORN - ILIAS
alex
2025-09-18 e1217fbcd93654ad6492b641bf53e4e0d0368bfd
GS-2317
1 files added
2 files modified
167 ■■■■ changed files
app.js 68 ●●●●● patch | view | raw | blame | history
lib/db.js 36 ●●●● patch | view | raw | blame | history
test/testApiKursOffline.js 63 ●●●●● patch | view | raw | blame | history
app.js
@@ -35,8 +35,7 @@
      await promiseDelay(500) // delay response to avoid denial of service attacks
      res.code(403)
      return res.send({status: "error", error: "access denied"})
   }
   else {
    } else {
      // log.debug("AUTH FOR ", req.url)
   }
})
@@ -52,8 +51,7 @@
      const search = req.query?.search
      if (!search) {
         return res.code(422).send({status: "error", msg: "no search"})
      }
      else {
        } else {
         log.info(search)
         const data = await searchLib.search(search)
         return res.send(data)
@@ -85,8 +83,7 @@
      const user = await db.getUserByLogin(login)
      if (user) {
         return res.send(user)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -98,8 +95,7 @@
      const user = await db.getUserByUserId(userid)
      if (user) {
         return res.send(user)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -112,8 +108,7 @@
      const tn = await db.getUserTeilnahmen(userId)
      if (tn) {
         return res.send(tn)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -132,8 +127,7 @@
      const {usr_id} = req.params
      if (!usr_id || isNaN(Number(usr_id))) {
         return res.code(500).send({status: "error", msg: "userId error"})
      }
      else {
        } else {
         const res2 = await libIlias.deleteUser(usr_id)
         log.info(res2)
         return res.send(res2)
@@ -147,8 +141,7 @@
      const data = await db.getObjIdFromRefId(ref_id)
      if (data) {
         return res.send(data)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -157,8 +150,7 @@
      let data = await db.getRefIdFromObjId(obj_id)
      if (data) {
         return res.send(data)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -169,8 +161,7 @@
      let data = await db.getKurse()
      if (data) {
         return res.send(data)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -179,8 +170,7 @@
      let data = await db.getKurs(refId)
      if (data) {
         return res.send(data)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -189,8 +179,7 @@
      let data = await db.getKursItems2(refId)
      if (data) {
         return res.send(data)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -199,8 +188,7 @@
      let data = await db.getKursTeilnehmer(refId)
      if (data) {
         return res.send(data)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -220,8 +208,7 @@
      let data = await db.getSingleKursTeilnehmer(refId, userId)
      if (data) {
         return res.send(data)
      }
      else {
        } else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
@@ -261,6 +248,35 @@
      }
   })
    .get("/api/kurs/:refId/offline", async function (req, res) {
        const refId = Number(req.params.refId)
        try {
            const {obj_id} = await db.getObjIdFromRefId(refId)
            const res2 = await db.getKursOffline(obj_id)
            return res.send(res2)
        } catch (err) {
            console.error(err)
            log.error(err.message)
            return res.code(500).send({status: "error", message: err.message})
        }
    })
    .post("/api/kurs/:refId/offline", async function (req, res) {
        const refId = Number(req.params.refId)
        const {offline} = req.body
        // console.dir(req.body, {depth: null, colors: true, maxArrayLength: null})
        try {
            const {obj_id} = await db.getObjIdFromRefId(refId)
            const res2 = await db.setKursOffline(offline, obj_id)
            return res.send(res2)
        } catch (err) {
            console.error(err)
            log.error(err.message)
            return res.code(500).send({status: "error", message: err.message})
        }
    })
   .delete("/api/kurs/:refId/teilnehmer/:usrId", async function (req, res) {
      const {refId, usrId} = req.params
      if (!refId || !usrId) throw {status: "error", msg: "refId and usrId requried"}
lib/db.js
@@ -43,6 +43,9 @@
   getSingleKursTeilnehmer,
   getKursTeilnehmerCount,
    getKursOffline,
    setKursOffline,
   getUdf,
   getKursTeilnehmerRolle,
@@ -63,8 +66,7 @@
              LIMIT ${limit}
              OFFSET ${offset}
      `
   }
   else {
    } else {
      const ids = await searchLib.search(search)
      if (!ids.length) {
         throw "nothing found"
@@ -395,9 +397,30 @@
              WHERE om.usr_id = ${usr_id}
                AND om.member = 1
   `
   log.info(q)
    // log.info(q).catch(console.error)
   const [results] = await pool.query(q)
   return results
}
async function getKursOffline(obj_id) {
    const pool = await poolP
    const q = `SELECT offline
                FROM ${database}.object_data
               WHERE obj_id = ${obj_id}
    `
    const [results] = await pool.query(q)
    return results[0]
}
async function setKursOffline(isOffline, obj_id) {
    const pool = await poolP
    const q = `UPDATE ${database}.object_data
               SET offline = ${isOffline}
               WHERE obj_id = ${obj_id}
    `
    const [results] = await pool.query(q)
    // return results
    return {offline: isOffline}
}
/////// UDF ////////////////////////////////////////////////////////////////
@@ -424,8 +447,7 @@
   if (results.length) {
      const {obj_id, title, description} = results[0]
      return obj_id
   }
   else {
    } else {
      return null
   }
}
@@ -457,7 +479,6 @@
   const [results] = await pool.query(q)
   return results
}
/////// STATUS ////////////////////////////////////////////////////////////////
@@ -522,8 +543,7 @@
   if (affectedRows1 && affectedRows2) {
      return {status: "ok"}
   }
   else {
    } else {
      throw {
         status: "error",
         reason: {affectedRows1, affectedRows2}
test/testApiKursOffline.js
New file
@@ -0,0 +1,63 @@
const expect = require("chai").expect
const settings = require("../settings")
const libIlias = require("../lib/libIlias")
const db = require("../lib/db")
const testData = require("./data")
/////////////////////////////////////////////////////////////////////////
function getUrl(ref_id) {
    return `http://localhost:${settings.port}/api/kurs/${ref_id}/offline?token=${settings.authtoken}`
}
const ref_id = 595
describe("using the API", function () {
    describe("the Route GET /api/kurs/:refId/offline", function () {
        it("should return the offline or online status of a kurs", async function () {
            const url = getUrl(ref_id)
            console.log(url)
            const res = await fetch(url, {
                method: "GET",
                // body: JSON.stringify(body),
                headers: {
                    'Content-Type': 'application/json', // Indicate JSON data
                },
            })
            // console.log(res)
            const data = await res.json()
            console.log(data)
            expect(data).to.be.a("object").and.to.have.property("offline")
        })
    })
    describe("the Route POST /api/kurs/:refId/offline", function () {
        it("should set the offline or online status of a kurs", async function () {
            const url = getUrl(ref_id)
            console.log(url)
            const body = {offline: 0}
            const res = await fetch(url, {
                method: "POST",
                body: JSON.stringify(body),
                headers: {
                    'Content-Type': 'application/json', // Indicate JSON data
                },
            })
            // console.log(res)
            const data = await res.json()
            console.log(data)
            expect(data).to.be.a("object").and.to.have.property("offline")
        })
    })
})
/////////////////////////////////////////////////////////////////////////