-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathboard.service.ts
More file actions
88 lines (81 loc) · 2.14 KB
/
board.service.ts
File metadata and controls
88 lines (81 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';
import * as firebase from 'firebase/app';
import { switchMap, map } from 'rxjs/operators';
import { Board, Task } from './board.model';
@Injectable({
providedIn: 'root'
})
export class BoardService {
constructor(private afAuth: AngularFireAuth, private db: AngularFirestore) {}
/**
* Creates a new board for the current user
*/
async createBoard(data: Board) {
const user = await this.afAuth.auth.currentUser;
return this.db.collection('boards').add({
...data,
uid: user.uid,
tasks: [{ description: 'Hello!', label: 'yellow' }]
});
}
/**
* Get all boards owned by current user
*/
getUserBoards() {
return this.afAuth.authState.pipe(
switchMap(user => {
if (user) {
return this.db
.collection<Board>('boards', ref =>
ref.where('uid', '==', user.uid).orderBy('priority')
)
.valueChanges({ idField: 'id' });
} else {
return [];
}
}),
// map(boards => boards.sort((a, b) => a.priority - b.priority))
);
}
/**
* Run a batch write to change the priority of each board for sorting
*/
sortBoards(boards: Board[]) {
const db = firebase.firestore();
const batch = db.batch();
const refs = boards.map(b => db.collection('boards').doc(b.id));
refs.forEach((ref, idx) => batch.update(ref, { priority: idx }));
batch.commit();
}
/**
* Delete board
*/
deleteBoard(boardId: string) {
return this.db
.collection('boards')
.doc(boardId)
.delete();
}
/**
* Updates the tasks on board
*/
updateTasks(boardId: string, tasks: Task[]) {
return this.db
.collection('boards')
.doc(boardId)
.update({ tasks });
}
/**
* Remove a specific task from the board
*/
removeTask(boardId: string, task: Task) {
return this.db
.collection('boards')
.doc(boardId)
.update({
tasks: firebase.firestore.FieldValue.arrayRemove(task)
});
}
}