Parcourir la source

Worked on ratings.

Kris il y a 8 ans
Parent
commit
1a70dd1d51
4 fichiers modifiés avec 56 ajouts et 5 suppressions
  1. 2 1
      backend/app.js
  2. 39 0
      backend/logic/coreHandler.js
  3. 3 1
      backend/schemas/user.js
  4. 12 3
      frontend/App.vue

+ 2 - 1
backend/app.js

@@ -50,7 +50,8 @@ function setupExpress() {
 
 	global.db = {
 		user: require('./schemas/user')(mongoose),
-		station: require('./schemas/station')(mongoose)
+		station: require('./schemas/station')(mongoose),
+		song: require('./schemas/song')(mongoose)
 	};
 
 		const mongoStore = new MongoStore({'mongooseConnection': MongoDB});

+ 39 - 0
backend/logic/coreHandler.js

@@ -195,5 +195,44 @@ module.exports = {
 				}
 			}
 		});
+	},
+
+	'/songs/:id/toggleLike': (songId, userId, cb) => {
+
+		var user = global.db.user.findOne(userId);
+		var song = global.db.song.findOne(songId);
+		if (user !== undefined) {
+			if (song !== undefined) {
+				var liked = false;
+				if (song.likes.indexOf(userId) === -1) {
+					liked = true;
+					// Add like
+				} else {
+					// Remove like
+				}
+				if (song.dislikes.indexOf(userId) !== -1) {
+					// Remove dislike
+				}
+				// Emit to all sockets with this user that their likes/dislikes updated.
+				// Emit to all sockets in the room that the likes/dislikes has updated
+				cb({liked: liked, disliked: false});
+			} else {
+				cb({err: "Song not found."});
+			}
+		} else {
+			cb({err: "User not found."});
+		}
+
+	},
+
+	'/user/ratings': (userId, cb) => {
+
+		var user = global.db.user.findOne(userId);
+		if (user !== undefined) {
+			cb({likes: user.likes, dislikes: user.dislikes});
+		} else {
+			cb({err: "User not found."});
+		}
+
 	}
 };

+ 3 - 1
backend/schemas/user.js

@@ -37,7 +37,9 @@ module.exports = mongoose => {
             songsAccepted: { type: Number, default: 0 }
         },
         createdAt: { type: Date, default: Date.now() },
-		friends: []
+		friends: [],
+		likes: [],
+		dislikes: []
     });
 
     return mongoose.model('user', userSchema);

+ 12 - 3
frontend/App.vue

@@ -24,6 +24,8 @@
 					email: "",
 					password: ""
 				},
+				likes: [],
+				dislikes: [],
 				loggedIn: true,
 				groups: [
 					{
@@ -63,9 +65,16 @@
 			}
 		},
 		ready: function () {
-			this.socket = io();
-			this.socket.on("ready", function(status) {
-				this.loggedIn = status;
+			var local = this;
+			local.socket = io();
+			local.socket.on("ready", function(status) {
+				local.loggedIn = status;
+				local.socket.emit("/user/ratings", function(result) {
+					if (!result.err) {
+						local.likes = result.likes;
+						local.dislikes = result.dislikes;
+					}
+				});
 			});
 		},
 		events: {