-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongodbService.js
More file actions
107 lines (89 loc) · 2.94 KB
/
MongodbService.js
File metadata and controls
107 lines (89 loc) · 2.94 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
const { MongoClient, ObjectId } = require('mongodb');
const dbName = "dressRecommender";
const url = "mongodb://localhost:27017";
class MongodbService{
async init(){
const client = await MongoClient.connect(url);
this.db = client.db(dbName);
}
async getAllUsers(){
const allUsers = await this.db.collection('users').find({}).toArray();
return allUsers;
}
async getOneUser(userId){
const findUser = await this.db.collection('users').findOne({_id: ObjectId(userId)});
return findUser;
}
async getUserByEmail(email){
const findUser = await this.db.collection('users').findOne({email: email});
return findUser;
}
async addUser(user){
const newUser = await this.db.collection('users').findOneAndUpdate(
{email: user.email},
{$set: user},
{upsert: true});
}
async updateUser(userId, userObject){
console.log('updateUser userId >> ', userId)
console.log('updateUser userObject >> ', userObject)
const updatedUser = await this.db.collection('users').findOneAndUpdate(
{_id: ObjectId(userId)},
{$set: userObject},
{returnNewDocument: true})
return updatedUser;
}
async deleteUser(userId){
const removeUser =await this.db.collection('users').deleteOne({_id:ObjectId(userId)})
}
async getClothes(userId){
const findClothes = await this.db.collection('clothes').find({userId}).toArray();
return findClothes;
}
async getClothesByImage(image){
const findImage = await this.db.collection('clothes').findOne({image})
return findImage;
}
async getClothesByIds(clothesId){
const idAsObjectId = clothesId.map(_id =>ObjectId(_id));
const findClothes = await this.db.collection('clothes').find({_id: {$in: idAsObjectId}}).toArray();
return findClothes;
}
async addCloth(cloth){
const newCloth= await this.db.collection('clothes').findOneAndUpdate(
{image: cloth.image},
{$set:cloth},
{upsert:true});
}
async deleteCloth(clothId){
const removeCloth =await this.db.collection('clothes').deleteOne({_id:ObjectId(clothId)})
}
async getOutfits(userId){
const findOutfits= await this.db.collection('outfits').find({userId}).sort({_id: -1}).toArray();
return findOutfits;
}
async getOutfitsById(_id){
const findId = await this.db.collection('outfits').findOne({_id})
return findId;
}
async addOutfit(outfit){
const outfitId = outfit._id;
delete outfit._id;
const newOutfit= await this.db.collection('outfits').findOneAndUpdate(
{_id: ObjectId(outfitId)},
{$set:outfit},
{upsert:true}
)
}
async deleteOutfit(outfitId){
const removeOutfit =await this.db.collection('outfits').deleteOne({_id:ObjectId(outfitId)})
}
async getRecommendations(userId){
// Find current time
// Categorize current time with occasion
// Filter based on occassion
const findRecommendations= await this.db.collection('outfits').find({userId}).toArray();
return findRecommendations.filter(r => !r.lastWornDate);
}
}
module.exports = MongodbService;