get all shopkeepers

This commit is contained in:
Łukasz Góreczny 2021-01-21 18:19:24 +01:00
parent f9c41061e1
commit 20dec1e4eb
7 changed files with 83 additions and 31 deletions

View File

@ -53,6 +53,7 @@ import { GameMasterShopkeepersTableComponent } from './components/game-master-sh
import { GameMasterTurntrackerComponent } from './components/game-master-turntracker/game-master-turntracker.component';
import { DragDropModule } from '@angular/cdk/drag-drop';
import { ChooseMonsterDialogComponent } from './components/choose-monster-dialog/choose-monster-dialog.component';
import { ShopkeeperService } from '../services/shopkeeper.service';
@NgModule({
declarations: [
@ -113,6 +114,7 @@ import { ChooseMonsterDialogComponent } from './components/choose-monster-dialog
ArmorService,
OtherEquipmentService,
MonsterService,
ShopkeeperService,
],
bootstrap: [AppComponent],
entryComponents: [

View File

@ -97,6 +97,7 @@ export class GameMasterDashboardComponent implements OnInit, OnDestroy {
ngOnInit() {
this.signalRService.Login();
this.UpdateCharactersList();
}
UpdateSidenavStatus(sidenav: string, newValue: boolean) {

View File

@ -11,20 +11,16 @@
<td mat-cell *matCellDef="let row"> {{row.name}} </td>
</ng-container>
<ng-container matColumnDef="itemsCount">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Items Count </th>
<td mat-cell *matCellDef="let row"> {{row.itemsCount}} </td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef> Actions </th>
<td mat-cell *matCellDef="let row">
<button mat-flat-button
*ngIf="row.isAvailable"
color="red">
color="red"
(click)="DeactivateShopkeeper(row.id)">
Deactivate
</button>
<button mat-flat-button *ngIf="!row.isAvailable" color="green">
<button mat-flat-button *ngIf="!row.isAvailable" color="green" (click)="ActivateShopkeeper(row.id)">
Activate
</button>
<button mat-flat-button color="warn" >

View File

@ -3,6 +3,11 @@ import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { MonsterViewModel } from '../../../types/viewmodels/monster-viewmodels/MonsterViewModel';
import { ShopkeeperViewModel } from '../../../types/viewmodels/shopkeeper-viewmodels/ShopkeeperViewModel';
import { ShopkeeperService } from '../../../services/shopkeeper.service';
import { first } from 'rxjs/operators';
import { ErrorResponse } from '../../../types/ErrorResponse';
import { HttpErrorResponse } from '@angular/common/http';
@Component({
selector: 'app-game-master-shopkeepers-table',
@ -10,35 +15,44 @@ import { MonsterViewModel } from '../../../types/viewmodels/monster-viewmodels/M
styleUrls: ['./game-master-shopkeepers-table.component.css'],
})
export class GameMasterShopkeepersTableComponent implements OnInit {
displayedColumns: string[] = ['name', 'itemsCount', 'actions'];
dataSource: MatTableDataSource<{
name: string;
itemsCount: number;
isAvailable: boolean;
}>; //TODO zmienić na skopkeeper view model
displayedColumns: string[] = ['name', 'actions'];
dataSource: MatTableDataSource<ShopkeeperViewModel>;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort;
constructor() {}
constructor(private shopkeeperService: ShopkeeperService) {}
ngOnInit() {
this.dataSource = new MatTableDataSource<{
name: string;
itemsCount: number;
isAvailable: boolean;
}>([
{
name: 'Test',
itemsCount: 12,
isAvailable: true,
this.shopkeeperService
.GetAllShopkeepers()
.pipe(first())
.subscribe(
(result) => {
this.dataSource = new MatTableDataSource(result);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
},
{
name: 'Test2',
itemsCount: 12,
isAvailable: false,
},
]);
(error: ErrorResponse | HttpErrorResponse) => {
console.error(error);
if (error instanceof HttpErrorResponse) {
error = error.error as ErrorResponse;
}
console.error(error.message);
}
);
}
ActivateShopkeeper(shopkeeperId: number) {
if (this.dataSource.data.find((e) => e.isAvailable) != null) {
this.dataSource.data.find((e) => e.id == shopkeeperId).isAvailable = true;
//TODO podpięcie pod backend zmianę
}
}
DeactivateShopkeeper(shopkeeperId: number) {
this.dataSource.data.find((e) => e.id == shopkeeperId).isAvailable = true;
//TODO podpięcie pod backend zmianę
}
applyFilter(event: Event) {

View File

@ -0,0 +1,34 @@
import { Inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of, throwError } from 'rxjs';
import { ShopkeeperViewModel } from '../types/viewmodels/shopkeeper-viewmodels/ShopkeeperViewModel';
import { Either } from '../types/Either';
import { ErrorResponse } from '../types/ErrorResponse';
import { switchMap } from 'rxjs/operators';
Injectable({
providedIn: 'root',
});
export class ShopkeeperService {
private baseUrl = 'api/shopkeeper/';
constructor(private http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
this.baseUrl = baseUrl + this.baseUrl;
}
GetAllShopkeepers(): Observable<ShopkeeperViewModel[]> {
return this.http
.get<Either<ShopkeeperViewModel[], ErrorResponse>>(
this.baseUrl + 'getShopkeepers'
)
.pipe(
switchMap((response) => {
debugger;
if (response.isLeft) {
return of(response.left);
} else {
return throwError(response.right);
}
})
);
}
}

View File

@ -0,0 +1,5 @@
export interface ShopkeeperViewModel {
id: number;
name: string;
isAvailable: boolean;
}

View File

@ -26,7 +26,7 @@ namespace SessionCompanion.Controllers
/// </summary>
/// <returns>Lista sklepikarzy</returns>
[HttpGet("getShopkeepers")]
public async Task<List<ShopkeeperViewModel>> GetShopkeepers()
public async Task<Either<List<ShopkeeperViewModel>, ErrorResponse>> GetShopkeepers()
{
return _service.Get().ToList();
}