import { Component } from '@angular/core'; import { BackendService, ScrumUserstory, ScrumStatus, ScrumCategory, ScrumSprint, } from '../../services/backend.service'; import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; import { SprintFormComponent } from '../sprint-form/sprint-form.component'; @Component({ selector: 'app-backlog', templateUrl: './backlog.component.html', styleUrls: ['./backlog.component.css'], }) export class BacklogComponent { public status: ScrumStatus[] = []; public categories: ScrumCategory[] = []; public sprints: ScrumSprint[] = []; public storys: ScrumUserstory[] = []; public selectedSprint: ScrumSprint; /** * Constructor of the class that initialized the communication with the backend * @param backendService * @param modalService */ constructor( private backendService: BackendService, private modalService: NgbModal ) { backendService.getSprints().subscribe((response) => { if (response.status > 399) { alert('Fehler'); } else { this.sprints.push(...response.body); this.selectedSprint = this.currentSprint; } }); backendService.getUserstories().subscribe((response) => { if (response.status > 399) { alert('Fehler'); } else { this.storys.push(...response.body); } }); backendService.getAllStatus().subscribe((response) => { if (response.status > 399) { alert('Fehler'); } else { this.status.push(...response.body); } }); backendService.getCategories().subscribe((response) => { if (response.status > 399) { alert('Fehler'); } else { this.categories.push(...response.body); } }); } /** * Formats an ISO DateString to a simple date string in the format "DD.MM.YYYY" * @param isoFormatString date formatted as an ISO DateString (Date objects get converted implicitly) */ public toDateString(isoFormatString) { const date = new Date(isoFormatString); return `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()}`; } //#region getters /** * Getter that returns an array with all userstories that are in the currently selected sprint. * The relation to the sprint is determined by the userstory's sprintId. * If no sprint is selected it just returns an empty array. */ public get choosen(): ScrumUserstory[] { if (this.selectedSprint === undefined) { return null; } return this.storys.filter((u) => u.sprintId == this.selectedSprint.id); } /** * Getter that returns an array with all userstories that aren't in any sprint. * The relation to no sprint is determined by the userstory's sprintId being undefined. */ public get backlog(): ScrumUserstory[] { return this.storys.filter((u) => u.sprintId === undefined); } /** * Getter that returns the current sprint. * The current sprint is selected by determining if todays date is between the startDate and the endDate of the sprint. */ public get currentSprint(): ScrumSprint { const now = Date.now(); return this.sprints.find( (s) => Date.parse(s.startDate) < now && Date.parse(s.endDate) > now ); } //#endregion getters //#region backlogFunctions /** * Adds a userstory to the currently selected sprint by changing it's sprintId to the current sprint's id * @param userstory userstory object that shall be added to the selected sprint's backlog */ public addToSprintBacklog(userstory: ScrumUserstory) { userstory.sprintId = this.selectedSprint.id; this.backendService.putUserstory(userstory).subscribe((response) => { if (response.status > 399) { alert('Fehler'); } }); } /** * Deletes a userstory from the currently selected sprint by changing it's sprintId to undefined * @param userstory userstory object that shall be removed from the selected sprint's backlog */ public deleteFromSprintBacklog(userstory: ScrumUserstory) { userstory.sprintId = undefined; this.backendService.putUserstory(userstory).subscribe((response) => { if (response.status > 399) { alert('Fehler'); } }); } //#endregion backlogFunctions //#region modals /** * Opens the SprintForm Modal to let the user create a new sprint. * The created sprint get pushed to the database and the local sprints array. */ public openSprintForm() { const modalRef = this.modalService.open(SprintFormComponent, { backdrop: 'static', keyboard: true, size: 'md', }); modalRef.result.then((result) => { this.sprints.push(result); }); } //#endregion modals }