Skip to content
31 changes: 31 additions & 0 deletions action/Action-Switcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const CheckStatusAction = require("./Check-Status-Action");
const StartFastingAction = require("./Start-Fasting-Action");
const StopFastingAction = require("./Stop-Fasting-Action");
const ListAllFastsAction = require("./List-All-Fasts-Action");
const UpdateFastingAction = require("./Update-Fasting-Action");

class ActionSwitcher {
constructor() {}

async startAction(menuObjects, action) {
switch (menuObjects[action - 1].handler) {
case "1":
await new CheckStatusAction().chackStatus();
break;
case "2":
await new StartFastingAction().startFastingAction();
break;
case "3":
await new StopFastingAction().stopFastingAction();
break;
case "4":
await new UpdateFastingAction().updateFastingAction();
break;
case "5":
await new ListAllFastsAction().listAllFastsAction();
break;
}
}
}

module.exports = ActionSwitcher;
46 changes: 46 additions & 0 deletions action/Check-Status-Action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const InputOutput = require("../input-output/Input-Output");
const FastFactory = require("../fast/Fast-Factory");
const DataService = require("../data/Data-Service");

class CheckStatusAction {
constructor() {}

async chackStatus() {
const res = await new FastFactory().createBulk();

res.forEach((fast) => {
let nowDateUTC = new Date().toISOString();

if (fast.end <= nowDateUTC) {
fast.active = false;

console.log("Your last fast has already ended. Here are the details:");
console.log("Start time: ", new Date(fast.start).toString());
console.log("End time: ", new Date(fast.end).toString());

console.log(
`Hours spent in fasting: ${
new Date(fast.end).getHours() - new Date(fast.start).getHours()
}`
);
console.log("Status: ", fast.active);
console.log("\n");

new DataService().write(res);
} else {
console.log("You have an ongoing fast:");
console.log("Start time: ", new Date(fast.start).toString());
console.log("End time: ", new Date(fast.end).toString());
console.log(
`Hours elapsed: ${
new Date(nowDateUTC).getHours() - new Date(fast.start).getHours()
}`
);
console.log("Status: ", fast.active);
console.log("\n");
}
});
}
}

module.exports = CheckStatusAction;
28 changes: 28 additions & 0 deletions action/List-All-Fasts-Action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const FastFactory = require("../fast/Fast-Factory");

class ListAllFastsAction {
constructor() {}

async listAllFastsAction() {
const fasts = await new FastFactory().createBulk();

console.log(
"Following is a detailed list of all fasts (past and present): \n"
);

if (fasts.length < 1)
return console.log("You haven't started fasting yet.");

fasts.forEach((fast) => {
console.log("Started at : ", new Date(fast.start).toString());
console.log(
fast.active ? "To end at: " : "Ended at: ",
new Date(fast.end).toString()
);
console.log("Duration of fast: ", fast.duration);
console.log("Status: ", fast.active ? "Active" : "Done", "\n");
});
}
}

module.exports = ListAllFastsAction;
35 changes: 35 additions & 0 deletions action/Start-Fasting-Action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const InputOutput = require("../input-output/Input-Output");
const FastFactory = require("../fast/Fast-Factory");
const DataService = require("../data/Data-Service");
const UserChoiceHandler = require("../user/User-Choice-Handler");

class StartFastingAction {
constructor() {}

async startFastingAction() {
const inputOutput = new InputOutput();
const duration = await inputOutput.question(
"Enter duration of fasting (i.e 12): "
);
inputOutput.close();

const start = new Date();
const end = new Date();
end.setTime(end.getTime() + duration * 60 * 60 * 1000);

const fasts = await new FastFactory().createBulk();

const newFast = {
start: start,
duration: duration,
end: end,
active: true,
};

fasts.push(newFast);

await new DataService().write(fasts);
}
}

module.exports = StartFastingAction;
38 changes: 38 additions & 0 deletions action/Stop-Fasting-Action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const InputOutput = require("../input-output/Input-Output");
const FastFactory = require("../fast/Fast-Factory");
const DataService = require('../data/Data-Service')
const Application = require('../application/Application')

class StopFastingAction {
constructor() {}

async stopFastingAction() {
const inputOutput = new InputOutput();

const answer = await inputOutput.question(
"Are you sure you want to stop your fast?\n1.Yes\n2.No\n"
);
inputOutput.close()
const fasts = await new FastFactory().createBulk();
if (answer == 1) {

fasts.forEach((fast) => {
if (fast.active) {
fast.active = false;
fast.end = new Date().toISOString();
fast.duration =
new Date(fast.end).getHours() - new Date(fast.start).getHours();
fast.duration = fast.duration.toString();
}
});

await new DataService().write(fasts)

} else {
console.log("Try again.\n")
}
// new Application()
}
}

module.exports = StopFastingAction;
92 changes: 92 additions & 0 deletions action/Update-Fasting-Action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const InputOutput = require("../input-output/Input-Output");
const FastFactory = require("../fast/Fast-Factory");
const DataService = require("../data/Data-Service");

class UpdateFastingAction {
constructor() {
this.months = [
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december",
];
this.fastChoice = ["1", "2", "3", "4"];
this.fastTypes = [16, 18, 20, 36];
}

async updateFastingAction() {
// Enter Month
const inputOutput = new InputOutput();
const month = await inputOutput.question(
"Enter the month of your fast: \n"
);

if (this.months.includes(month.toLowerCase())) {
// Enter Day of the month
const dayOfMonth = await inputOutput.question(
"Enter the day of month: \n"
);

if (dayOfMonth > 0 && dayOfMonth <= 31) {
// Enter the time when the fast has started (00:00 - 24:00):
const time = await inputOutput.question(
"Enter the time when the fast has started (00:00 - 24:00): \n"
);
if (time.includes(":")) {
const hours = time.split(":")[0];
const minutes = time.split(":")[1];
if (hours <= 24 && hours >= 0 && minutes <= 59 && minutes >= 0) {
console.log("Legit hours and minutes");

// Enter the duration of the fast (16, 18, 20, 36)
const durationChoice = await inputOutput.question(
"1. 16 hour fast\n2. 18 hour fast\n3. 20 hour fast\n4. 36 hour fast\n"
);

if (this.fastChoice.includes(durationChoice)) {
// Recalculate end date of the updated fast

const duration = this.fastTypes[durationChoice - 1];

const start = new Date(
`${month} ${dayOfMonth}, ${new Date().getFullYear()} ${hours}:${minutes}`
);
const end = new Date();
end.setTime(start.getTime() + duration * 60 * 60 * 1000);

const fasts = await new FastFactory().createBulk();

// Find last active fast and update it.
const fast = fasts.pop();

fast.start = start;
fast.end = end;
fast.duration = duration.toString();

fasts.push(fast);
await new DataService().write(fasts);

// console.log("Your current fast has been updated: ", fast)
console.log("Your fast has been updated successfully.");
}
}
} else {
console.log("Invalid input. Start again.");
}
} else {
console.log("Invalid input. Start again.");
}
} else console.log("Invalid input. Start again.");
inputOutput.close();
}
}

module.exports = UpdateFastingAction;
10 changes: 10 additions & 0 deletions application/Application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const UserChoiceHandler = require("../user/User-Choice-Handler");

class Application {
constructor() {
new UserChoiceHandler().process()
}
}


module.exports = Application
1 change: 1 addition & 0 deletions core/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"start":"2022-03-29T14:41:33.433Z","duration":"2","end":"2022-03-29T16:41:33.433Z","active":false},{"start":"2022-04-11T11:43:35.562Z","duration":"0","end":"2022-04-11T11:44:08.691Z","active":false},{"start":"2022-04-11T11:44:00.000Z","duration":"36","end":"2022-04-12T23:44:00.000Z","active":true}]
22 changes: 22 additions & 0 deletions core/menu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"name": "Check Status\n",
"handler": "1"
},
{
"name": "Start Fasting\n",
"handler": "2"
},
{
"name": "Stop Fasting\n",
"handler": "3"
},
{
"name": "Update Fasting\n",
"handler": "4"
},
{
"name": "List all Fasts\n",
"handler": "5"
}
]
25 changes: 25 additions & 0 deletions data/Data-Service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require("fs").promises;

class DataService {
constructor() {}

async read(filename) {
try {
const res = await fs.readFile(`core/${filename}.json`, "utf-8");
return JSON.parse(res);
} catch (error) {
console.log("Data-Service -> read(): ", error);
}
}

async write(newFasts) {
try {
const res = await fs.writeFile("core/db.json", JSON.stringify(newFasts));
if (res) console.log("Successfully Written to File.\n");
} catch (error) {
console.err("Data-Service => write(newFasts): ", error);
}
}
}

module.exports = DataService;
25 changes: 25 additions & 0 deletions fast/Fast-Factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Fast = require("./Fast");
const DataService = require("../data/Data-Service");

class FastFactory {
constructor() {}

create(start, duration, end, active) {
return new Fast(start, duration, end, active);
}

async createBulk() {
try {
const res = await new DataService().read("db");
return res.map((fast) => {
const { start, duration, end, active } = fast;
return this.create(start, duration, end, active);
});
} catch (error) {
console.error("Fast-Factory => createBulk(): ", error);
return [];
}
}
}

module.exports = FastFactory;
10 changes: 10 additions & 0 deletions fast/Fast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Fast {
constructor(start, duration, end, active) {
this.start = start;
this.duration = duration;
this.end = end;
this.active = active;
}
}

module.exports = Fast;
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const UserChoiceHandler = require("./user/User-Choice-Handler");
const Application = require('./application/Application');

(async () => {

await new UserChoiceHandler().process();

})();
Loading