diff --git a/src/main/java/org/codiki/posts/PostController.java b/src/main/java/org/codiki/posts/PostController.java index a762ce3..96fc39c 100755 --- a/src/main/java/org/codiki/posts/PostController.java +++ b/src/main/java/org/codiki/posts/PostController.java @@ -57,9 +57,9 @@ public class PostController { @JsonView(View.PostDTO.class) @GetMapping("/{postKey}") - public PostDTO getByKey(@PathVariable("postKey") final String pPostKey, + public Post getByKey(@PathVariable("postKey") final String pPostKey, final HttpServletResponse response) { - final PostDTO result = getByKeyAndSource(pPostKey, response); + final Post result = getByKeyAndSource(pPostKey, response); if(result != null) { result.setText(parserService.parse(result.getText())); } @@ -68,13 +68,13 @@ public class PostController { @JsonView(View.PostDTO.class) @GetMapping("/{postKey}/source") - public PostDTO getByKeyAndSource(@PathVariable("postKey")final String pPostKey, + public Post getByKeyAndSource(@PathVariable("postKey")final String pPostKey, final HttpServletResponse response) { - PostDTO result = null; + Post result = null; final Optional post = postRepository.getByKey(pPostKey); if(post.isPresent()) { - result = new PostDTO(post.get()); + result = post.get(); } else { response.setStatus(HttpServletResponse.SC_NOT_FOUND); } @@ -96,9 +96,8 @@ public class PostController { @JsonView(View.PostDTO.class) @GetMapping("/byCategory/{categoryId}") - public List getByCategory(@PathVariable("categoryId") final Long pCategoryId) { - return postRepository.getByCategoryId(pCategoryId).stream() - .map(PostDTO::new).collect(Collectors.toList()); + public List getByCategory(@PathVariable("categoryId") final Long pCategoryId) { + return postRepository.getByCategoryId(pCategoryId); } @JsonView(View.PostDTO.class) diff --git a/src/main/ts-v7/src/app/app.module.ts b/src/main/ts-v7/src/app/app.module.ts index 8f39750..656bcbe 100755 --- a/src/main/ts-v7/src/app/app.module.ts +++ b/src/main/ts-v7/src/app/app.module.ts @@ -29,6 +29,7 @@ import { ChangePasswordComponent } from './account-settings/change-password/chan import { ProfilEditionComponent } from './account-settings/profil-edition/profil-edition.component'; import { PostComponent } from './posts/post.component'; import { NotFoundComponent } from './not-found/not-found.component'; +import { ByCategoryComponent } from './posts/byCategory/by-category.component'; // Reusable components import { PostCardComponent } from './core/post-card/post-card.component'; @@ -43,6 +44,7 @@ import { MyPostsService } from './posts/myPosts/my-posts.service'; import { ChangePasswordService } from './account-settings/change-password/change-password.service'; import { ProfilEditionService } from './account-settings/profil-edition/profil-edition.service'; import { PostService } from './posts/post.service'; +import { ByCategoryService } from './posts/byCategory/by-category.service'; @NgModule({ declarations: [ @@ -58,7 +60,8 @@ import { PostService } from './posts/post.service'; ChangePasswordComponent, ProfilEditionComponent, PostComponent, - NotFoundComponent + NotFoundComponent, + ByCategoryComponent ], imports: [ BrowserModule, @@ -82,6 +85,7 @@ import { PostService } from './posts/post.service'; ChangePasswordService, ProfilEditionService, PostService, + ByCategoryService, { provide: HTTP_INTERCEPTORS, useClass: UnauthorizedInterceptor, multi: true } ], bootstrap: [AppComponent] diff --git a/src/main/ts-v7/src/app/app.routing.ts b/src/main/ts-v7/src/app/app.routing.ts index 303d3bf..bc39945 100755 --- a/src/main/ts-v7/src/app/app.routing.ts +++ b/src/main/ts-v7/src/app/app.routing.ts @@ -10,6 +10,7 @@ import { AccountSettingsComponent } from './account-settings/account-settings.co import { ChangePasswordComponent } from './account-settings/change-password/change-password.component'; import { ProfilEditionComponent } from './account-settings/profil-edition/profil-edition.component'; import { PostComponent } from './posts/post.component'; +import { ByCategoryComponent } from './posts/byCategory/by-category.component'; export const appRoutes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, @@ -21,5 +22,6 @@ export const appRoutes: Routes = [ { path: 'changePassword', component: ChangePasswordComponent, canActivate: [AuthGuard] }, { path: 'profilEdit', component: ProfilEditionComponent, canActivate: [AuthGuard] }, { path: 'posts/:postKey', component: PostComponent }, + { path: 'posts/byCategory/:categoryId', component: ByCategoryComponent}, { path: '**', redirectTo: '/home' } ]; diff --git a/src/main/ts-v7/src/app/posts/byCategory/by-category.component.html b/src/main/ts-v7/src/app/posts/byCategory/by-category.component.html new file mode 100644 index 0000000..1341b6d --- /dev/null +++ b/src/main/ts-v7/src/app/posts/byCategory/by-category.component.html @@ -0,0 +1,7 @@ +
+ +

Catégorie {{category.name}}

+
+ +
+
diff --git a/src/main/ts-v7/src/app/posts/byCategory/by-category.component.ts b/src/main/ts-v7/src/app/posts/byCategory/by-category.component.ts new file mode 100644 index 0000000..9eee26d --- /dev/null +++ b/src/main/ts-v7/src/app/posts/byCategory/by-category.component.ts @@ -0,0 +1,31 @@ +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; + +import { Post, Category } from '../../core/entities'; +import { ByCategoryService } from './by-category.service'; + +@Component({ + selector: 'app-posts-by-category', + templateUrl: './by-category.component.html' +}) +export class ByCategoryComponent implements OnInit { + category: Category; + listPosts: Array; + + constructor( + private route: ActivatedRoute, + private byCategoryService: ByCategoryService + ) {} + + ngOnInit(): void { + // Get the category + this.byCategoryService.getCategoryById(+this.route.snapshot.paramMap.get('categoryId')).subscribe(category => { + this.category = category; + + // Get the posts by category + this.byCategoryService.getPostsByCategory(this.category).subscribe(listPosts => { + this.listPosts = listPosts; + }); + }); + } +} diff --git a/src/main/ts-v7/src/app/posts/byCategory/by-category.service.ts b/src/main/ts-v7/src/app/posts/byCategory/by-category.service.ts new file mode 100644 index 0000000..b8c547d --- /dev/null +++ b/src/main/ts-v7/src/app/posts/byCategory/by-category.service.ts @@ -0,0 +1,18 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { Observable } from 'rxjs'; +import { Post, Category } from '../../core/entities'; + +@Injectable() +export class ByCategoryService { + + constructor(private http: HttpClient) {} + + getCategoryById(categoryId: number): Observable { + return this.http.get(`/api/categories/${categoryId}`); + } + + getPostsByCategory(category: Category): Observable> { + return this.http.get>(`/api/posts/byCategory/${category.id}`); + } +}