2023-02-08 12:30:56 +01:00
|
|
|
const { ObjectId } = require('mongodb');
|
|
|
|
const connector = require("./CONNECTOR");
|
|
|
|
var dbo;
|
|
|
|
connector.connect().then((ret) => {
|
|
|
|
dbo = ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.collection = function (callback) {
|
|
|
|
dbo
|
|
|
|
.collection("radios")
|
|
|
|
.find({})
|
|
|
|
.sort({ name: 1 })
|
|
|
|
.toArray((err, result) => {
|
|
|
|
result.forEach(item => {
|
|
|
|
item.type = "radio";
|
|
|
|
});
|
|
|
|
callback(result);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.byId = function (id, callback) {
|
|
|
|
dbo
|
|
|
|
.collection("radios")
|
|
|
|
.findOne({ _id: ObjectId(id) }, (err, result) => {
|
|
|
|
if (err) throw err;
|
2023-09-21 11:13:47 +02:00
|
|
|
if (!result.covers) {
|
|
|
|
result.covers = {};
|
|
|
|
}
|
2023-02-08 12:30:56 +01:00
|
|
|
callback(result);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.add = function (radio, callback) {
|
|
|
|
dbo.collection("radios").updateOne(
|
|
|
|
{ url: radio.url },
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
name: radio.name,
|
|
|
|
url: radio.url
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ upsert: true },
|
|
|
|
err => {
|
|
|
|
if (err) throw err;
|
|
|
|
dbo.collection("radios").findOne({ url: radio.url }, (err, result) => {
|
|
|
|
if (err) throw err;
|
|
|
|
callback(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.delete = function (id, callback) {
|
|
|
|
dbo
|
|
|
|
.collection("radios")
|
|
|
|
.deleteOne({ _id: ObjectId(id) }, (err, result) => {
|
|
|
|
if (err) throw err;
|
|
|
|
callback(result);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update = function (radio, callback) {
|
|
|
|
dbo.collection("radios").updateOne(
|
|
|
|
{ _id: radio._id },
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
name: radio.name,
|
|
|
|
url: radio.url,
|
2023-09-21 11:13:47 +02:00
|
|
|
covers: radio.covers
|
2023-02-08 12:30:56 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{ upsert: false },
|
|
|
|
err => {
|
|
|
|
if (err) throw err;
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|