Add ByCategory component.
This commit is contained in:
@@ -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> 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<PostDTO> getByCategory(@PathVariable("categoryId") final Long pCategoryId) {
|
||||
return postRepository.getByCategoryId(pCategoryId).stream()
|
||||
.map(PostDTO::new).collect(Collectors.toList());
|
||||
public List<Post> getByCategory(@PathVariable("categoryId") final Long pCategoryId) {
|
||||
return postRepository.getByCategoryId(pCategoryId);
|
||||
}
|
||||
|
||||
@JsonView(View.PostDTO.class)
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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' }
|
||||
];
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<div>
|
||||
<app-spinner *ngIf="!listPosts"></app-spinner>
|
||||
<h1 *ngIf="listPosts">Catégorie {{category.name}}</h1>
|
||||
<div *ngIf="listPosts" class="col-lg-8 offset-lg-2">
|
||||
<app-post-card *ngFor="let post of listPosts" [post]="post"></app-post-card>
|
||||
</div>
|
||||
</div>
|
||||
@@ -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<Post>;
|
||||
|
||||
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;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<Category> {
|
||||
return this.http.get<Category>(`/api/categories/${categoryId}`);
|
||||
}
|
||||
|
||||
getPostsByCategory(category: Category): Observable<Array<Post>> {
|
||||
return this.http.get<Array<Post>>(`/api/posts/byCategory/${category.id}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user