diff --git a/bookshop/handlers/AdminService.Authors.CREATE.js b/bookshop/handlers/AdminService.Authors.CREATE.js index 7a8d6f0b..a9912881 100644 --- a/bookshop/handlers/AdminService.Authors.CREATE.js +++ b/bookshop/handlers/AdminService.Authors.CREATE.js @@ -8,6 +8,8 @@ async function run() { let res = await SELECT.one`title`.from(`Books`).where(`ID=201`) let { title } = res let Author = req.data + //await srv.read('Books') + Author.modifiedBy = "Custom Event handler changed this!" Author.placeOfDeath = " --- Somewhere over " + title + " --- create in Sandbox" //await this.emit("createdAuthor", { Author }) diff --git a/bookshop/srv/AdminService-Authors-CREATE.cds b/bookshop/srv/AdminService-Authors-CREATE.cds index 3af7eb7a..a33b9751 100644 --- a/bookshop/srv/AdminService-Authors-CREATE.cds +++ b/bookshop/srv/AdminService-Authors-CREATE.cds @@ -1,17 +1,5 @@ using from '..\admin-service'; -annotate entity AdminService.Authors with @extension.logic.insert : "async function run() { - //debugger - //while (true) {} - //process.exit() - //1.substring() - // let res = await specialselect - let res = await SELECT.one`title`.from(`Books`).where(`ID=201`) - let { title } = res - let Author = req.data - Author.modifiedBy = "Custom Event handler changed this!" - Author.placeOfDeath = " --- Somewhere over " + title + " --- create in Sandbox" - //await this.emit("createdAuthor", { Author }) - return Author -} -run() -" \ No newline at end of file + +annotate AdminService.Authors with @extension.logic: [{when: 'CREATE', code: 'async function run() {\r\n \/\/debugger\r\n \/\/while (true) {}\r\n \/\/process.exit()\r\n \/\/1.substring()\r\n \/\/ let res = await specialselect\r\n let res = await SELECT.one`title`.from(`Books`).where(`ID=201`)\r\n let { title } = res\r\n let Author = req.data\r\n Author.modifiedBy = \"Custom Event handler changed this!\"\r\n Author.placeOfDeath = \" --- Somewhere over \" + title + \" --- create in Sandbox\"\r\n \/\/await this.emit(\"createdAuthor\", { Author })\r\n return Author\r\n}\r\nrun()\r\n'}, +{when: 'READ', code: 'function getYear(v) {\r\n return parseInt(v.substr(0, 4))\r\n}\r\nfunction getMonth(v) {\r\n return parseInt(v.substr(5, 2))\r\n}\r\nfunction getDay(v) {\r\n return parseInt(v.substr(8, 2))\r\n}\r\n\r\nfunction getAge(from, to) {\r\n if (from === undefined || from == null) return 0\r\n if (to === undefined || to == null) to = new Date().toISOString()\r\n let year = getYear(to) - getYear(from) - 1\r\n if (\r\n getMonth(to) > getMonth(from) ||\r\n (getMonth(to) === getMonth(from) && getDay(to) >= getDay(from))\r\n ) {\r\n year++\r\n }\r\n return year\r\n}\r\n\r\nconst result_ = Array.isArray(result) ? result : [result]\r\nfor (const row of result_) {\r\n row.modifiedBy += \" --- read in sandbox\"\r\n row.age = getAge(row.dateOfBirth, row.dateOfDeath)\r\n}'} +]; \ No newline at end of file diff --git a/bookshop/test/requests.http b/bookshop/test/requests.http index 867a426c..eb33e27a 100644 --- a/bookshop/test/requests.http +++ b/bookshop/test/requests.http @@ -1,6 +1,6 @@ @server = http://localhost:4004 @me = Authorization: Basic {{$processEnv USER}}: -@id = 1113 +@id = 2000 ### ------------------------------------------------------------------------