Skip to content
Snippets Groups Projects
Commit 588f9bdf authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

feat: add news tag handling

parent 1c281068
No related branches found
No related tags found
2 merge requests!46Recette,!45Dev
export enum TagEnum {
aLaUne = 'a-la-une',
appels = 'appels',
projets = 'projets',
formations = 'formations',
infos = 'infos',
dossiers = 'dossiers',
etudes = 'etudes',
ressources = 'ressources',
}
import { Controller, Get, HttpService, Param, Query } from '@nestjs/common';
import { Controller, Get, HttpService, Logger, Param, Query } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { catchError, map } from 'rxjs/operators';
import { ApiQuery } from '@nestjs/swagger';
import { Post } from './schemas/post.schema';
import { PostsService } from './posts.service';
......@@ -32,7 +32,24 @@ export class PostsController {
page: query.page ? query.page : null,
},
})
.pipe(map((response) => response.data));
.pipe(
map((response) => response.data),
catchError((err) => {
Logger.error(err);
return new Observable();
})
);
}
@Get('tags')
public async findAllTags(): Promise<{ public: Tag[]; commune: Tag[]; others: Tag[] }> {
return Promise.all([
this.postsService.getLocationTags(),
this.postsService.getPublicTags(),
this.postsService.getRegularTags(),
]).then((data) => {
return { commune: data[0], public: data[1], others: data[2] };
});
}
@Get(':id')
......@@ -46,14 +63,4 @@ export class PostsController {
})
.pipe(map((response) => response.data));
}
@Get('tags')
public async findAllTags(): Promise<{ public: Tag[]; commune: Tag[]; others: Tag[] }> {
return Promise.all([
this.postsService.getLocationTags(),
this.postsService.getPublicTags(),
this.postsService.getRegularTags(),
]).then((data) => {
return { commune: data[0], public: data[1], others: data[2] };
});
}
}
......@@ -3,6 +3,7 @@ import { Logger } from '@nestjs/common';
import * as GhostAdminAPI from '@tryghost/admin-api';
import * as _ from 'lodash';
import { map } from 'rxjs/operators';
import { TagEnum } from './enums/tag.enum';
import { Tag } from './schemas/tag.schema';
@Injectable()
......@@ -27,6 +28,24 @@ export class PostsService {
public getTags(): Promise<Tag[]> {
return this.api.tags.browse({ limit: 'all' });
}
// public getTags(): Promise<Tag[]> {
// return new Promise((resolve) => {
// this.api.tags.browse({ limit: 'all' }).then((tags: Tag[]) => {
// tags.sort((x, y) => {
// if (x < y) {
// return -1;
// }
// if (x > y) {
// return 1;
// }
// return 0;
// });
// console.log(tags);
// resolve(tags);
// });
// });
// }
/**
* Get all locations tags. Ex: Oullins, Sainte-foy etc...
......@@ -49,6 +68,20 @@ export class PostsService {
*/
public async getRegularTags(): Promise<Tag[]> {
const tags = await this.getTags();
return _.filter(tags, { description: null });
let publicTags = _.filter(tags, { description: null });
// Remove 'Appel a projet'
_.remove(publicTags, { slug: TagEnum.appels });
// Move 'a la une' at index 0
publicTags = this.arraymove(publicTags, _.findIndex(publicTags, { slug: TagEnum.aLaUne }), 0);
// Move 'info' at index 1
publicTags = this.arraymove(publicTags, _.findIndex(publicTags, { slug: TagEnum.infos }), 1);
return publicTags;
}
public arraymove(arr, fromIndex, toIndex): Array<any> {
const element = arr[fromIndex];
arr.splice(fromIndex, 1);
arr.splice(toIndex, 0, element);
return arr;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment