maj partielle de la création d'un post
This commit is contained in:
parent
1168d9c349
commit
7a96d5f2e0
18
src/app/guards/admin.guard.ts
Normal file
18
src/app/guards/admin.guard.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import {CanActivateFn, Router} from '@angular/router';
|
||||
import {inject} from '@angular/core';
|
||||
import {CookieService} from 'ngx-cookie-service';
|
||||
import {Author} from '../models/author';
|
||||
|
||||
export const adminGuard: CanActivateFn = (route, state) => {
|
||||
const router = inject(Router);
|
||||
const cookieService = inject(CookieService);
|
||||
|
||||
if (cookieService.get("author") !== '') {
|
||||
if (JSON.parse(cookieService.get("author")).role !== 'ADMIN')
|
||||
{
|
||||
router.navigate(['/']);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
17
src/app/guards/writer.guard.ts
Normal file
17
src/app/guards/writer.guard.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import {CanActivateFn, Router} from '@angular/router';
|
||||
import {inject} from '@angular/core';
|
||||
import {CookieService} from 'ngx-cookie-service';
|
||||
|
||||
export const writerGuard: CanActivateFn = (route, state) => {
|
||||
const router = inject(Router);
|
||||
const cookieService = inject(CookieService);
|
||||
|
||||
if (cookieService.get("author") !== '') {
|
||||
if (JSON.parse(cookieService.get("author")).role !== 'WRITER' && JSON.parse(cookieService.get("author")).role !== 'ADMIN')
|
||||
{
|
||||
router.navigate(['/']);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
0
src/app/new-post/new-post.component.css
Normal file
0
src/app/new-post/new-post.component.css
Normal file
23
src/app/new-post/new-post.component.html
Normal file
23
src/app/new-post/new-post.component.html
Normal file
@ -0,0 +1,23 @@
|
||||
<app-header></app-header>
|
||||
<form [formGroup]="form" (ngSubmit)="onSubmit()">
|
||||
<label for="title">Titre du post</label>
|
||||
<input id="title" type="text" pInputText formControlName="title"/>
|
||||
<label for="category">Catégorie du post</label>
|
||||
<input pInputText id="category" formControlName="category" type="text"/>
|
||||
<label for="desc">Description du post</label>
|
||||
<textarea formControlName="description" id="desc" pInputTextarea></textarea>
|
||||
<label>Image descriptive du post</label>
|
||||
<p-fileUpload accept="image/*"
|
||||
maxFileSize="1000000"
|
||||
[showUploadButton]="false"
|
||||
[showCancelButton]="false"
|
||||
chooseLabel="Sélectionner une image"
|
||||
(onSelect)="onSelected($event)">
|
||||
<ng-template pTemplate="content">
|
||||
</ng-template>
|
||||
</p-fileUpload>
|
||||
<p-editor formControlName="body" [style]="{ height: '320px' }"/>
|
||||
<p-button type="submit" label="envoyer" ></p-button>
|
||||
</form>
|
||||
|
||||
|
100
src/app/new-post/new-post.component.ts
Normal file
100
src/app/new-post/new-post.component.ts
Normal file
@ -0,0 +1,100 @@
|
||||
import {Component, OnDestroy} from '@angular/core';
|
||||
import {HeaderComponent} from '../header/header.component';
|
||||
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';
|
||||
import {InputTextModule} from 'primeng/inputtext';
|
||||
import {InputTextareaModule} from 'primeng/inputtextarea';
|
||||
import {FileSelectEvent, FileUploadModule} from 'primeng/fileupload';
|
||||
import {mergeMap, Subscription, switchMap} from 'rxjs';
|
||||
import {Post} from '../models/post';
|
||||
import {PostService} from '../services/post.service';
|
||||
import {CookieService} from 'ngx-cookie-service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {EditorModule} from 'primeng/editor';
|
||||
import {AuthorService} from '../services/author.service';
|
||||
import {Author} from '../models/author';
|
||||
|
||||
@Component({
|
||||
selector: 'app-new-post',
|
||||
standalone: true,
|
||||
imports: [
|
||||
HeaderComponent,
|
||||
ReactiveFormsModule,
|
||||
InputTextModule,
|
||||
InputTextareaModule,
|
||||
FileUploadModule,
|
||||
EditorModule
|
||||
],
|
||||
templateUrl: './new-post.component.html',
|
||||
styleUrl: './new-post.component.css'
|
||||
})
|
||||
export class NewPostComponent implements OnDestroy {
|
||||
subs: Subscription[] = []
|
||||
actualAuthor: Author | undefined;
|
||||
uploadedFile: File | undefined;
|
||||
form: FormGroup = new FormGroup({
|
||||
description: new FormControl('', [Validators.required, Validators.maxLength(512)]),
|
||||
title: new FormControl('', [Validators.required, Validators.maxLength(50)]),
|
||||
body: new FormControl('', [Validators.required]),
|
||||
category: new FormControl('', [Validators.required, Validators.maxLength(50)]),
|
||||
});
|
||||
|
||||
constructor(private postService: PostService,
|
||||
private cookieService: CookieService,
|
||||
private authorService: AuthorService,
|
||||
private messageService: MessageService,) {
|
||||
this.actualAuthor = JSON.parse(this.cookieService.get("author"));
|
||||
}
|
||||
|
||||
onSelected(event: FileSelectEvent) {
|
||||
if (event.currentFiles && event.currentFiles.length > 0) {
|
||||
this.uploadedFile = event.currentFiles[0];
|
||||
}
|
||||
}
|
||||
|
||||
onSubmit() {
|
||||
const formData = this.form.value;
|
||||
|
||||
if (this.uploadedFile) {
|
||||
const postToPost: any = { // LOL
|
||||
body: formData.body as string,
|
||||
description: formData.description as string,
|
||||
title: formData.title as string,
|
||||
category: formData.category as string
|
||||
};
|
||||
|
||||
this.subs.push(
|
||||
this.postService.createPost(postToPost, this.cookieService.get("token")).pipe(
|
||||
mergeMap(post =>
|
||||
this.authorService.attributePost(this.actualAuthor?.id, post.id, this.cookieService.get("token")).pipe(
|
||||
mergeMap((_) =>
|
||||
this.postService.changeIllustration(post.id, this.uploadedFile, this.cookieService.get("token"))
|
||||
)
|
||||
)
|
||||
)
|
||||
).subscribe({
|
||||
next: () => {
|
||||
console.log('Post créé, attribué et illustration changée avec succès.');
|
||||
},
|
||||
error: (err) => {
|
||||
this.failureMessage('Erreur', err.message);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
failureMessage(summary: string, detail: string): void {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: summary,
|
||||
detail: detail,
|
||||
life: 3000,
|
||||
closable: false
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.subs.forEach(sub => sub.unsubscribe());
|
||||
}
|
||||
|
||||
}
|
@ -23,7 +23,16 @@ export class AuthorService {
|
||||
return this.httpClient.get<Author>("http://localhost:8080/api/authors/me", {'headers': headers});
|
||||
}
|
||||
|
||||
getAuthor(id: string | null): Observable<Author> {
|
||||
attributePost(authorId: string | undefined, postId: bigint, token: string) {
|
||||
const httpOptions = {
|
||||
headers: new HttpHeaders({
|
||||
'Authorization': `Bearer ${token}`
|
||||
})
|
||||
};
|
||||
return this.httpClient.put<any>(`http://localhost:8080/api/authors/${authorId}/posts`, postId, httpOptions);
|
||||
}
|
||||
|
||||
getAuthor(id: string | undefined): Observable<Author> {
|
||||
if (id) {
|
||||
return this.httpClient.get<Author>("http://localhost:8080/api/authors/" + id);
|
||||
} else {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {Observable} from 'rxjs';
|
||||
import {Post} from '../models/post';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@ -11,6 +11,25 @@ export class PostService {
|
||||
constructor(private httpClient: HttpClient) {}
|
||||
|
||||
list(): Observable<Post[]> {
|
||||
return this.httpClient.get<Post[]>("http://localhost:8080/api/posts");
|
||||
return this.httpClient.get<Post[]>(this.url);
|
||||
}
|
||||
|
||||
createPost(post: any, token: string | undefined): Observable<Post> {
|
||||
const httpOptions = {
|
||||
headers: new HttpHeaders({
|
||||
'Authorization': `Bearer ${token}`
|
||||
})
|
||||
};
|
||||
return this.httpClient.post<Post>(this.url, post, httpOptions);
|
||||
}
|
||||
|
||||
changeIllustration(id: bigint, image: File | undefined, token: string): Observable<Post> {
|
||||
const httpOptions = {
|
||||
headers: new HttpHeaders({
|
||||
'Authorization': `Bearer ${token}`
|
||||
})
|
||||
};
|
||||
return this.httpClient.put<Post>(`${this.url}/${id}`, image, httpOptions);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,2 +1,4 @@
|
||||
@import '../node_modules/primeng/resources/themes/lara-light-indigo/theme.css';
|
||||
@import '../node_modules/primeicons/primeicons.css';
|
||||
@import '../node_modules/quill/dist/quill.bubble.css';
|
||||
@import '../node_modules/quill/dist/quill.snow.css';
|
||||
|
Loading…
Reference in New Issue
Block a user