From aa6d5ca768c1510017cb6ceea719bb3959459da4 Mon Sep 17 00:00:00 2001 From: Geert van der Cruijsen Date: Wed, 13 Nov 2019 19:50:13 +0100 Subject: [PATCH] user credit overview --- Xpirit.BeerXchange/Services/BeerService.cs | 11 ++++-- .../src/app/app-routing.module.ts | 4 ++- .../src/app/app.component.html | 3 ++ .../xpirit-beerxchange/src/app/app.module.ts | 2 ++ .../fridge-beerlist-current.component.ts | 2 +- .../fridge-beerlist-history.component.ts | 2 +- .../list-credit/list-credit.component.css | 0 .../list-credit/list-credit.component.html | 20 +++++++++++ .../list-credit/list-credit.component.spec.ts | 25 +++++++++++++ .../list-credit/list-credit.component.ts | 35 +++++++++++++++++++ .../src/app/model/userCredits.ts | 4 +++ .../src/app/services/fridge.service.ts | 5 +++ 12 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.css create mode 100644 frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.html create mode 100644 frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.spec.ts create mode 100644 frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.ts create mode 100644 frontend/xpirit-beerxchange/src/app/model/userCredits.ts diff --git a/Xpirit.BeerXchange/Services/BeerService.cs b/Xpirit.BeerXchange/Services/BeerService.cs index 16c8606..55d7ae3 100644 --- a/Xpirit.BeerXchange/Services/BeerService.cs +++ b/Xpirit.BeerXchange/Services/BeerService.cs @@ -61,18 +61,23 @@ public async Task UpdateBeer(Beer beer) public async Task GetUserCredits(string user) { - return await context.Beer.CountAsync(b => b.CreatedBy == user) - await context.Beer.CountAsync(b => b.RemovedBy == user); + var beers = await context.Beer.ToListAsync(); + var created = beers.Count(b => b.CreatedBy == user); + var removed = beers.Count(b => b.RemovedBy == user); + return created - removed; } public async Task> GetAllUserCredits() { + var beers = await context.Beer.ToListAsync(); + List userCredits = new List(); - foreach (var user in context.Beer.Select(b => b.CreatedBy).Distinct()) + foreach (var user in beers.Select(b => b.CreatedBy).Distinct()) { UserCredits credit = new UserCredits() { Name = user, - Credits = await context.Beer.CountAsync(b => b.CreatedBy == user) - await context.Beer.CountAsync(b => b.RemovedBy == user) + Credits = beers.Count(b => b.CreatedBy == user) - beers.Count(b => b.RemovedBy == user) }; userCredits.Add(credit); } diff --git a/frontend/xpirit-beerxchange/src/app/app-routing.module.ts b/frontend/xpirit-beerxchange/src/app/app-routing.module.ts index 6e65e1f..510d677 100644 --- a/frontend/xpirit-beerxchange/src/app/app-routing.module.ts +++ b/frontend/xpirit-beerxchange/src/app/app-routing.module.ts @@ -7,13 +7,15 @@ import { AddBeerComponent } from './components/add-beer/add-beer.component'; import { ViewCreditComponent } from './components/view-credit/view-credit.component'; import { TransferCreditComponent } from './components/transfer-credit/transfer-credit.component'; import { TakeBeerComponent } from './components/take-beer/take-beer.component'; +import { ListCreditComponent } from './components/list-credit/list-credit.component'; const routes: Routes = [ { path: '', component: HomeComponent, canActivate: [MsalGuard] }, { path: 'add', component: AddBeerComponent, canActivate: [MsalGuard] }, { path: 'credit', component: ViewCreditComponent, canActivate: [MsalGuard] }, { path: 'take', component: TakeBeerComponent, canActivate: [MsalGuard] }, - { path: 'transfer', component: TransferCreditComponent, canActivate: [MsalGuard] } + { path: 'transfer', component: TransferCreditComponent, canActivate: [MsalGuard] }, + { path: 'usercredits', component: ListCreditComponent, canActivate: [MsalGuard] } ]; @NgModule({ diff --git a/frontend/xpirit-beerxchange/src/app/app.component.html b/frontend/xpirit-beerxchange/src/app/app.component.html index ba523b3..0540d03 100644 --- a/frontend/xpirit-beerxchange/src/app/app.component.html +++ b/frontend/xpirit-beerxchange/src/app/app.component.html @@ -29,6 +29,9 @@

Take Beer

Transfer Credits

+ +

User Credits

+
diff --git a/frontend/xpirit-beerxchange/src/app/app.module.ts b/frontend/xpirit-beerxchange/src/app/app.module.ts index 19ab18a..733ef19 100644 --- a/frontend/xpirit-beerxchange/src/app/app.module.ts +++ b/frontend/xpirit-beerxchange/src/app/app.module.ts @@ -34,6 +34,7 @@ import { FridgeBeerlistHistoryComponent } from './components/fridge-beerlist-his import { TransferCreditComponent } from './components/transfer-credit/transfer-credit.component'; import { TakeBeerComponent } from './components/take-beer/take-beer.component'; import { FridgeService } from './services/fridge.service'; +import { ListCreditComponent } from './components/list-credit/list-credit.component'; export function initializeApp(appConfig: AppConfig) { const promise = appConfig.loadAppConfig().pipe(tap((settings: IAppConfig) => { @@ -65,6 +66,7 @@ export function msalConfigFactory() { FridgeBeerlistHistoryComponent, TransferCreditComponent, TakeBeerComponent, + ListCreditComponent, ], imports: [ MsalModule, diff --git a/frontend/xpirit-beerxchange/src/app/components/fridge-beerlist-current/fridge-beerlist-current.component.ts b/frontend/xpirit-beerxchange/src/app/components/fridge-beerlist-current/fridge-beerlist-current.component.ts index 7c991f0..bb50ffa 100644 --- a/frontend/xpirit-beerxchange/src/app/components/fridge-beerlist-current/fridge-beerlist-current.component.ts +++ b/frontend/xpirit-beerxchange/src/app/components/fridge-beerlist-current/fridge-beerlist-current.component.ts @@ -9,7 +9,7 @@ import { Subscription, timer } from 'rxjs'; styleUrls: ['./fridge-beerlist-current.component.css'] }) export class FridgeBeerlistCurrentComponent implements OnInit, OnDestroy { - displayedColumns: string[] = ['name', 'brewery', 'country', 'createdBy', 'createdBy', 'addedDate']; + displayedColumns: string[] = ['name', 'brewery', 'country', 'createdBy', 'addedDate']; private readonly _subscription: Subscription = new Subscription(); constructor(private fridgeService: FridgeService) { } diff --git a/frontend/xpirit-beerxchange/src/app/components/fridge-beerlist-history/fridge-beerlist-history.component.ts b/frontend/xpirit-beerxchange/src/app/components/fridge-beerlist-history/fridge-beerlist-history.component.ts index 9faa0e0..f54459e 100644 --- a/frontend/xpirit-beerxchange/src/app/components/fridge-beerlist-history/fridge-beerlist-history.component.ts +++ b/frontend/xpirit-beerxchange/src/app/components/fridge-beerlist-history/fridge-beerlist-history.component.ts @@ -9,7 +9,7 @@ import { Beer } from 'src/app/model/beer'; styleUrls: ['./fridge-beerlist-history.component.css'] }) export class FridgeBeerlistHistoryComponent implements OnInit, OnDestroy { - displayedColumns: string[] = ['name', 'brewery', 'country', 'createdBy', 'createdBy', 'addedDate', 'removedBy', 'removedDate']; + displayedColumns: string[] = ['name', 'brewery', 'country', 'createdBy', 'addedDate', 'removedBy', 'removedDate']; private readonly _subscription: Subscription = new Subscription(); diff --git a/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.css b/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.css new file mode 100644 index 0000000..e69de29 diff --git a/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.html b/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.html new file mode 100644 index 0000000..cf17654 --- /dev/null +++ b/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.html @@ -0,0 +1,20 @@ +
+
+

User Credit Overview

+

Loading credits...

+
+ + + + + + + + + + + +
Name {{element.name}} Credits {{element.credits}}
+
+
+
\ No newline at end of file diff --git a/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.spec.ts b/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.spec.ts new file mode 100644 index 0000000..e27dc02 --- /dev/null +++ b/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ListCreditComponent } from './list-credit.component'; + +describe('ListCreditComponent', () => { + let component: ListCreditComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ListCreditComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ListCreditComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.ts b/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.ts new file mode 100644 index 0000000..754d548 --- /dev/null +++ b/frontend/xpirit-beerxchange/src/app/components/list-credit/list-credit.component.ts @@ -0,0 +1,35 @@ +import { Component, OnInit } from '@angular/core'; +import { Subscription, timer } from 'rxjs'; +import { FridgeService } from 'src/app/services/fridge.service'; +import { UserCredits } from '../../model/userCredits'; + +@Component({ + selector: 'list-credit', + templateUrl: './list-credit.component.html', + styleUrls: ['./list-credit.component.css'] +}) +export class ListCreditComponent implements OnInit { + displayedColumns: string[] = ['name', 'credits']; + private readonly _subscription: Subscription = new Subscription(); + + constructor(private fridgeService: FridgeService) { } + + userCredits: Array = []; + + ngOnInit() { + this._subscription.add(timer(0, 30000).subscribe(() => { + this.setUserCreditList(); + })); + } + + setUserCreditList() { + this._subscription.add(this.fridgeService.getUserCredits().subscribe((userCredits: Array) => { + this.userCredits = userCredits; + })); + } + + ngOnDestroy() { + this._subscription.unsubscribe(); + } + +} diff --git a/frontend/xpirit-beerxchange/src/app/model/userCredits.ts b/frontend/xpirit-beerxchange/src/app/model/userCredits.ts new file mode 100644 index 0000000..c36af62 --- /dev/null +++ b/frontend/xpirit-beerxchange/src/app/model/userCredits.ts @@ -0,0 +1,4 @@ +export interface UserCredits { + name: string; + credits: number; +} \ No newline at end of file diff --git a/frontend/xpirit-beerxchange/src/app/services/fridge.service.ts b/frontend/xpirit-beerxchange/src/app/services/fridge.service.ts index 54093cc..af7d43c 100644 --- a/frontend/xpirit-beerxchange/src/app/services/fridge.service.ts +++ b/frontend/xpirit-beerxchange/src/app/services/fridge.service.ts @@ -6,6 +6,7 @@ import { Beer } from '../model/beer'; import { BeerAddition } from '../model/beerAddition'; import { BeerRemoval } from '../model/beerRemoval'; import { CreditTransfer } from '../model/creditTransfer'; +import { UserCredits } from '../model/userCredits'; @Injectable({ @@ -47,4 +48,8 @@ export class FridgeService { getFridgeUsers(): Observable>{ return this.http.get>(`${AppConfig.settings.apiUrl}/user`); } + + getUserCredits(): Observable>{ + return this.http.get>(`${AppConfig.settings.apiUrl}/credit`); + } }