This commit is contained in:
N0rdye 2023-03-24 00:32:57 +05:00
parent f7ded6fa57
commit 8b93449924
7 changed files with 148 additions and 61 deletions

142
index.js
View File

@ -5,75 +5,127 @@ const {
} = require('express'); } = require('express');
const app = express(); const app = express();
const today = new Date()
const day = today.getDate();
const month = today.getMonth()+1;
const year = today.getFullYear();
const { json } = require("body-parser");
const { pathToFileURL } = require("url");
app.set('view engine','ejs'); const {
app.use(express.urlencoded({extended: false})); json
} = require("body-parser");
const {
pathToFileURL
} = require("url");
app.set('view engine', 'ejs');
app.use(express.urlencoded({
extended: false
}));
app.use(express.static('public')); app.use(express.static('public'));
const fs = require('fs'); const fs = require('fs');
let today = new Date()
today.setHours(today.getHours() + 5);
app.get('/user', (req,res) => { setInterval(function() {
let timeu = new Date();
timeu.setHours(timeu.getHours() + 5);
const last = today;
let sec = today.getDate();
let secu = timeu.getDate();
// console.log(sec, secu);
if (sec != secu) {
let pill = `[0]`;
let comment = "";
fs.readFile('./pills.json', 'utf8', (err, data) => {
let jdata = JSON.parse(data);
let num = j_max(jdata) + 1;
let name = `pill_${num}`;
let c_date = get_date(last);
jwrite_pill(name, pill, c_date, comment);
})
console.log(pill);
today = timeu;
console.log(today, timeu, last);
}
}, 1000);
function get_date(time) {
// today.setHours(today.getHours() + 5);
let day = time.getDate();
let month = time.getMonth() + 1;
let year = time.getFullYear();
// console.log(day.toString().length);
// console.log(month.toString().length);
day = (day.toString().length <= 1) ? `0${day}` : day;
month = (month.toString().length <= 1) ? `0${month}` : month;
return `${day}.${month}.${year}`;
}
app.get('/user', (req, res) => {
fs.readFile('./pills.json', 'utf8', (err, data) => { fs.readFile('./pills.json', 'utf8', (err, data) => {
let jdata = JSON.parse(data); let jdata = JSON.parse(data);
res.render(`user`, { data: dict_reverse(jdata)}); res.render(`user`, {
data: dict_reverse(jdata)
});
}) })
}) })
app.get('/', (req,res) => { app.get('/', (req, res) => {
res.redirect("/user"); res.redirect("/user");
}) })
console.log(get_date(today));
function dict_reverse(obj) { function dict_reverse(obj) {
new_obj= {} new_obj = {}
rev_obj = Object.keys(obj).reverse(); rev_obj = Object.keys(obj).reverse();
rev_obj.forEach(function(i) { rev_obj.forEach(function(i) {
new_obj[i] = obj[i]; new_obj[i] = obj[i];
}) })
return new_obj; return new_obj;
} }
app.post('/scripts',(req,res) => { app.post('/scripts', (req, res) => {
let inp = req.body; let inp = req.body;
console.log(inp); console.log(inp);
if(inp["func"]=="save"){ if (inp["func"] == "save") {
let pill = `[${inp["pill"]}]`; let pill = `[${inp["pill"]}]`;
let comment = inp["comment"]; let comment = inp["comment"];
fs.readFile('./pills.json','utf8',(err,data)=>{ fs.readFile('./pills.json', 'utf8', (err, data) => {
let jdata = JSON.parse(data); let jdata = JSON.parse(data);
let num = j_max(jdata)+1; let num = j_max(jdata) + 1;
let name = `pill_${num}`; let name = `pill_${num}`;
let c_date = `${day}.${month}.${year}`; let c_date = get_date(today);
jwrite_pill(name,pill,c_date,comment); jwrite_pill(name, pill, c_date, comment);
}) })
console.log(pill); console.log(pill);
} } else if (inp["func"] == "del") {
else if(inp["func"]=="del"){
let pill = inp["pill"]; let pill = inp["pill"];
fs.readFile('./pills.json','utf8',(err,data)=>{ fs.readFile('./pills.json', 'utf8', (err, data) => {
let jdata = JSON.parse(data); let jdata = JSON.parse(data);
delete jdata[pill]; delete jdata[pill];
jwrite(jdata); jwrite(jdata);
}) })
} } else if (inp["func"] == "edit" && inp["mode"] == "save") {
else if(inp["func"]=="edit"&&inp["mode"]=="save"){
let name = inp["name"]; let name = inp["name"];
let date = inp["date"]; let date = inp["date"];
let dates = date.toString().split("-");
date = `${dates[2]}.${dates[1]}.${dates[0]}`;
let comment = inp["comment"]; let comment = inp["comment"];
let pill = inp["pill"];
console.log("ecdit"); let pill = `[${inp["pill"]}]`;
jwrite_pill(name,pill,date,comment); console.log("edit");
jwrite_pill(name, pill, date, comment);
} }
res.redirect("/user"); res.redirect("/user");
}) })
function jwrite(jdata){ function jwrite(jdata) {
// console.log(jdata); // console.log(jdata);
let wdata = JSON.stringify(jdata); let wdata = JSON.stringify(jdata);
fs.writeFile('./pills.json', wdata, 'utf8', err => { fs.writeFile('./pills.json', wdata, 'utf8', err => {
@ -87,15 +139,15 @@ function jwrite(jdata){
function jwrite_pill(name,pill,date,comment){ function jwrite_pill(name, pill, date, comment) {
comment = (comment==""||comment==null||comment==" "||comment==undefined||comment==" ")? "-":comment; comment = (comment == "" || comment == null || comment == " " || comment == undefined || comment == " ") ? "-" : comment;
// console.log(comment,"||"); // console.log(comment,"||");
fs.readFile('./pills.json','utf8',(err,data)=>{ fs.readFile('./pills.json', 'utf8', (err, data) => {
let jdata = JSON.parse(data); let jdata = JSON.parse(data);
jdata[name] = { jdata[name] = {
"date":date, "date": date,
"pill":pill, "pill": pill,
"comment":comment "comment": comment
}; };
// console.log(jdata); // console.log(jdata);
let wdata = JSON.stringify(jdata); let wdata = JSON.stringify(jdata);
@ -109,22 +161,30 @@ function jwrite_pill(name,pill,date,comment){
}) })
} }
app.post("/edit",(req,res) =>{ app.post("/edit", (req, res) => {
let inp = req.body; let inp = req.body;
let name = inp["name"]; let name = inp["name"];
let pill = inp["pill"]; let pill = inp["pill"].replace('[', '').replace(']', "");
let date = inp["date"]; let date = inp["date"];
let comment = inp["comment"]; let comment = inp["comment"];
console.log("e"); comment = (comment == "-") ? "" : comment;
res.render(`edit`, { name:name,pill:pill,comment:comment,date:date});
console.log("edit");
res.render(`edit`, {
name: name,
pill: pill,
comment: comment,
date: date
});
}) })
function j_max(arg){ function j_max(arg) {
let max = 0; let max = 0;
for (const key in arg) { for (const key in arg) {
let num = parseInt(key.split("_")[parseInt(key.split("_").length-1)]); let num = parseInt(key.split("_")[parseInt(key.split("_").length - 1)]);
// console.log(key); // console.log(key);
max = (num>max)? num:max; max = (num > max) ? num : max;
} }
console.log(max); console.log(max);
return max; return max;

0
package-lock.json generated Executable file → Normal file
View File

0
package.json Executable file → Normal file
View File

File diff suppressed because one or more lines are too long

0
sacrap/index1.js Executable file → Normal file
View File

View File

@ -1,13 +1,35 @@
<style> <style>
*{ * {
text-align: center; text-align: center;
} }
</style> </style>
<form action="/scripts" method="post"> <form action="/scripts" method="post">
<%
let dates = date.split(".");
let date2 = `${dates[2]}-${dates[1]}-${dates[0]}`;
console.log(date2);
%>
<input type="hidden" name="name" value="<%= name %>"> <input type="hidden" name="name" value="<%= name %>">
<input type="text" name="pill" value="<%= pill %>"> <input type="date" name="date" value="<%= date2 %>">
<input type="text" name="date" value="<%= date %>"> <input type="text" name="comment" value="<%= comment %>" placeholder="comment">
<input type="text" name="comment" value="<%= comment %>"> <!-- <input type="text" name="pill" value="<%= pill %>"> -->
<select id="pills" name="pill">
<!-- <option value="4/2">4/2</option>
<option value="4">4</option>
<option value="2">2</option>
<option value="0">0</option> -->
<%
let pills = ["4/2","4/1","4","2","2/1","1","0"];
let i=0;
pills.forEach(el => {
let check = (el==pill)? "selected":"";
%>
<option class="pill" id="pill-<%= i %>" value="<%= el %>" <%= check %>><%= el %></option>
<%
i++;
});
%>
</select>
<input type="hidden" name="func" value="edit"> <input type="hidden" name="func" value="edit">
<input type="submit" name="mode" value="save"> <input type="submit" name="mode" value="save">
<input type="submit" name="mode" value="cancel"> <input type="submit" name="mode" value="cancel">

View File

@ -1,45 +1,50 @@
<style> <style>
.pill{ .pill {
border: 1px black solid; border: 1px black solid;
display: flex; display: flex;
padding-inline: 3px; padding-inline: 3px;
} }
.pill input{
.pill input {
margin: 0px; margin: 0px;
/* padding: 20px; */ /* padding: 20px; */
} }
.pill label{
.pill label {
padding-inline: 4px; padding-inline: 4px;
padding-block: 1px; padding-block: 1px;
} }
.pills{
.pills {
list-style-type: none; list-style-type: none;
display: flex; display: flex;
} }
.f1{
.f1 {
display: flex; display: flex;
justify-content: space-evenly; justify-content: space-evenly;
margin-block: 15px; margin-block: 15px;
} }
input{
input {
text-align: center; text-align: center;
} }
</style> </style>
<form class="f1" action="/scripts" method="post"> <form class="f1" action="/scripts" method="post">
<input type="submit" value="add"> <input type="submit" value="add">
<ul class="pills"> <select id="pills" name="pill">
<% <%
let pills = ["4/2","4/1","4","2","2/1","1","0"]; let pills = ["4/2","4/1","4","2","2/1","1","0"];
let i=0; let i=0;
pills.forEach(el => { pills.forEach(el => {
let check = (el=="4")? "checked":""; let check = (el=="4")? "selected":"";
%> %>
<li class="pill"><label for="<%= i %>"><%= el %></label> <input id="<%= i %>" type="radio" name="pill" value="<%= el %>" <%= check %>></li> <option class="pill" id="pill-<%= i %>" value="<%= el %>" <%= check %>><%= el %></option>
<% <%
i++; i++;
}); });
%> %>
</ul> </select>
<input type="text" name="comment" value=" " placeholder="comment"> <input type="text" name="comment" value="" placeholder="comment">
<input type="hidden" name="func" value="save"> <input type="hidden" name="func" value="save">
</form> </form>