@@ -2,8 +2,10 @@ import { Request, Response, NextFunction } from 'express';
2
2
import { QueryTypes } from 'sequelize' ;
3
3
import { sequelize } from '../db' ;
4
4
import { asyncWrapper } from '../middleware' ;
5
- import { SnippetModel } from '../models' ;
6
- import { ErrorResponse , tagsParser } from '../utils' ;
5
+ import { SnippetInstance , SnippetModel } from '../models' ;
6
+ import { ErrorResponse , getTags , tagsParser , Logger } from '../utils' ;
7
+
8
+ const logger = new Logger ( 'snippets-controller' ) ;
7
9
8
10
/**
9
11
* @description Create new snippet
@@ -37,7 +39,22 @@ export const createSnippet = asyncWrapper(
37
39
*/
38
40
export const getAllSnippets = asyncWrapper (
39
41
async ( req : Request , res : Response , next : NextFunction ) : Promise < void > => {
40
- const snippets = await SnippetModel . findAll ( ) ;
42
+ const snippets = await SnippetModel . findAll ( {
43
+ raw : true
44
+ } ) ;
45
+
46
+ await new Promise < void > ( async resolve => {
47
+ try {
48
+ for await ( let snippet of snippets ) {
49
+ const tags = await getTags ( + snippet . id ) ;
50
+ snippet . tags = tags ;
51
+ }
52
+ } catch ( err ) {
53
+ logger . log ( 'Error while fetching tags' ) ;
54
+ } finally {
55
+ resolve ( ) ;
56
+ }
57
+ } ) ;
41
58
42
59
res . status ( 200 ) . json ( {
43
60
data : snippets
@@ -46,14 +63,15 @@ export const getAllSnippets = asyncWrapper(
46
63
) ;
47
64
48
65
/**
49
- * @description Get single sinppet by id
66
+ * @description Get single snippet by id
50
67
* @route /api/snippets/:id
51
68
* @request GET
52
69
*/
53
70
export const getSnippet = asyncWrapper (
54
71
async ( req : Request , res : Response , next : NextFunction ) : Promise < void > => {
55
72
const snippet = await SnippetModel . findOne ( {
56
- where : { id : req . params . id }
73
+ where : { id : req . params . id } ,
74
+ raw : true
57
75
} ) ;
58
76
59
77
if ( ! snippet ) {
@@ -65,6 +83,9 @@ export const getSnippet = asyncWrapper(
65
83
) ;
66
84
}
67
85
86
+ const tags = await getTags ( + req . params . id ) ;
87
+ snippet . tags = tags ;
88
+
68
89
res . status ( 200 ) . json ( {
69
90
data : snippet
70
91
} ) ;
@@ -144,19 +165,20 @@ export const deleteSnippet = asyncWrapper(
144
165
) ;
145
166
146
167
/**
147
- * @description Count snippets by language
168
+ * @description Count tags
148
169
* @route /api/snippets/statistics/count
149
170
* @request GET
150
171
*/
151
- export const countSnippets = asyncWrapper (
172
+ export const countTags = asyncWrapper (
152
173
async ( req : Request , res : Response , next : NextFunction ) : Promise < void > => {
153
174
const result = await sequelize . query (
154
175
`SELECT
155
- COUNT(language) AS count,
156
- language
157
- FROM snippets
158
- GROUP BY language
159
- ORDER BY language ASC` ,
176
+ COUNT(tags.name) as count,
177
+ tags.name
178
+ FROM snippets_tags
179
+ INNER JOIN tags ON snippets_tags.tag_id = tags.id
180
+ GROUP BY tags.name
181
+ ORDER BY name ASC` ,
160
182
{
161
183
type : QueryTypes . SELECT
162
184
}
0 commit comments