Skip to content

Commit

Permalink
user credit overview
Browse files Browse the repository at this point in the history
  • Loading branch information
Geertvdc committed Nov 13, 2019
1 parent 5d2b9a0 commit aa6d5ca
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 6 deletions.
11 changes: 8 additions & 3 deletions Xpirit.BeerXchange/Services/BeerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,23 @@ public async Task UpdateBeer(Beer beer)

public async Task<int> 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<List<UserCredits>> GetAllUserCredits()
{
var beers = await context.Beer.ToListAsync();

List<UserCredits> userCredits = new List<UserCredits>();
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);
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/xpirit-beerxchange/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
3 changes: 3 additions & 0 deletions frontend/xpirit-beerxchange/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ <h3 matLine>Take Beer</h3>
<a mat-list-item href="/#transfer" (click)="sidenav.toggle()">
<h3 matLine>Transfer Credits</h3>
</a>
<a mat-list-item href="/#usercredits" (click)="sidenav.toggle()">
<h3 matLine>User Credits</h3>
</a>
</mat-list>
</mat-sidenav>
<mat-sidenav-content>
Expand Down
2 changes: 2 additions & 0 deletions frontend/xpirit-beerxchange/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -65,6 +66,7 @@ export function msalConfigFactory() {
FridgeBeerlistHistoryComponent,
TransferCreditComponent,
TakeBeerComponent,
ListCreditComponent,
],
imports: [
MsalModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class="card">
<div class="card-body">
<h2>User Credit Overview</h2>
<p *ngIf="!(userCredits.length > 0)"><em>Loading credits...</em></p>
<div class="mat-elevation-z8">
<table mat-table [dataSource]="userCredits" class="w-100" *ngIf="userCredits.length > 0">
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<ng-container matColumnDef="credits">
<th mat-header-cell *matHeaderCellDef> Credits </th>
<td mat-cell *matCellDef="let element"> {{element.credits}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -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<ListCreditComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ListCreditComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ListCreditComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -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<UserCredits> = [];

ngOnInit() {
this._subscription.add(timer(0, 30000).subscribe(() => {
this.setUserCreditList();
}));
}

setUserCreditList() {
this._subscription.add(this.fridgeService.getUserCredits().subscribe((userCredits: Array<UserCredits>) => {
this.userCredits = userCredits;
}));
}

ngOnDestroy() {
this._subscription.unsubscribe();
}

}
4 changes: 4 additions & 0 deletions frontend/xpirit-beerxchange/src/app/model/userCredits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface UserCredits {
name: string;
credits: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -47,4 +48,8 @@ export class FridgeService {
getFridgeUsers(): Observable<Array<string>>{
return this.http.get<Array<string>>(`${AppConfig.settings.apiUrl}/user`);
}

getUserCredits(): Observable<Array<UserCredits>>{
return this.http.get<Array<UserCredits>>(`${AppConfig.settings.apiUrl}/credit`);
}
}

0 comments on commit aa6d5ca

Please sign in to comment.