@@ -2,13 +2,15 @@ import { Request, Response, NextFunction } from 'express';
22import  {  QueryTypes  }  from  'sequelize' ; 
33import  {  sequelize  }  from  '../db' ; 
44import  {  asyncWrapper  }  from  '../middleware' ; 
5+ import  {  SnippetModel ,  Snippet_TagModel  }  from  '../models' ; 
56import  { 
6-  SnippetInstance , 
7-  SnippetModel , 
8-  Snippet_TagModel , 
9-  TagModel 
10- }  from  '../models' ; 
11- import  {  ErrorResponse ,  getTags ,  tagParser ,  Logger  }  from  '../utils' ; 
7+  ErrorResponse , 
8+  getTags , 
9+  tagParser , 
10+  Logger , 
11+  createTags 
12+ }  from  '../utils' ; 
13+ import  {  Body  }  from  '../typescript/interfaces' ; 
1214
1315const  logger  =  new  Logger ( 'snippets-controller' ) ; 
1416
@@ -19,11 +21,6 @@ const logger = new Logger('snippets-controller');
1921 */ 
2022export  const  createSnippet  =  asyncWrapper ( 
2123 async  ( req : Request ,  res : Response ,  next : NextFunction ) : Promise < void >  =>  { 
22-  interface  Body  { 
23-  language : string ; 
24-  tags : string [ ] ; 
25-  } 
26- 2724 // Get tags from request body 
2825 const  {  language,  tags : requestTags  }  =  < Body > req . body ; 
2926 const  parsedRequestTags  =  tagParser ( [ 
@@ -37,38 +34,8 @@ export const createSnippet = asyncWrapper(
3734 tags : [ ...parsedRequestTags ] . join ( ',' ) 
3835 } ) ; 
3936
40-  // Get all tags 
41-  const  rawAllTags  =  await  sequelize . query < {  id : number ;  name : string  } > ( 
42-  `SELECT * FROM tags` , 
43-  {  type : QueryTypes . SELECT  } 
44-  ) ; 
45- 46-  const  parsedAllTags  =  rawAllTags . map ( tag  =>  tag . name ) ; 
47- 48-  // Create array of new tags 
49-  const  newTags  =  [ ...parsedRequestTags ] . filter ( 
50-  tag  =>  ! parsedAllTags . includes ( tag ) 
51-  ) ; 
52- 53-  // Create new tags 
54-  if  ( newTags . length  >  0 )  { 
55-  for  ( const  tag  of  newTags )  { 
56-  const  {  id,  name }  =  await  TagModel . create ( {  name : tag  } ) ; 
57-  rawAllTags . push ( {  id,  name } ) ; 
58-  } 
59-  } 
60- 61-  // Associate tags with snippet 
62-  for  ( const  tag  of  parsedRequestTags )  { 
63-  const  tagObj  =  rawAllTags . find ( t  =>  t . name  ==  tag ) ; 
64- 65-  if  ( tagObj )  { 
66-  await  Snippet_TagModel . create ( { 
67-  snippet_id : snippet . id , 
68-  tag_id : tagObj . id 
69-  } ) ; 
70-  } 
71-  } 
37+  // Create tags 
38+  await  createTags ( parsedRequestTags ,  snippet . id ) ; 
7239
7340 // Get raw snippet values 
7441 const  rawSnippet  =  snippet . get ( {  plain : true  } ) ; 
@@ -162,10 +129,28 @@ export const updateSnippet = asyncWrapper(
162129 ) ; 
163130 } 
164131
165-  snippet  =  await  snippet . update ( req . body ) ; 
132+  // Get tags from request body 
133+  const  {  language,  tags : requestTags  }  =  < Body > req . body ; 
134+  let  parsedRequestTags  =  tagParser ( [ ...requestTags ,  language . toLowerCase ( ) ] ) ; 
135+ 136+  // Update snippet 
137+  snippet  =  await  snippet . update ( { 
138+  ...req . body , 
139+  tags : [ ...parsedRequestTags ] . join ( ',' ) 
140+  } ) ; 
141+ 142+  // Delete old tags and create new ones 
143+  await  Snippet_TagModel . destroy ( {  where : {  snippet_id : req . params . id  }  } ) ; 
144+  await  createTags ( parsedRequestTags ,  snippet . id ) ; 
145+ 146+  // Get raw snippet values 
147+  const  rawSnippet  =  snippet . get ( {  plain : true  } ) ; 
166148
167149 res . status ( 200 ) . json ( { 
168-  data : snippet 
150+  data : { 
151+  ...rawSnippet , 
152+  tags : [ ...parsedRequestTags ] 
153+  } 
169154 } ) ; 
170155 } 
171156) ; 
0 commit comments