Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
maryanchuk04 committed Aug 6, 2023
1 parent 6f540f0 commit 9096223
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 56 deletions.
126 changes: 71 additions & 55 deletions YeProfspilka.Admin/src/app/store/effects/question.effect.ts
Original file line number Diff line number Diff line change
@@ -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;
})
)
)
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace YeProfspilka.Backend.Controllers;

[ApiController]
[Route("question")]
[Authorize]
public class QuestionController : ControllerBase
{
private readonly IQuestionService _questionService;
Expand Down Expand Up @@ -65,7 +66,6 @@ public async Task<IActionResult> Update(QuestionDto questionDto)
}

[HttpDelete("{id}")]
[Authorize(Policy = PolicyNames.ModeratorAndAdminPolicyName)]
public async Task<IActionResult> Delete(Guid id)
{
try
Expand Down

0 comments on commit 9096223

Please sign in to comment.