| 
									
										
										
										
											2018-08-03 11:41:17 +01:00
										 |  |  | import debug from 'debug'; | 
					
						
							| 
									
										
										
										
											2018-07-31 16:58:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-29 12:12:15 +00:00
										 |  |  | const log = debug('fcc:boot:news'); | 
					
						
							| 
									
										
										
										
											2018-08-03 11:41:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default function newsBoot(app) { | 
					
						
							| 
									
										
										
										
											2019-01-14 22:49:13 +05:30
										 |  |  |   const router = app.loopback.Router(); | 
					
						
							| 
									
										
										
										
											2018-11-29 12:12:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-14 22:49:13 +05:30
										 |  |  |   router.get('/n', (req, res) => res.redirect('/news')); | 
					
						
							|  |  |  |   router.get('/n/:shortId', createShortLinkHandler(app)); | 
					
						
							| 
									
										
										
										
											2018-11-29 12:12:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function createShortLinkHandler(app) { | 
					
						
							|  |  |  |   const { Article } = app.models; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return function shortLinkHandler(req, res, next) { | 
					
						
							|  |  |  |     const { shortId } = req.params; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!shortId) { | 
					
						
							| 
									
										
										
										
											2019-01-14 22:49:13 +05:30
										 |  |  |       return res.redirect('/news'); | 
					
						
							| 
									
										
										
										
											2018-11-29 12:12:15 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-14 22:49:13 +05:30
										 |  |  |     log('shortId', shortId); | 
					
						
							| 
									
										
										
										
											2018-11-29 12:12:15 +00:00
										 |  |  |     return Article.findOne( | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         where: { | 
					
						
							|  |  |  |           or: [{ shortId }, { slugPart: shortId }] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       (err, article) => { | 
					
						
							|  |  |  |         if (err) { | 
					
						
							|  |  |  |           next(err); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!article) { | 
					
						
							| 
									
										
										
										
											2019-01-14 22:49:13 +05:30
										 |  |  |           return res.redirect('/news'); | 
					
						
							| 
									
										
										
										
											2018-11-29 12:12:15 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         const { | 
					
						
							| 
									
										
										
										
											2019-01-14 22:49:13 +05:30
										 |  |  |           slugPart | 
					
						
							| 
									
										
										
										
											2018-11-29 12:12:15 +00:00
										 |  |  |         } = article; | 
					
						
							| 
									
										
										
										
											2019-01-14 22:49:13 +05:30
										 |  |  |         const slug = `/news/${slugPart}`; | 
					
						
							|  |  |  |         return res.redirect(slug); | 
					
						
							| 
									
										
										
										
											2018-11-29 12:12:15 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |