SKE-65 can solve competition method

This commit is contained in:
Przemysław Stawujak 2019-01-03 11:41:46 +01:00
parent 4ef8928ec9
commit 9ecd87db5d
5 changed files with 135 additions and 19 deletions

View File

@ -7,7 +7,7 @@
<div class="description"> <div class="description">
<div class="section-color">Nagrody:</div> {{competition.prize}} <div class="section-color">Nagrody:</div> {{competition.prize}}
</div> </div>
<button type="button" style="margin-top: 20px" [disabled]="mode==='quiz'" class="btn btn-success" (click)="solveCompetition()"> <button type="button" style="margin-top: 20px" [disabled]="mode==='quiz' || !canSolveCompetition" class="btn btn-success" (click)="solveCompetition()">
{{l("Start")}} {{l("Start")}}
</button> </button>
<hr /> <hr />

View File

@ -3,8 +3,8 @@ import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/app-component-base'; import { AppComponentBase } from '@shared/app-component-base';
import { Subscription } from 'rxjs/Rx'; import { Subscription } from 'rxjs/Rx';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { mergeMap } from 'rxjs/operators';
import { finalize } from 'rxjs/operators'; import { finalize } from 'rxjs/operators';
import { forkJoin } from 'rxjs';
import { CompetitionServiceProxy, import { CompetitionServiceProxy,
CompetitionDto, CompetitionDto,
QuestionDto, QuestionDto,
@ -18,11 +18,14 @@ import { CompetitionServiceProxy,
export class CompetitionDetailComponent extends AppComponentBase implements OnInit, OnDestroy { export class CompetitionDetailComponent extends AppComponentBase implements OnInit, OnDestroy {
private paramSubscription: Subscription; private paramSubscription: Subscription;
private routeSubscription: Subscription;
public competitionId: number; public competitionId: number;
public competition: CompetitionDto = null; public competition: CompetitionDto = null;
public competitionDetailAreaId: string = 'competition-detail-area'; public competitionDetailAreaId: string = 'competition-detail-area';
public canSolveCompetition: boolean = false;
public mode = 'detail'; public mode = 'detail';
public pager = { public pager = {
@ -41,25 +44,31 @@ export class CompetitionDetailComponent extends AppComponentBase implements OnIn
public ngOnInit() { public ngOnInit() {
this.competition = new CompetitionDto(); this.competition = new CompetitionDto();
this.getCompetition();
this.routeSubscription = this.route.params
.subscribe(params => {
this.competitionId = +params['id'];
console.log('competitionId: ' + this.competitionId);
this.getCompetition();
})
} }
private getCompetition(): void { private getCompetition(): void {
this.paramSubscription = this.route.params this.setBusy(this.competitionDetailAreaId);
.pipe(mergeMap(params => {
this.competitionId = +params['id']; this.paramSubscription = forkJoin([this._competitionService.getCompetition(this.competitionId),
console.log('competitionId: ' + this.competitionId); this._competitionService.canSolveCompetition(this.competitionId)])
this.setBusy(this.competitionDetailAreaId); .pipe(finalize(() => { this.clearBusy(this.competitionDetailAreaId); }))
const competitionDetailStream = this._competitionService .subscribe((data: [CompetitionDto, boolean]) => {
.getCompetition(this.competitionId) this.competition = data[0];
.pipe(finalize(() => { this.clearBusy(this.competitionDetailAreaId); })) this.competition.questions.forEach((x) => this.shuffleOptions(x.questionOptions));
return competitionDetailStream this.pager.count = this.competition.questions.length;
})).subscribe((result: CompetitionDto) => { console.log(this.competition);
this.competition = result;
this.competition.questions.forEach((x) => this.shuffleOptions(x.questionOptions)); this.canSolveCompetition = data[1];
this.pager.count = this.competition.questions.length; console.log(this.canSolveCompetition);
console.log(this.competition); });
});
} }
private shuffleOptions(array) { private shuffleOptions(array) {
@ -120,6 +129,10 @@ export class CompetitionDetailComponent extends AppComponentBase implements OnIn
if (this.paramSubscription) { if (this.paramSubscription) {
this.paramSubscription.unsubscribe(); this.paramSubscription.unsubscribe();
} }
if (this.routeSubscription) {
this.routeSubscription.unsubscribe();
}
} }
} }

View File

@ -328,6 +328,61 @@ export class CompetitionServiceProxy {
} }
return _observableOf<CompetitionDto>(<any>null); return _observableOf<CompetitionDto>(<any>null);
} }
/**
* @competitionId (optional)
* @return Success
*/
canSolveCompetition(competitionId: number | null | undefined): Observable<boolean> {
let url_ = this.baseUrl + "/api/services/app/Competition/CanSolveCompetition?";
if (competitionId !== undefined)
url_ += "competitionId=" + encodeURIComponent("" + competitionId) + "&";
url_ = url_.replace(/[?&]$/, "");
let options_ : any = {
observe: "response",
responseType: "blob",
headers: new HttpHeaders({
"Content-Type": "application/json",
"Accept": "application/json"
})
};
return this.http.request("post", url_, options_).pipe(_observableMergeMap((response_ : any) => {
return this.processCanSolveCompetition(response_);
})).pipe(_observableCatch((response_: any) => {
if (response_ instanceof HttpResponseBase) {
try {
return this.processCanSolveCompetition(<any>response_);
} catch (e) {
return <Observable<boolean>><any>_observableThrow(e);
}
} else
return <Observable<boolean>><any>_observableThrow(response_);
}));
}
protected processCanSolveCompetition(response: HttpResponseBase): Observable<boolean> {
const status = response.status;
const responseBlob =
response instanceof HttpResponse ? response.body :
(<any>response).error instanceof Blob ? (<any>response).error : undefined;
let _headers: any = {}; if (response.headers) { for (let key of response.headers.keys()) { _headers[key] = response.headers.get(key); }};
if (status === 200) {
return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
let result200: any = null;
let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result200 = resultData200 !== undefined ? resultData200 : <any>null;
return _observableOf(result200);
}));
} else if (status !== 200 && status !== 204) {
return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
}));
}
return _observableOf<boolean>(<any>null);
}
} }
@Injectable() @Injectable()

View File

@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using SystemKonkursow.Authorization.Users;
using SystemKonkursow.Competition.CompetitionCategory.Dto; using SystemKonkursow.Competition.CompetitionCategory.Dto;
namespace SystemKonkursow.Competition.CompetitionCategory namespace SystemKonkursow.Competition.CompetitionCategory
@ -15,16 +16,22 @@ namespace SystemKonkursow.Competition.CompetitionCategory
private readonly IRepository<Domain.Competition, long> _competitionRepository; private readonly IRepository<Domain.Competition, long> _competitionRepository;
private readonly IRepository<Domain.Question, int> _questionRepository; private readonly IRepository<Domain.Question, int> _questionRepository;
private readonly IRepository<Domain.QuestionOption, int> _questionOptionRepository; private readonly IRepository<Domain.QuestionOption, int> _questionOptionRepository;
private readonly UserManager _userManager;
private readonly IRepository<Domain.Participant, int> _participantRepository;
public CompetitionAppService(IRepository<Domain.CompetitionCategory, int> competitionCategoryRepository, public CompetitionAppService(IRepository<Domain.CompetitionCategory, int> competitionCategoryRepository,
IRepository<Domain.Competition, long> competitionRepository, IRepository<Domain.Competition, long> competitionRepository,
IRepository<Domain.Question, int> questionRepository, IRepository<Domain.Question, int> questionRepository,
IRepository<Domain.QuestionOption, int> questionOptionRepository) IRepository<Domain.QuestionOption, int> questionOptionRepository,
UserManager userManager,
IRepository<Domain.Participant, int> participantRepository)
{ {
_competitionCategoryRepository = competitionCategoryRepository; _competitionCategoryRepository = competitionCategoryRepository;
_competitionRepository = competitionRepository; _competitionRepository = competitionRepository;
_questionRepository = questionRepository; _questionRepository = questionRepository;
_questionOptionRepository = questionOptionRepository; _questionOptionRepository = questionOptionRepository;
_userManager = userManager;
_participantRepository = participantRepository;
} }
[AbpAuthorize] [AbpAuthorize]
@ -116,5 +123,34 @@ namespace SystemKonkursow.Competition.CompetitionCategory
return mappedObject; return mappedObject;
} }
[AbpAuthorize]
public async Task<bool> CanSolveCompetition(int competitionId)
{
var user = await GetCurrentUserAsync();
var isParticipant = await _userManager.IsParticipantUserAsync(user);
if (isParticipant)
{
var participant = await _participantRepository.GetAll()
.FirstOrDefaultAsync(t => t.UserId == user.Id);
var competition = await _competitionRepository.GetAll()
.FirstOrDefaultAsync(t => t.Id == competitionId);
if (participant.ParticipantClass < competition.MinClass || participant.ParticipantClass > competition.MaxClass)
{
return false;
}
return true;
}
else
{
return false;
}
}
} }
} }

View File

@ -11,6 +11,7 @@ using Abp.Domain.Uow;
using Abp.Organizations; using Abp.Organizations;
using Abp.Runtime.Caching; using Abp.Runtime.Caching;
using SystemKonkursow.Authorization.Roles; using SystemKonkursow.Authorization.Roles;
using System.Threading.Tasks;
namespace SystemKonkursow.Authorization.Users namespace SystemKonkursow.Authorization.Users
{ {
@ -54,5 +55,16 @@ namespace SystemKonkursow.Authorization.Users
settingManager) settingManager)
{ {
} }
public async Task<bool> CheckHasUserRole(User user, string roleName)
{
return await IsInRoleAsync(user, roleName);
}
public async Task<bool> IsParticipantUserAsync(User user)
{
return await CheckHasUserRole(user, StaticRoleNames.Host.Participant);
}
} }
} }