-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectfile5.js
129 lines (107 loc) · 4.61 KB
/
projectfile5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// varying userids and getting the values by using search params
var http = require("http");
//var url = require("url");
const { URL } = require("url");
const URLSearchParams= require("url");
var fs = require("fs");
var readline=require("readline-sync");
//var parsedUrl = url.parse('/itemsAvailable?model=variable1&userId=variable2',true);
//href: '/itemsAvailable?model=nokia',
//search: '?model=nokia',
//query: {model: 'nokia'},
//pathname: '/itemsAvailable'
var url_string = "http://www.example.com?model=variable1&userId=variable2";
var url = new URL (url_string);
var phone = url.searchParams.get("model");
var valueOfUser = url.searchParams.get("userId");
console.log(phone);
var d = new Date();
var date= d.getDate();
var hour = d.getHours();
var min = d.getMinutes();
var itemsBought =0;
var month = d.getMonth()-1;
var arr=['jan','feb','mar','may','june','july','aug','sept','oct','nov','dec'];
var value1=arr[month]+String(date);
console.log(value1);
//var c = parsedUrl.searchParams.get("userId");
//phone = readline.question('which one do u chose?');
//console.log(phone);
//if(parsedUrl.query.model=='nokia'){console.log("you have chosen nokia");};
//if(parsedUrl.query.model=='samsung'){console.log("you have chosen samsung");};
//valueOfUser=readline.question("USER ID: ");
//var phone = parsedUrl.query.model;
//var valueOfUser = parsedUrl.query.userId;
var value1=arr[month]+String(date);
var itemsAvailable =[
{
model : 'nokia',
available : 10
},
{
model: 'samsung',
available: 20
}
];//create an object to store the itemsAvailable
var nokia1 = itemsAvailable[0].available;
var samsung1 = itemsAvailable[1].available;
function reset(){
var value ='userid'+String(valueOfUser); // check if user file exits
fs.readFile(value,'utf8',function(err,data){
var value1=arr[month]+String(date);
if(err) { // if there is an error in reading file
console.log("error cant read"); //i.e if the file does not exist
var Obj={[value1]:{"model":[phone],"count":0,"userid":[valueOfUser],"itemsLeftofNokia":[nokia1],"itemsLeftofSamsung":[samsung1]}}; // create
var p = JSON.stringify(Obj); // a new file with name userid of that person
console.log(p);
fs.closeSync(fs.openSync(value,'w'));
fs.writeFile(value,p,'utf8',function(err,data){if(err)console.log("error writing data")});
}
else{
var Objr = JSON.parse(data); // if the file already exists check if todaysdateproperty exists
console.log(Objr);
var value1=arr[month]+String(date);
var value2= arr[month]+String(date-1);
if(Objr[value1]==undefined){ // check if object property for todays date is defined
console.log("date doesnot exist");
var j = Objr[value2].itemsLeftofNokia;
var l = Objr[value2].itemsLeftofSamsung;
Objr[value1]= {"model":[phone],"count":0,"userid":[valueOfUser],"itemsLeftofNokia":[j],"itemsLeftofSamsung":[l]}};
// if not defined include todaysdate property
console.log(Objr);
var w = JSON.stringify(Objr);
fs.writeFile(value,w,'utf8',function(err,data){if(err) console.log("cant write")});}// after adding it to the object write into file
});}
reset();
http.createServer(function(req,res){
//if(phone=='nokia'){nokia1--;}
//else if(phone=='samsung'){samsung1--;}
var value1=arr[month]+String(date);
var value ='userid'+String(valueOfUser);
fs.readFile(value,'utf8',function(err,data){
if(err) console.log("cannot read file");
else
{
var Obj2=JSON.parse(data);
if(Obj2[value1].count==0)
{
res.write("u can buy the item"); // check if count value=0
Obj2[value1].count=1; //if yes change it to 1
Obj2[value1].model=phone;
console.log("true");
if(phone=='nokia'){Obj2[value1].itemsLeftofNokia--;} // if chosen nokia decrement the nokia count
else if(phone=='samsung'){Obj2[value1].itemsLeftofSamsung--;} //similarly for samsung
var n = Obj2[value1].itemsLeftofNokia; // create a variable whose value equals total no of items bought
var m=Obj2[value1].itemsLeftofSamsung;
var value3 =arr[month]+String(date+1); // create tomorrows date and append it to the existing userid object file
Obj2[value3]={"model":[phone],'count':0,'userId':[valueOfUser],"itemsLeftofNokia":[n],"itemsLeftofSamsung":[m]};
console.log(Obj2);
console.log(Obj2[value3].model);
var t=JSON.stringify(Obj2);
fs.writeFile(value,t,'utf8',function(err,data){if(err){console.log("cant write");}})
}
else {res.write("u cannot buy the item");}
}
res.end();
})
}).listen(3000);