Update: Playlist.delete + returns added + check if ObjectId is valid
This commit is contained in:
parent
b73da1a288
commit
a34f3030dd
1 changed files with 53 additions and 27 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
const db = require("../models/mongodb.model");
|
const db = require("../models/mongodb.model");
|
||||||
const {sendError, sendMessage} = require ("../config/response.config");
|
const {sendError, sendMessage} = require ("../config/response.config");
|
||||||
const {checkLogin} = require("../config/sessionJWT.config");
|
const {checkLogin} = require("../config/sessionJWT.config");
|
||||||
|
const ObjectId = require('mongoose').Types.ObjectId;
|
||||||
const Playlist = db.playlists;
|
const Playlist = db.playlists;
|
||||||
|
|
||||||
// Create a new Playlist
|
// Create a new Playlist
|
||||||
|
|
@ -111,17 +112,21 @@ exports.findOne = (req, res) => {
|
||||||
const token = checkLogin(req, res);
|
const token = checkLogin(req, res);
|
||||||
if(token && typeof req.params.id !== 'undefined') {
|
if(token && typeof req.params.id !== 'undefined') {
|
||||||
const id = req.params.id;
|
const id = req.params.id;
|
||||||
Playlist.findById(id, {})
|
if(id && ObjectId.isValid(id)){
|
||||||
.then(data => {
|
Playlist.findById(id, {})
|
||||||
if(data){
|
.then(data => {
|
||||||
return sendMessage(res, 23, data, token);
|
if(data){
|
||||||
} else {
|
return sendMessage(res, 23, data, token);
|
||||||
return sendError(res,404,105,`Playlist not found with id=${id}`, token);
|
} else {
|
||||||
}
|
return sendError(res,404,105,`Playlist not found with id=${id}`, token);
|
||||||
})
|
}
|
||||||
.catch(err => {
|
})
|
||||||
return sendError(res,500,100,err.message || `Some error occurred while finding the Playlist with id=${id}`, token);
|
.catch(err => {
|
||||||
});
|
return sendError(res,500,100,err.message || `Some error occurred while finding the Playlist with id=${id}`, token);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return sendError(res, 500, -1, `Error id is not valid`, token);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return sendError(res, 500, -1, `No id given`, token);
|
return sendError(res, 500, -1, `No id given`, token);
|
||||||
}
|
}
|
||||||
|
|
@ -157,18 +162,22 @@ exports.update = (req, res) => {
|
||||||
// Remove undefined key
|
// Remove undefined key
|
||||||
Object.keys(update).forEach(key => update[key] === undefined ? delete update[key] : {});
|
Object.keys(update).forEach(key => update[key] === undefined ? delete update[key] : {});
|
||||||
|
|
||||||
Playlist.updateOne({_id: id, userId: token.id}, update)
|
if(id && ObjectId.isValid(id)){
|
||||||
.then(data => {
|
Playlist.updateOne({_id: id, userId: token.id}, update)
|
||||||
if(data) {
|
.then(data => {
|
||||||
//Object.keys(update).forEach(key => data[key] = update[key]);
|
if(data) {
|
||||||
sendMessage(res, 7, update, token);
|
//Object.keys(update).forEach(key => data[key] = update[key]);
|
||||||
} else {
|
return sendMessage(res, 24, update, token);
|
||||||
sendError(res, 404, -1, `Playlist not found with id=${id}`, token);
|
} else {
|
||||||
}
|
return sendError(res, 404, -1, `Playlist not found with id=${id}`, token);
|
||||||
})
|
}
|
||||||
.catch(err => {
|
})
|
||||||
sendError(res, 500, -1, err.message || `Some error occurred while updating the Playlist with id=${id}`, token);
|
.catch(err => {
|
||||||
});
|
return sendError(res, 500, -1, err.message || `Some error occurred while updating the Playlist with id=${id}`, token);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return sendError(res, 500, -1, `Error id is not valid`, token);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return sendError(res, 500, -1, `No id given`, token);
|
return sendError(res, 500, -1, `No id given`, token);
|
||||||
|
|
@ -178,8 +187,25 @@ exports.update = (req, res) => {
|
||||||
// Delete a Playlist with playlist id
|
// Delete a Playlist with playlist id
|
||||||
exports.delete = (req, res) => {
|
exports.delete = (req, res) => {
|
||||||
const token = checkLogin(req, res);
|
const token = checkLogin(req, res);
|
||||||
if(token){
|
if(token && typeof req.params.id !== 'undefined') {
|
||||||
return sendError(res, 501, -1, "Playlist.delete not Implemented", token);
|
const id = req.params.id;
|
||||||
|
if(id && ObjectId.isValid(id)){
|
||||||
|
Playlist.findByIdAndUpdate(id, {isActive: false}, {useFindAndModify: false})
|
||||||
|
.then(data => {
|
||||||
|
if(data) {
|
||||||
|
return sendMessage(res, 25, {message: `Playlist ${id} was successfully deleted.`}, token);
|
||||||
|
} else {
|
||||||
|
return sendError(res, 404, 105, `Playlist not found with id=${id}`, token);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
return sendError(res, 500, 100, err.message || `Some error occurred while deleting the Playlist with id=${id}`, token);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return sendError(res, 500, -1, `Error id is not valid`, token);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return sendError(res, 500, -1, `No id given`, token);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -189,12 +215,12 @@ exports.deleteAll = (req, res) => {
|
||||||
if(token) {
|
if(token) {
|
||||||
Playlist.deleteMany({userId: {$eq: token.id}})
|
Playlist.deleteMany({userId: {$eq: token.id}})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
sendMessage(res, 1, {
|
return sendMessage(res, 26, {
|
||||||
message: `${data.deletedCount} Playlists were deleted successfully.`
|
message: `${data.deletedCount} Playlists were deleted successfully.`
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
sendError(res, 500, -1, err.message || "Some error occurred while removing all Playlists.");
|
return sendError(res, 500, -1, err.message || "Some error occurred while removing all Playlists.");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Reference in a new issue