Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Last active June 17, 2019 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavilanch/09b73b233b2eb6888f880dfc324e7a1b to your computer and use it in GitHub Desktop.
Save gavilanch/09b73b233b2eb6888f880dfc324e7a1b to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { AngularFirestore, DocumentReference } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { Todo } from '../models/todo';
import { TodoViewModel } from '../models/todo-view-model';
@Injectable({
providedIn: 'root'
})
export class TodoService {
constructor(private db: AngularFirestore) { }
private todoCollectionName = 'todos';
getTodos(): Observable<firebase.firestore.QuerySnapshot> {
return this.db.collection<Todo>(this.todoCollectionName, ref => ref.orderBy('lastModifiedDate', 'desc')).get();
}
saveTodo(todo: Todo): Promise<DocumentReference> {
return this.db.collection(this.todoCollectionName).add(todo);
}
editTodo(todo: TodoViewModel): Promise<void>{
return this.db.collection(this.todoCollectionName).doc(todo.id).update(todo);
}
editTodoPartial(id: string, obj: Object): Promise<void>{
return this.db.collection(this.todoCollectionName).doc(id).update(obj);
}
deleteTodo(idTodo: string): Promise<void>{
return this.db.collection(this.todoCollectionName).doc(idTodo).delete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment