1
1
/*
2
2
* @Author : ly525
3
3
* @Date : 2019-12-04 19:55:24
4
- * @LastEditors : ly525
5
- * @LastEditTime : 2019-12-15 18:56:44
4
+ * @LastEditors : ly525
5
+ * @LastEditTime : 2019-12-21 19:32:05
6
6
* @FilePath : /luban-h5/back-end/h5-api/api/work/controllers/Work.js
7
7
* @Github : https://github.com/ly525/luban-h5
8
8
* @Description :
9
9
* @Copyright 2018 - 2019 luban-h5. All Rights Reserved
10
10
*/
11
11
'use strict' ;
12
12
const request = require ( 'request' ) ;
13
+ const _ = require ( 'lodash' ) ;
13
14
14
15
/**
15
16
* Read the documentation (https://strapi.io/documentation/3.0.0-beta.x/guides/controllers.html#core-controllers)
@@ -52,8 +53,7 @@ module.exports = {
52
53
const work = await strapi . services . work . findOne ( ctx . params ) ;
53
54
54
55
// learn the query from: https://github.com/strapi/foodadvisor/blob/master/api/api/restaurant/controllers/Restaurant.js#L40
55
- // eslint-disable-next-line no-undef
56
- let formRecords = await Workform . query ( qb => {
56
+ let formRecords = await strapi . services . workform . query ( qb => {
57
57
qb . where ( 'work' , '=' , work . id ) ;
58
58
} ) . fetchAll ( ) ;
59
59
formRecords = formRecords . toJSON ( ) ;
@@ -62,6 +62,26 @@ module.exports = {
62
62
// eslint-disable-next-line require-atomic-updates
63
63
return ctx . body = { uuidMap2Name, formRecords } ;
64
64
} ,
65
+ queryWorksWithForms : async ( ctx ) => {
66
+ let formRecords = await strapi . services . workform . fetchAll ( {
67
+ withRelated : [
68
+ { 'work' : qb => qb . column ( 'id' ) }
69
+ ] ,
70
+ columns : [ 'id' , 'work' ]
71
+ } ) ;
72
+ formRecords = formRecords . toJSON ( ) ;
73
+ const groupedFormRecords = _ . groupBy ( formRecords , 'work.id' ) ;
74
+
75
+ let workRecords = await strapi . services . work . fetchAll ( {
76
+ columns : [ 'id' , 'title' ]
77
+ } ) ;
78
+ workRecords = workRecords . toJSON ( ) . map ( work => ( {
79
+ ...work ,
80
+ form_count : groupedFormRecords [ work . id ] . length
81
+ } ) ) . filter ( work => work . form_count ) ;
82
+
83
+ return ctx . body = workRecords ;
84
+ } ,
65
85
setAsTemplate : async ( ctx ) => {
66
86
const work = await strapi . services . work . findOne ( ctx . params ) ;
67
87
0 commit comments