diff --git a/YeProfspilka.Admin/src/app/store/effects/question.effect.ts b/YeProfspilka.Admin/src/app/store/effects/question.effect.ts index 65e50db..09b8e0a 100644 --- a/YeProfspilka.Admin/src/app/store/effects/question.effect.ts +++ b/YeProfspilka.Admin/src/app/store/effects/question.effect.ts @@ -1,67 +1,83 @@ -import { Injectable } from '@angular/core'; +import { ToastrService, } from 'ngx-toastr'; +import { catchError, EMPTY, exhaustMap, map, mergeMap, of, } from 'rxjs'; +import { QuestionService, } from 'src/app/services/question.service'; -import { Actions, createEffect, ofType } from '@ngrx/effects'; -import { ToastrService } from 'ngx-toastr'; -import { catchError, exhaustMap, map, mergeMap, of } from 'rxjs'; - -import { QuestionService } from 'src/app/services/question.service'; +import { Injectable, } from '@angular/core'; +import { Actions, createEffect, ofType, } from '@ngrx/effects'; import { - createQuestion, - createQuestionSuccess, - deleteQuestion, - deleteQuestionSuccess, - fetchQuestions, - fetchQuestionsSuccess, - updateQuestion, - updateQuestionSuccess, + createQuestion, createQuestionSuccess, deleteQuestion, deleteQuestionSuccess, fetchQuestions, + fetchQuestionsSuccess, updateQuestion, updateQuestionSuccess, } from '../actions/questions.action'; @Injectable() export class QuestionEffect { - constructor(private actions$: Actions, private questionService: QuestionService, private toastrService: ToastrService) {} + constructor( + private actions$: Actions, + private questionService: QuestionService, + private toastrService: ToastrService + ) {} - fetchQuestions$ = createEffect(() => - this.actions$.pipe( - ofType(fetchQuestions), - exhaustMap(() => this.questionService.getAll().pipe(map((questions) => fetchQuestionsSuccess({ questions })))) - ) - ); + fetchQuestions$ = createEffect(() => + this.actions$.pipe( + ofType(fetchQuestions), + exhaustMap(() => + this.questionService + .getAll() + .pipe(map((questions) => fetchQuestionsSuccess({ questions }))) + ) + ) + ); - createQuestion$ = createEffect(() => - this.actions$.pipe( - ofType(createQuestion), - mergeMap(({ question }) => - this.questionService - .create({ - answer: question.answer, - questionText: question.questionText, - }) - .pipe( - map(() => { - this.toastrService.success('Питання успішно створено!'); - return createQuestionSuccess({ question }); - }), - catchError(() => { - this.toastrService.success('Питання успішно створено!'); - return of(undefined); - }) - ) - ) - ) - ); + createQuestion$ = createEffect(() => + this.actions$.pipe( + ofType(createQuestion), + mergeMap(({ question }) => + this.questionService + .create({ + answer: question.answer, + questionText: question.questionText, + }) + .pipe( + map(() => { + this.toastrService.success('Питання успішно створено!'); + return createQuestionSuccess({ question }); + }), + catchError(() => { + this.toastrService.success('Питання успішно створено!'); + return of(undefined); + }) + ) + ) + ) + ); - updateQuestion$ = createEffect(() => - this.actions$.pipe( - ofType(updateQuestion), - exhaustMap(({ question }) => this.questionService.update(question).pipe(map((question) => updateQuestionSuccess({ question })))) - ) - ); + updateQuestion$ = createEffect(() => + this.actions$.pipe( + ofType(updateQuestion), + exhaustMap(({ question }) => + this.questionService + .update(question) + .pipe(map((question) => updateQuestionSuccess({ question }))) + ) + ) + ); - deleteQuestion$ = createEffect(() => - this.actions$.pipe( - ofType(deleteQuestion), - exhaustMap(({ id }) => this.questionService.delete(id).pipe(map(() => deleteQuestionSuccess({ id })))) - ) - ); + deleteQuestion$ = createEffect(() => + this.actions$.pipe( + ofType(deleteQuestion), + exhaustMap(({ id }) => + this.questionService.delete(id).pipe( + map(() => { + this.toastrService.success('Успішно видалено'); + return deleteQuestionSuccess({ id }); + }), + catchError((err) => { + this.toastrService.success('Щось пішло не так!'); + return EMPTY; + }) + ) + ) + ) + ); } diff --git a/YeProfspilka.Backend/YeProfspilka.API/Controllers/QuestionController.cs b/YeProfspilka.Backend/YeProfspilka.API/Controllers/QuestionController.cs index 5659345..fb44a05 100644 --- a/YeProfspilka.Backend/YeProfspilka.API/Controllers/QuestionController.cs +++ b/YeProfspilka.Backend/YeProfspilka.API/Controllers/QuestionController.cs @@ -9,6 +9,7 @@ namespace YeProfspilka.Backend.Controllers; [ApiController] [Route("question")] +[Authorize] public class QuestionController : ControllerBase { private readonly IQuestionService _questionService; @@ -65,7 +66,6 @@ public async Task Update(QuestionDto questionDto) } [HttpDelete("{id}")] - [Authorize(Policy = PolicyNames.ModeratorAndAdminPolicyName)] public async Task Delete(Guid id) { try