File tree 4 files changed +53
-0
lines changed
resource/src/lib/controllers
4 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 1
1
export * from './lib/event-providers' ;
2
2
export * from './lib/application' ;
3
3
export * from './lib/schemas' ;
4
+ export * from './lib/utils' ;
4
5
export * from './lib/dtos' ;
Original file line number Diff line number Diff line change
1
+ import { EventDto } from '../dtos' ;
2
+
3
+ const createSitemapUrl = ( loc : string , lastmod : string | null ) => `<url>
4
+ <loc>${ loc } </loc>
5
+ ${ lastmod ? `<lastmod>${ lastmod } </lastmod>` : '' }
6
+ </url>` ;
7
+
8
+ export function createSitemapFromEvents ( events : EventDto [ ] ) {
9
+ const header = `<?xml version="1.0" encoding="UTF-8"?>
10
+ <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` ;
11
+
12
+ const urls = events
13
+ . map ( ( event ) => {
14
+ const loc = `https://devparana.mx/#/evento/${ event . id } ` ;
15
+ const lastmod = event . date ? new Date ( event . date ) . toISOString ( ) : null ;
16
+
17
+ return createSitemapUrl ( loc , lastmod ) ;
18
+ } )
19
+ . join ( '' ) ;
20
+
21
+ const footer = `</urlset>` ;
22
+
23
+ return `${ header } ${ urls } ${ footer } ` ;
24
+ }
Original file line number Diff line number Diff line change
1
+ export * from './create-sitemap-from-events' ;
Original file line number Diff line number Diff line change @@ -18,6 +18,8 @@ import {
18
18
Query ,
19
19
Delete ,
20
20
Controller ,
21
+ Res ,
22
+ BadRequestException ,
21
23
} from '@nestjs/common' ;
22
24
import {
23
25
EventDto ,
@@ -27,9 +29,12 @@ import {
27
29
UpdateEventDto ,
28
30
RSVPsFacade ,
29
31
CreateRSVPDto ,
32
+ createSitemapFromEvents ,
30
33
} from '@devmx/event-data-source' ;
31
34
import 'multer' ;
32
35
import { subDays } from 'date-fns/subDays' ;
36
+ import { Response } from 'express' ;
37
+
33
38
34
39
@ApiTags ( 'Eventos' )
35
40
@Controller ( 'events' )
@@ -49,6 +54,28 @@ export class EventsController {
49
54
}
50
55
}
51
56
57
+ @Get ( 'sitemap.xml' )
58
+ @Allowed ( )
59
+ @ApiPage ( EventDto )
60
+ async sitemap ( @Res ( ) res : Response ) {
61
+ let date = new Date ( ) ;
62
+ date = subDays ( date , 1 ) ;
63
+
64
+ try {
65
+ const events = await this . eventsFacade . findFrom ( date , {
66
+ page : 0 ,
67
+ size : 100 ,
68
+ } ) ;
69
+
70
+ const sitemap = createSitemapFromEvents ( events . data ) ;
71
+
72
+ res . setHeader ( 'Content-Type' , 'application/xml' ) ;
73
+ res . send ( sitemap ) ;
74
+ } catch ( err ) {
75
+ throw new BadRequestException ( ) ;
76
+ }
77
+ }
78
+
52
79
@Get ( )
53
80
@Allowed ( )
54
81
@ApiPage ( EventDto )
You can’t perform that action at this time.
0 commit comments