Extracted creation and delete

This commit is contained in:
Jim Martens 2021-09-23 16:56:23 +02:00
parent e5f5bea875
commit 070a6adb89
7 changed files with 115 additions and 43 deletions

View File

@ -31,7 +31,7 @@
<button class="btn btn-outline-success mx-1" type="button" (click)="editSession(session.key)">
<span class="bi bi-pencil"></span>
</button>
<button class="btn btn-outline-danger" type="button" (click)="deleteSession(session.key)">
<button class="btn btn-outline-danger" type="button" (click)="sessionService.deleteSession(session.key)">
<span class="bi bi-trash"></span>
</button>
</td>
@ -40,7 +40,7 @@
</table>
<form #addForm="ngForm"
(ngSubmit)="addSession(
(ngSubmit)="sessionService.addSession(
sessionNumber.value, sessionDate.value, currentPage
); addForm.resetForm(); sessionNumber.value = ''; sessionDate.value = ''">
<div class="row">

View File

@ -6,6 +6,7 @@ import {first} from 'rxjs/operators';
import {Body} from '../administration/bodies/body';
import {Session} from './session';
import {SessionService} from '../shared/session.service';
@Component({
selector: 'app-edit',
@ -23,7 +24,8 @@ export class EditComponent implements OnInit, OnDestroy {
constructor(private database: AngularFireDatabase,
private router: Router,
private activeRoute: ActivatedRoute) {
private activeRoute: ActivatedRoute,
public sessionService: SessionService) {
}
ngOnInit(): void {
@ -68,28 +70,8 @@ export class EditComponent implements OnInit, OnDestroy {
this.router.navigate(['edit']);
}
public addSession(sessionNumber: string, date: string, body: string | undefined): void {
if (body == null) {
console.error("body should not be undefined");
return;
}
const sessionRef = this.database.list<Session>('sessions');
sessionRef.push({
number: +sessionNumber,
date: date,
body: body
});
}
public editSession(key: string | null): void {
this.router.navigate([key], {relativeTo: this.activeRoute});
}
public deleteSession(sessionKey: string | null): void {
const sessionRef = this.database.list<Session>('sessions');
if (sessionKey != null) {
sessionRef.remove(sessionKey);
}
}
}

View File

@ -6,7 +6,7 @@ import {Observable, Subscription} from 'rxjs';
import {Faction} from '../../administration/factions/faction';
import {RecordingState} from './recording-state';
import {SpeechType} from './speech-type';
import {Speech} from './speech';
import {SpeechTimeService} from '../../shared/speech-time.service';
@Component({
selector: 'app-time-tracking',
@ -27,7 +27,8 @@ export class TimeTrackingComponent implements OnInit, OnDestroy {
private subscription: Subscription | undefined;
constructor(private database: AngularFireDatabase,
private route: ActivatedRoute) {
private route: ActivatedRoute,
private speechTimeService: SpeechTimeService) {
}
ngOnInit(): void {
@ -116,13 +117,13 @@ export class TimeTrackingComponent implements OnInit, OnDestroy {
const date = new Date();
this.speechTime += date.getTime() - this.speechStart;
this.recordTime(this.sessionKey, factionKey, this.speechTime, SpeechType.SPEECH);
this.speechTimeService.recordTime(this.sessionKey, factionKey, this.speechTime, SpeechType.SPEECH);
this.speechTime = 0;
if (this.currentCommentaryFaction != null) {
this.statePerFaction.set(this.currentCommentaryFaction, RecordingState.NOT_RECORDING);
this.commentaryTime += date.getTime() - this.commentaryStart;
this.recordTime(this.sessionKey, this.currentCommentaryFaction, this.commentaryTime, SpeechType.COMMENTARY);
this.speechTimeService.recordTime(this.sessionKey, this.currentCommentaryFaction, this.commentaryTime, SpeechType.COMMENTARY);
this.currentCommentaryFaction = undefined;
this.commentaryTime = 0;
}
@ -137,7 +138,7 @@ export class TimeTrackingComponent implements OnInit, OnDestroy {
if (this.currentCommentaryFaction != null) {
this.statePerFaction.set(this.currentCommentaryFaction, RecordingState.NOT_RECORDING);
this.commentaryTime += date.getTime() - this.commentaryStart;
this.recordTime(this.sessionKey, this.currentCommentaryFaction, this.commentaryTime, SpeechType.COMMENTARY);
this.speechTimeService.recordTime(this.sessionKey, this.currentCommentaryFaction, this.commentaryTime, SpeechType.COMMENTARY);
}
this.commentaryStart = date.getTime();
@ -173,7 +174,7 @@ export class TimeTrackingComponent implements OnInit, OnDestroy {
this.currentCommentaryFaction = undefined;
const date = new Date();
this.commentaryTime += date.getTime() - this.commentaryStart;
this.recordTime(this.sessionKey, factionKey, this.commentaryTime, SpeechType.COMMENTARY);
this.speechTimeService.recordTime(this.sessionKey, factionKey, this.commentaryTime, SpeechType.COMMENTARY);
this.commentaryTime = 0;
if (this.currentSpeechFaction != null) {
@ -181,18 +182,4 @@ export class TimeTrackingComponent implements OnInit, OnDestroy {
this.speechTime += date.getTime() - this.speechStart;
}
}
private recordTime(sessionKey: string | undefined, factionKey: string, timeInMilliseconds: number, type: SpeechType): void {
if (sessionKey == null) {
return;
}
const speechTimesRef = this.database.list<Speech>('speechTimes');
speechTimesRef.push({
sessionKey: sessionKey,
factionKey: factionKey,
timeInMilliseconds: timeInMilliseconds,
type: type,
});
}
}

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { SessionService } from './session.service';
describe('SessionService', () => {
let service: SessionService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(SessionService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,34 @@
import { Injectable } from '@angular/core';
import {AngularFireDatabase} from '@angular/fire/compat/database';
import {Session} from '../edit/session';
import {SpeechTimeService} from './speech-time.service';
@Injectable({
providedIn: 'root'
})
export class SessionService {
constructor(private database: AngularFireDatabase,
private speechTimes: SpeechTimeService) { }
public addSession(sessionNumber: string, date: string, body: string | undefined): void {
if (body == null) {
console.error("body should not be undefined");
return;
}
const sessionRef = this.database.list<Session>('sessions');
sessionRef.push({
number: +sessionNumber,
date: date,
body: body
});
}
public deleteSession(sessionKey: string | null): void {
const sessionRef = this.database.list<Session>('sessions');
if (sessionKey != null) {
sessionRef.remove(sessionKey);
this.speechTimes.deleteSpeechTimes(sessionKey);
}
}
}

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { SpeechTimeService } from './speech-time.service';
describe('SpeechTimeService', () => {
let service: SpeechTimeService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(SpeechTimeService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,37 @@
import { Injectable } from '@angular/core';
import {AngularFireDatabase} from '@angular/fire/compat/database';
import {SpeechType} from '../edit/time-tracking/speech-type';
import {Speech} from '../edit/time-tracking/speech';
@Injectable({
providedIn: 'root'
})
export class SpeechTimeService {
constructor(private database: AngularFireDatabase) { }
public recordTime(sessionKey: string | undefined, factionKey: string, timeInMilliseconds: number, type: SpeechType): void {
if (sessionKey == null) {
return;
}
const speechTimesRef = this.database.list<Speech>('speechTimes');
speechTimesRef.push({
sessionKey: sessionKey,
factionKey: factionKey,
timeInMilliseconds: timeInMilliseconds,
type: type,
});
}
public deleteSpeechTimes(sessionKey: string): void {
const speechTimesRef = this.database.list<Speech>('speechTimes');
const affectedTimes = speechTimesRef.query.orderByChild('sessionKey').equalTo(sessionKey);
affectedTimes.once('value', snapshot => {
const promises: Promise<any>[] = [];
snapshot.forEach((child) => {
promises.push(child.ref.remove());
});
Promise.all(promises);
})
}
}