Skip to content

Commit

Permalink
add read records
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzyfhuba committed Jun 9, 2022
1 parent aea3580 commit c2863e6
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/app/controllers/DataController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const DataController = {
const { query } = request;
// need id of user

const records = await Record.getAllById(query.id);
const savings = await Saving.getAllById(query.id);
const bills = await Bill.getAllById(query.id);
const saving_records = await SavingRecord.getAllById(query.id);
const records = await Record.getAllByUserId(query.id);
const savings = await Saving.getAllByUserId(query.id);
const bills = await Bill.getAllByUserId(query.id);
const saving_records = await SavingRecord.getAllByUserId(query.id);

// decode json saving save
const saving_records_decode = saving_records.map(saving_record => {
Expand Down
64 changes: 64 additions & 0 deletions src/app/controllers/RecordController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const Record = require("../models/Record");

const RecordController = {
whereAll: async (request, h) => {
const { query } = request;
// need id of user

const records = await Record.getAllByUserId(query.id);
const savings = await Saving.getAllByUserId(query.id);
const bills = await Bill.getAllByUserId(query.id);
const saving_records = await SavingRecord.getAllByUserId(query.id);

// decode json saving save
const saving_records_decode = saving_records.map(saving_record => {
return JSON.parse(saving_record.save);
});

return h.response({
error: false,
statusCode: 200,
message: 'Success',
data: {
records: records,
savings: savings,
saving_record: saving_records_decode,
bills: bills
}
}).code(200);
},

whereIncome: async (request, h) => {
const { query } = request;
// need id of user

const records = await Record.getIncome(query.id);

return h.response({
error: false,
statusCode: 200,
message: 'Success',
data: {
records: records
}
}).code(200);
},

whereExpense: async (request, h) => {
const { query } = request;
// need id of user

const records = await Record.getExpense(query.id);

return h.response({
error: false,
statusCode: 200,
message: 'Success',
data: {
records: records
}
}).code(200);
}
};

module.exports = RecordController;
2 changes: 1 addition & 1 deletion src/app/database/seeder/SavingPlanRecordSeeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports.seed = async function(knex) {
for (let j = 0; j < 5; j++) {
const user = users[i];

const savings = await Saving.getAllById(user.id);
const savings = await Saving.getAllByUserId(user.id);

const save = [];
for (let k = 0; k < Math.floor(Math.random() * 10) + 1; k++) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/models/Bill.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const connection = require('../config/database');

class Bill{
static async getAllById(id){
static async getAllByUserId(id){
const Bills = await connection.promise().query(
'SELECT * FROM bills WHERE user_id = ?',
[id]
Expand Down
File renamed without changes.
Empty file.
34 changes: 32 additions & 2 deletions src/app/models/Record.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
const connection = require('../config/database');

class Record{
static async getAllById(id){
static async getAllByUserId(user_id){
const records = await connection.promise().query(
'SELECT * FROM records WHERE user_id = ?',
[id]
[user_id]
);
return records[0];
}

static async getIncome(user_id) {
const incomes = await connection.promise().query(
'SELECT * FROM records WHERE type = ? AND user_id = ?', ['income', user_id]
).catch(err => console.log(err));
return incomes[0];
}

static async getExpense(user_id) {
const expenses = await connection.promise().query(
'SELECT * FROM records WHERE type = ? AND user_id = ?', ['expense', user_id]
);
return expenses[0];
}

static async storeIncome({user_id, amount, note, category}){
const record = await connection.promise().query(
'INSERT INTO records (user_id, amount, note, category, type) VALUES (?, ?, ?, ?, ?)',
[user_id, amount, note, category, 'income']
);
return record;
}

static async storeExpense({user_id, amount, note, category}){
const record = await connection.promise().query(
'INSERT INTO records (user_id, amount, note, category, type) VALUES (?, ?, ?, ?, ?)',
[user_id, amount, note, category, 'expense']
);
return record;
}
}

module.exports = Record;
2 changes: 1 addition & 1 deletion src/app/models/Saving.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const connection = require('../config/database');

class Saving{
static async getAllById(id){
static async getAllByUserId(id){
const savings = await connection.promise().query(
'SELECT * FROM saving_plans WHERE user_id = ?',
[id]
Expand Down
2 changes: 1 addition & 1 deletion src/app/models/SavingRecord.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const connection = require('../config/database');

class SavingRecord{
static async getAllById(id){
static async getAllByUserId(id){
const saving_records = await connection.promise().query(
'SELECT * FROM saving_records WHERE user_id = ?',
[id]
Expand Down
11 changes: 11 additions & 0 deletions src/app/routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Authentication = require('./controllers/Authentication');
const DataController = require('./controllers/DataController');
const RecordController = require('./controllers/RecordController');

const routes = [
{
Expand Down Expand Up @@ -47,6 +48,16 @@ const routes = [
path: '/api/getalldata',
handler: DataController.whereAll,
},
{
method: 'GET',
path: '/api/getincome',
handler: RecordController.whereIncome,
},
{
method: 'GET',
path: '/api/getexpense',
handler: RecordController.whereExpense,
},
];

module.exports = routes;

0 comments on commit c2863e6

Please sign in to comment.