forked from RochMoreau/secure-web-dev-workshop2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-data.js
More file actions
147 lines (124 loc) · 4.53 KB
/
import-data.js
File metadata and controls
147 lines (124 loc) · 4.53 KB
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
142
143
144
145
146
147
require('dotenv').config()
// module dotenv = env en mémoire, config() va chercher .env
//console.log(process.env)
const mongoose = require('mongoose')
const { Schema } = mongoose;
// on connecte a la bdd mongodb on recupere les credentials dans notre fichier .env
mongoose.connect(process.env.MONGO_URI).then(() => {console.log('Connected!')
getFilmingLocationById('2019-1611');
getFilmingLocationByFilmName('French Exit');
//deleteLocationById('32'); //test : ne marche pas ok
//deleteLocationById('633f19972cd2549596ba3922'); // ok !!
//addLocation('Horror','Laura MONGO','2018','Laura le retour','75020','2019-456','Roch Moreau','ESILV','2018-11-23');
//updateLocation('634d83c853fde20788d76a3d', 'filmName','Laura is back')
updateManyLocation('Australian exit','Exit');
getFilmingLocationByFilmName('Exit');
})
// .connect() est asynchrone
// on définit le schema mongoose
const filmSchema = new Schema({
filmType: String,
filmProducerName: String,
endDate: Date,
filmName: String,
district: String,
geolocation: {
coordinates: [
Number
],
type: String
},
sourceLocationId: String,
filmDirectorName: String,
address: String,
startDate: Date,
year: Number,
});
const Location = mongoose.model('Location',filmSchema)
//création du modele locations
//const maPremiereLocation = new Location({filmType:"Horror"})
//maPremiereLocation.save().then()
async function addFilmingLocation(filmList){
for (const elt of filmList) {
let film = new Location();
film.filmType = elt.fields.type_tournage;
film.filmProducerName = elt.fields.nom_producteur;
film.endDate = elt.fields.date_fin;
film.filmName = elt.fields.nom_tournage;
film.district = elt.fields.ardt_lieu;
film.coordinates = elt.fields.geo_point_2d;
film.type = elt.fields.geo_shape.type;
film.sourceLocationId = elt.fields.id_lieu;
film.filmDirectorName = elt.fields.nom_realisateur;
film.address = elt.fields.adresse_lieu;
film.startDate = elt.fields.date_debut;
film.year = elt.fields.annee_tournage;
await film.save()
}
}
const filmingLocations = require('../secure-web-dev-workshop1/lieux-de-tournage-a-paris.json')
//addFilmingLocation(filmingLocations);
//const maPremiereLocation = new Location({filmType:"Horror"})
//maPremiereLocation.save().then()
/** Question 9 :
* Write a function to query one Location by its ID
*/
async function getFilmingLocationById(id){
const query = await Location.findOne({'sourceLocationId':id})
console.log(query);
}
// on teste les fonctions plus haut dans le haut donnect pour par avoir de problème
// si la fonction s'éxécute alors que la ocnnexion n'est pas encore établie
/**Question 10 :
* Write a function to query all Locations for a given filmName
*/
async function getFilmingLocationByFilmName (filmName){
const query = await Location.find({'filmName':filmName});
console.log(query);
}
/**Question 11 :
* Write a function to delete a Location by its ID
*/
async function deleteLocationById(id){
try {
const query = await Location.deleteOne({'_id':id})
console.log(query);
} catch (error) {
//console.error(error);
console.error('ID does not exist');
}
}
/** Question 12:
* Write a function to add a Location
*/
function addLocation(filmType, filmProducerName,endDate,filmName, district, sourceLocationId,filmDirectorName,address,startDate,year){
const maPremiereLocation = new Location({filmType:filmType, filmProducerName:filmProducerName,endDate:endDate,filmName:filmName,district:district,sourceLocationId:sourceLocationId,filmDirectorName:filmDirectorName,address:address,startDate:startDate,year:year})
maPremiereLocation.save().then()
console.log('Location created');
}
/** Question 13 :
* Write a function to update a Location
*/
async function updateLocation(id, option, newValue){
try {
const loc = await Location.findById(id)
loc[option] = newValue;
await loc.save();
console.log('Updated')
}
catch (error){
console.error(error)
}
}
async function updateManyLocation( oldValue, newValue){
try {
const res = await Location.updateMany({ filmName: oldValue }, {filmName: newValue});
console.log('Updated')
console.log('matched count : ' + res.matchedCount)
console.log('modified count : ' + res.modifiedCount)
}
catch (error){
console.error(error)
}
}
/** FINISH */