import { Injectable } from "@angular/core" ; import { Post } from "../models/Post" ; import { AngularFirestore } from "@angular/fire/compat/firestore" ; import { map } from "rxjs/operators" ; import { Observable } from "rxjs" ; @ Injectable ( { providedIn : "root" , } ) export class BlogService { constructor ( private db ) { } createPost ( post ) { const postData = JSON . parse ( JSON . stringify ( post ) ) ; return this . db . collection ( "blogs" ) . add ( postData ) ; } getAllPosts ( ) { const blogs = this . db . collection < Post > ( "blogs" , ( ref ) => ref . orderBy ( "createdDate" , "desc" ) ) . snapshotChanges ( ) . pipe ( map ( ( actions ) => { return actions . map ( ( c ) => ( { postId : c . payload . doc [ "id" ] , ... c . payload . doc . data ( ) , } ) ) ; } ) ) ; return blogs ; } getPostbyId ( id ) { const blogDetails = this . db . doc < Post > ( "blogs/" + id ) . valueChanges ( ) ; return blogDetails ; } updatePost ( postId , post ) { const putData = JSON . parse ( JSON . stringify ( post ) ) ; return this . db . doc ( "blogs/" + postId ) . update ( putData ) ; } deletePost ( postId ) { return this . db . doc ( "blogs/" + postId ) . delete ( ) ; } }