Skip to content

Commit

Permalink
CRUDs #6
Browse files Browse the repository at this point in the history
Patients
Medicines
Couriers
Orders
  • Loading branch information
djsanabriac committed Apr 2, 2020
1 parent 849d0cd commit c00c098
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/app/shared/models/courier.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Deserializable} from "./deserializable.model";

export class Medicine implements Deserializable{
export class Courier implements Deserializable{

id: string;
name: string;
idNumber: string;
plate: string;
Expand Down
2 changes: 2 additions & 0 deletions src/app/shared/models/medicine.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {Deserializable} from "./deserializable.model";

export class Medicine implements Deserializable{

id: string;
name:string; // upper case
packaging: string;
invima:string; // optional
Expand Down
3 changes: 2 additions & 1 deletion src/app/shared/models/order.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Deserializable} from "./deserializable.model";

export class Medicine implements Deserializable{
export class Order implements Deserializable{

id: string;
orderId: bigint;
patientIdNumber:string;
patientName:string;
Expand Down
1 change: 1 addition & 0 deletions src/app/shared/models/patient.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Deserializable} from "./deserializable.model";

export class Patient implements Deserializable{

id: string;
name: string;
idNumber: string;
birth_date: Date; // < today
Expand Down
200 changes: 187 additions & 13 deletions src/app/shared/services/firebase.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { Injectable } from '@angular/core';
import {AngularFirestore, DocumentReference, QueryFn} from "@angular/fire/firestore";
import {
AngularFirestore,
AngularFirestoreCollection,
AngularFirestoreDocument,
DocumentReference,
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'
Expand All @@ -12,6 +24,8 @@ export class FirebaseService {

constructor(private firestore: AngularFirestore) {

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

let patient = new Patient();
patient.name = "name";
patient.idNumber = "123456";
Expand All @@ -24,9 +38,39 @@ export class FirebaseService {
.then(value => console.debug("Created"))
.catch(reason => console.error("[error]"));*/

this.getPatient(ref => ref.where("idNumber","==","123456"))
.then(value => {console.debug(value);} )
.catch(reason => {console.error(reason);});
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(){
Expand All @@ -37,21 +81,151 @@ export class FirebaseService {
}

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

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

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

getPatient(queryFn: QueryFn): Promise<Patient[]>{
getPatients(queryFn?: QueryFn){

this.firestore.collection<Patient>('patients', queryFn).valueChanges().toPromise()
.then(value => {
console.log("[CUSTOM SUCCESS]", value);
})
.catch(reason => {
console.log("[CUSTOM ERROR]", reason)
});
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){

return this.firestore.collection<Patient>('patients', queryFn).valueChanges().toPromise();
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();
}

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

}

0 comments on commit c00c098

Please sign in to comment.