server/services/database/radios.js

83 lines
1.6 KiB
JavaScript
Raw Normal View History

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";
if (!item.covers) {
item.covers = {};
}
2023-02-08 12:30:56 +01:00
});
callback(result);
});
};
exports.byId = function (id, callback) {
dbo
.collection("radios")
.findOne({ _id: ObjectId(id) }, (err, result) => {
if (err) throw err;
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,
covers: radio.covers
2023-02-08 12:30:56 +01:00
}
},
{ upsert: false },
err => {
if (err) throw err;
if (callback) {
callback();
}
}
);
};