-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (111 loc) · 3.22 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
const express = require('express')
const app = express()
const cors = require('cors')
const mongoose=require("mongoose")
const bodyParser=require("body-parser")
require('dotenv').config()
///Configuracion
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
///Obtengo fecha actual
const nDate = () => new Date().toDateString()
///Conexion a MongoDb y definicion de esquemas y modelos
mongoose.connect(process.env['URL'], { useNewUrlParser: true, useUnifiedTopology: true })
const exerciseSchema=new mongoose.Schema({
description: String,
duration: Number,
date: {
type:String
}
})
const userSchema= new mongoose.Schema({
username:{
type:String,
required:true
},
log: [exerciseSchema ]
})
const User=mongoose.model("User",userSchema)
const Exercise=mongoose.model("Exercise",exerciseSchema)
///Manejadores de rutas
//Muestra todos los usuarios
app.get("/api/users",function(req,res){
User.find({},function(err,users){
res.json(users)
})
})
//Muestra los registros de un determinado usuario , pudiendo filtrar por fecha y cantidad de registros
app.get("/api/users/:_id/logs?",function(req,res){
const {_id}=req.params
const from=req.query.from || "1900-01-01"
const to=req.query.to || "3000-12-31"
const limit=req.query.limit || 1000
User.findById(_id,async function(err,user){
const filteredUser=user.log.filter((exercise,index) => (
(new Date(exercise.date).getTime()>=new Date(from).getTime())
&& (new Date(exercise.date).getTime()<=new Date(to).getTime())
)
).slice(0,limit)
let count=filteredUser.length
res.json({
username: user.username,
count: count,
_id: user._id,
log: filteredUser
})
})
})
//Guarda un nuevo usuario y devuelve la informacion
app.post("/api/users",async function(req,res){
const username=req.body.username
const instance = new User({
username: username
})
await instance.save(function(err,data){
res.json({
username: data.username ,
_id: data._id
})
})
})
//Guarda un registro de ejercicio, fecha es opcional
app.post("/api/users/:_id/exercises", async function(req,res){
const userId=req.params._id
const description=req.body.description
const duration=req.body.duration
const regex=/^\d{4}\D\d{2}\D\d{2}$/
let date
if(req.body.date===""|| !regex.test(req.body.date)){
date=nDate()
console.log("vacio")
} else{
date= new Date(req.body.date).toDateString()
}
const instance= new Exercise({
description: description,
duration: duration,
date: date
})
await instance.save(function(err){
User.findById(userId,(err,user)=>{
user.log.push(instance)
user.save()
res.json({
username: user.username,
description: instance.description,
duration: instance.duration,
date: instance.date,
_id: user._id
})
})
})
})
//Configuracion de puerto
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})