Skip to content

Commit

Permalink
Split FirebaseService into different services #6
Browse files Browse the repository at this point in the history
  • Loading branch information
djsanabriac committed Apr 8, 2020
1 parent 603c70d commit a4dae9a
Showing 7 changed files with 244 additions and 208 deletions.
4 changes: 2 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { AngularFireAuth } from '@angular/fire/auth';
import { auth } from 'firebase/app';
import { AuthService } from './shared/services/auth.service';
import { StorageService } from './shared/services/storage.service';
import {FirebaseService} from "./shared/services/firebase.service";
import {PatientsService} from "./shared/services/patients.service";

@Component({
selector: 'app-root',
@@ -12,5 +12,5 @@ import {FirebaseService} from "./shared/services/firebase.service";
})
export class AppComponent {
title = 'AyudandoAyudar';
constructor(authService: AuthService, storageService: StorageService, firebaseService: FirebaseService) {}
constructor(authService: AuthService, storageService: StorageService, patientsService: PatientsService) {}
}
44 changes: 43 additions & 1 deletion src/app/shared/services/courier.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
import { Injectable } from '@angular/core';
import {AngularFirestore, AngularFirestoreCollection, DocumentReference, QueryFn} from "@angular/fire/firestore";
import {Courier} from "../models/courier.model";
import {Observable} from "rxjs";
import {map} from "rxjs/operators";

@Injectable({
providedIn: 'root'
})
export class CourierService {

constructor() { }
constructor(private firestore: AngularFirestore) { }

// ------------------------------ Couriers ----------------------------------

couriersCollection: AngularFirestoreCollection<Courier>;
couriers: Observable<Courier[]>;

createCourier(courier: Courier): Promise<DocumentReference>{
return this.firestore.collection('couriers').add({...courier});
}

getCouriers(queryFn?: QueryFn){

this.couriersCollection = queryFn
? this.firestore.collection<Courier>('couriers', queryFn)
: this.firestore.collection<Courier>('couriers');

this.couriers = this.couriersCollection.snapshotChanges().pipe(
map(actions => actions.map( a =>{
let data = a.payload.doc.data() as Courier;
data.id = a.payload.doc.id;
return data;
}
))
);

return this.couriers;
}

updateCourier(courier: Courier){
return this.firestore.doc("couriers/"+courier.id).update({...courier});
}

deleteCourier(courierId: string){
return this.firestore.doc("couriers/" + courierId).delete();
}

// --------------------------------------------------------------------------

}
203 changes: 0 additions & 203 deletions src/app/shared/services/firebase.service.ts
Original file line number Diff line number Diff line change
@@ -7,13 +7,6 @@ import {
QueryFn
} from "@angular/fire/firestore";
import {User} from "../models/user.model";
import {Patient} from "../models/patient.model";
import {Observable} from "rxjs";
import actions from "@angular/fire/schematics/deploy/actions";
import {map} from "rxjs/operators";
import {Medicine} from "../models/medicine.model";
import {Courier} from "../models/courier.model";
import {Order} from "../models/order.model";

@Injectable({
providedIn: 'root'
@@ -23,54 +16,6 @@ export class FirebaseService {
users: User[];

constructor(private firestore: AngularFirestore) {

//FIXME Delete examples once we start to use the service

let patient = new Patient();
patient.name = "name";
patient.idNumber = "123456";
patient.birth_date = new Date();
patient.email = "email@email.com"
patient.address = "Calle Falsa 123";
patient.phoneNumber = "=573213331112"

/*this.createPatient(patient)
.then(value => console.debug("Created"))
.catch(reason => console.error("[error]"));*/

this.getPatients( ref => ref.where("idNumber", "==", "123456"))
.subscribe(patients => {
// from another service we would use:
// this.patients = value;
console.debug("[DEBUG] WITH query function", patients);
});

this.getPatients()
.subscribe(patients => {
// from another service, with patients: Observable<Patient>, we would use:
// this.patients = value;
console.debug("[DEBUG] WITHOUT query function", patients);
});


patient.id = "vvmpDJp1aRs0pIS0sSKD"; // ID comes when we use getPatients
patient.phoneNumber = "+573213213211"
this.updatePatient(patient)
.then(value => {
console.debug("[DEBUG] Updated", value);
})
.catch(reason => {
console.debug("[DEBUG] Error updating", reason);
});

this.deletePatient("tI4e9H6Jkr9U5AdIX0Kp") // A valid id
.then(value => {
console.debug("[DEBUG] Deleted", value);
})
.catch(reason => {
console.debug("[DEBUG] Error deleting", reason);
});

}

getUsers(){
@@ -80,152 +25,4 @@ export class FirebaseService {
});
}

// ------------------------------ Patients ----------------------------------

patientsCollection: AngularFirestoreCollection<Patient>;
patients: Observable<Patient[]>;

createPatient(patient: Patient): Promise<DocumentReference>{
return this.firestore.collection('patients').add({...patient});
}

getPatients(queryFn?: QueryFn){

this.patientsCollection = queryFn
? this.firestore.collection<Patient>('patients', queryFn)
: this.firestore.collection<Patient>('patients');

this.patients = this.patientsCollection.snapshotChanges().pipe(
map(actions => actions.map( a =>{
let data = a.payload.doc.data() as Patient;
data.id = a.payload.doc.id;
return data;
}
))
);

return this.patients;
}

updatePatient(patient: Patient){
return this.firestore.doc("patients/"+patient.id).update({...patient});
}

deletePatient(patientId: string){
return this.firestore.doc("patients/" + patientId).delete();
}

// --------------------------------------------------------------------------

// ------------------------------ Medicine ----------------------------------

medicinesCollection: AngularFirestoreCollection<Medicine>;
medicines: Observable<Medicine[]>;

createMedicine(medicine: Medicine): Promise<DocumentReference>{
return this.firestore.collection('medicines').add({...medicine});
}

getMedicines(queryFn?: QueryFn){

this.medicinesCollection = queryFn
? this.firestore.collection<Medicine>('medicines', queryFn)
: this.firestore.collection<Medicine>('medicines');

this.medicines = this.medicinesCollection.snapshotChanges().pipe(
map(actions => actions.map( a =>{
let data = a.payload.doc.data() as Medicine;
data.id = a.payload.doc.id;
return data;
}
))
);

return this.medicines;
}

updateMedicine(medicine: Medicine){
return this.firestore.doc("medicines/"+medicine.id).update({...medicine});
}

deleteMedicine(medicineId: string){
return this.firestore.doc("medicines/" + medicineId).delete();
}

// --------------------------------------------------------------------------

// ------------------------------ Couriers ----------------------------------

couriersCollection: AngularFirestoreCollection<Courier>;
couriers: Observable<Courier[]>;

createCourier(courier: Courier): Promise<DocumentReference>{
return this.firestore.collection('couriers').add({...courier});
}

getCouriers(queryFn?: QueryFn){

this.couriersCollection = queryFn
? this.firestore.collection<Courier>('couriers', queryFn)
: this.firestore.collection<Courier>('couriers');

this.couriers = this.couriersCollection.snapshotChanges().pipe(
map(actions => actions.map( a =>{
let data = a.payload.doc.data() as Courier;
data.id = a.payload.doc.id;
return data;
}
))
);

return this.couriers;
}

updateCourier(courier: Courier){
return this.firestore.doc("couriers/"+courier.id).update({...courier});
}

deleteCourier(courierId: string){
return this.firestore.doc("couriers/" + courierId).delete();
}

// --------------------------------------------------------------------------

// ------------------------------ Orders ----------------------------------

ordersCollection: AngularFirestoreCollection<Order>;
orders: Observable<Order[]>;

createOrder(order: Order): Promise<DocumentReference>{
return this.firestore.collection('orders').add({...order});
}

getOrders(queryFn?: QueryFn){

this.ordersCollection = queryFn
? this.firestore.collection<Order>('orders', queryFn)
: this.firestore.collection<Order>('orders');

this.orders = this.ordersCollection.snapshotChanges().pipe(
map(actions => actions.map( a =>{
let data = a.payload.doc.data() as Order;
data.id = a.payload.doc.id;
return data;
}
))
);

return this.orders;
}

updateOrder(order: Order){
return this.firestore.doc("orders/"+order.id).update({...order});
}

deleteOrder(orderId: string){
return this.firestore.doc("orders/" + orderId).delete();
}

// --------------------------------------------------------------------------

}
43 changes: 42 additions & 1 deletion src/app/shared/services/medicine.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
import { Injectable } from '@angular/core';
import {AngularFirestore, AngularFirestoreCollection, DocumentReference, QueryFn} from "@angular/fire/firestore";
import {Medicine} from "../models/medicine.model";
import {Observable} from "rxjs";
import {map} from "rxjs/operators";

@Injectable({
providedIn: 'root'
})
export class MedicineService {

constructor() { }
constructor(private firestore: AngularFirestore) { }

// ------------------------------ Medicine ----------------------------------

medicinesCollection: AngularFirestoreCollection<Medicine>;
medicines: Observable<Medicine[]>;

createMedicine(medicine: Medicine): Promise<DocumentReference>{
return this.firestore.collection('medicines').add({...medicine});
}

getMedicines(queryFn?: QueryFn){

this.medicinesCollection = queryFn
? this.firestore.collection<Medicine>('medicines', queryFn)
: this.firestore.collection<Medicine>('medicines');

this.medicines = this.medicinesCollection.snapshotChanges().pipe(
map(actions => actions.map( a =>{
let data = a.payload.doc.data() as Medicine;
data.id = a.payload.doc.id;
return data;
}
))
);

return this.medicines;
}

updateMedicine(medicine: Medicine){
return this.firestore.doc("medicines/"+medicine.id).update({...medicine});
}

deleteMedicine(medicineId: string){
return this.firestore.doc("medicines/" + medicineId).delete();
}

// --------------------------------------------------------------------------
}
12 changes: 12 additions & 0 deletions src/app/shared/services/orders.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';

import { OrdersService} from "./orders.service";

describe('OrdersService', () => {
beforeEach(() => TestBed.configureTestingModule({}));

it('should be created', () => {
const service: OrdersService = TestBed.get(OrdersService);
expect(service).toBeTruthy();
});
});
Loading

0 comments on commit a4dae9a

Please sign in to comment.