Created
December 27, 2015 00:43
-
-
Save justinramel/8d8f0790ede0dd605ed4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {CustomStorage} from '../../storage/custom-storage'; | |
import * as _ from 'lodash'; | |
export class RatesService { | |
constructor() { | |
this.storage = new CustomStorage(); | |
this.createRatesTableIfItDoesNotExist(); | |
} | |
all() { | |
return this.storage.query('select * from rates order by id asc').then(result => { | |
return _.map(result.rows, row => { | |
return this.create({ | |
id: row.id, | |
createdDate: new Date(row.createdDate), | |
direction: row.direction, | |
description: row.description, | |
amount: row.amount, | |
days: row.days | |
}); | |
}); | |
}).catch(error => { | |
console.log(error); | |
}); | |
} | |
calculateDailyRate() { | |
return this.all().then(all => { | |
let totalIn = _.chain(all).filter({direction: 'In'}).map('dailyRate').sum().value(); | |
let totalOut = _.chain(all).filter({direction: 'Out'}).map('dailyRate').sum().value(); | |
return totalIn - totalOut; | |
}); | |
} | |
create(options = {}) { | |
let Rate = { | |
id: null, | |
createdDate: new Date(), | |
direction: 'In', | |
description: '', | |
amount: 0, | |
days: 30, | |
get dailyRate() { | |
return parseFloat(this.amount) / parseInt(this.days); | |
} | |
}; | |
let rate = Object.create(Rate); | |
return Object.assign(rate, options); | |
} | |
save(rate) { | |
if (rate.id) { | |
return this.update(rate); | |
} else { | |
return this.insert(rate); | |
} | |
} | |
insert(rate) { | |
return this.storage.query(`insert into rates( | |
createdDate, | |
direction, | |
description, | |
amount, | |
days, | |
dailyRate | |
) values (?, ?, ?, ?, ?, ?)`, | |
[ | |
rate.createdDate.getTime(), | |
rate.direction, | |
rate.description, | |
parseFloat(rate.amount), | |
parseInt(rate.days), | |
rate.dailyRate | |
]).then(result => { | |
rate.id = result.insertId; | |
return rate; | |
}).catch(error => { | |
console.error('Error inserting rate', error); | |
}); | |
} | |
update(rate) { | |
return this.storage.query(`update rates set | |
createdDate=?, | |
direction=?, | |
description=?, | |
amount=?, | |
days=?, | |
dailyRate=? | |
where id=?`, | |
[ | |
rate.createdDate.getTime(), | |
rate.direction, | |
rate.description, | |
parseFloat(rate.amount), | |
parseInt(rate.days), | |
rate.dailyRate, | |
rate.id | |
]).then(result => { | |
return rate; | |
}).catch(error => { | |
console.error('Error updating rate', error); | |
}); | |
} | |
delete(rate) { | |
return this.storage.query('delete from rates where id=?', [rate.id]).then(result => { | |
return rate; | |
}).catch(error => { | |
console.error('Error deleting rate', error); | |
}); | |
} | |
createRatesTableIfItDoesNotExist() { | |
const createSql = `create table if not exists rates( | |
id integer primary key autoincrement, | |
createdDate long, | |
direction text, | |
description text, | |
amount real, | |
days integer, | |
dailyRate real | |
)`; | |
this.storage.query(createSql).catch(error => { | |
console.error('Error creating rates table', error); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment