Add ByCategory component.

This commit is contained in:
2019-02-10 16:14:56 +01:00
parent f0b53f752f
commit b3603e242f
6 changed files with 70 additions and 9 deletions

View File

@@ -57,9 +57,9 @@ public class PostController {
@JsonView(View.PostDTO.class) @JsonView(View.PostDTO.class)
@GetMapping("/{postKey}") @GetMapping("/{postKey}")
public PostDTO getByKey(@PathVariable("postKey") final String pPostKey, public Post getByKey(@PathVariable("postKey") final String pPostKey,
final HttpServletResponse response) { final HttpServletResponse response) {
final PostDTO result = getByKeyAndSource(pPostKey, response); final Post result = getByKeyAndSource(pPostKey, response);
if(result != null) { if(result != null) {
result.setText(parserService.parse(result.getText())); result.setText(parserService.parse(result.getText()));
} }
@@ -68,13 +68,13 @@ public class PostController {
@JsonView(View.PostDTO.class) @JsonView(View.PostDTO.class)
@GetMapping("/{postKey}/source") @GetMapping("/{postKey}/source")
public PostDTO getByKeyAndSource(@PathVariable("postKey")final String pPostKey, public Post getByKeyAndSource(@PathVariable("postKey")final String pPostKey,
final HttpServletResponse response) { final HttpServletResponse response) {
PostDTO result = null; Post result = null;
final Optional<Post> post = postRepository.getByKey(pPostKey); final Optional<Post> post = postRepository.getByKey(pPostKey);
if(post.isPresent()) { if(post.isPresent()) {
result = new PostDTO(post.get()); result = post.get();
} else { } else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setStatus(HttpServletResponse.SC_NOT_FOUND);
} }
@@ -96,9 +96,8 @@ public class PostController {
@JsonView(View.PostDTO.class) @JsonView(View.PostDTO.class)
@GetMapping("/byCategory/{categoryId}") @GetMapping("/byCategory/{categoryId}")
public List<PostDTO> getByCategory(@PathVariable("categoryId") final Long pCategoryId) { public List<Post> getByCategory(@PathVariable("categoryId") final Long pCategoryId) {
return postRepository.getByCategoryId(pCategoryId).stream() return postRepository.getByCategoryId(pCategoryId);
.map(PostDTO::new).collect(Collectors.toList());
} }
@JsonView(View.PostDTO.class) @JsonView(View.PostDTO.class)

View File

@@ -29,6 +29,7 @@ import { ChangePasswordComponent } from './account-settings/change-password/chan
import { ProfilEditionComponent } from './account-settings/profil-edition/profil-edition.component'; import { ProfilEditionComponent } from './account-settings/profil-edition/profil-edition.component';
import { PostComponent } from './posts/post.component'; import { PostComponent } from './posts/post.component';
import { NotFoundComponent } from './not-found/not-found.component'; import { NotFoundComponent } from './not-found/not-found.component';
import { ByCategoryComponent } from './posts/byCategory/by-category.component';
// Reusable components // Reusable components
import { PostCardComponent } from './core/post-card/post-card.component'; 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 { ChangePasswordService } from './account-settings/change-password/change-password.service';
import { ProfilEditionService } from './account-settings/profil-edition/profil-edition.service'; import { ProfilEditionService } from './account-settings/profil-edition/profil-edition.service';
import { PostService } from './posts/post.service'; import { PostService } from './posts/post.service';
import { ByCategoryService } from './posts/byCategory/by-category.service';
@NgModule({ @NgModule({
declarations: [ declarations: [
@@ -58,7 +60,8 @@ import { PostService } from './posts/post.service';
ChangePasswordComponent, ChangePasswordComponent,
ProfilEditionComponent, ProfilEditionComponent,
PostComponent, PostComponent,
NotFoundComponent NotFoundComponent,
ByCategoryComponent
], ],
imports: [ imports: [
BrowserModule, BrowserModule,
@@ -82,6 +85,7 @@ import { PostService } from './posts/post.service';
ChangePasswordService, ChangePasswordService,
ProfilEditionService, ProfilEditionService,
PostService, PostService,
ByCategoryService,
{ provide: HTTP_INTERCEPTORS, useClass: UnauthorizedInterceptor, multi: true } { provide: HTTP_INTERCEPTORS, useClass: UnauthorizedInterceptor, multi: true }
], ],
bootstrap: [AppComponent] bootstrap: [AppComponent]

View File

@@ -10,6 +10,7 @@ import { AccountSettingsComponent } from './account-settings/account-settings.co
import { ChangePasswordComponent } from './account-settings/change-password/change-password.component'; import { ChangePasswordComponent } from './account-settings/change-password/change-password.component';
import { ProfilEditionComponent } from './account-settings/profil-edition/profil-edition.component'; import { ProfilEditionComponent } from './account-settings/profil-edition/profil-edition.component';
import { PostComponent } from './posts/post.component'; import { PostComponent } from './posts/post.component';
import { ByCategoryComponent } from './posts/byCategory/by-category.component';
export const appRoutes: Routes = [ export const appRoutes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' }, { path: '', redirectTo: '/home', pathMatch: 'full' },
@@ -21,5 +22,6 @@ export const appRoutes: Routes = [
{ path: 'changePassword', component: ChangePasswordComponent, canActivate: [AuthGuard] }, { path: 'changePassword', component: ChangePasswordComponent, canActivate: [AuthGuard] },
{ path: 'profilEdit', component: ProfilEditionComponent, canActivate: [AuthGuard] }, { path: 'profilEdit', component: ProfilEditionComponent, canActivate: [AuthGuard] },
{ path: 'posts/:postKey', component: PostComponent }, { path: 'posts/:postKey', component: PostComponent },
{ path: 'posts/byCategory/:categoryId', component: ByCategoryComponent},
{ path: '**', redirectTo: '/home' } { path: '**', redirectTo: '/home' }
]; ];

View File

@@ -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>

View File

@@ -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;
});
});
}
}

View File

@@ -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}`);
}
}