Untitled
unknown
plain_text
2 years ago
5.7 kB
10
Indexable
const LiveMatch = require("./model"); // Adjust the path as necessary
const { createStreaming } = require("./services");
const { liveMatchValidationSchema } = require("./validation");
const Stream = require("../streams/model");
const { createStreaming, generateRandomId } = require("../../utils");
const saveStreams = async (match, streamingData) => {
const createdStreams = await Promise.all(
streamingData.map(async streamData => {
const newStream = new Stream({
id: generateRandomId(15),
matchId: match._id,
match_id: match.id,
...streamData
});
match.streaming_sources.push(newStream._id);
await newStream.save();
return newStream;
})
);
return createdStreams;
};
exports.createLiveMatch = async (req, res, next) => {
try {
const result = await liveMatchValidationSchema.validateAsync(req.body);
const matchData = result;
const streamingData = createStreaming(matchData);
const newMatch = new LiveMatch({
...matchData,
match_time: matchData.utcTime,
sports_type_name: matchData.sports_type_name,
streaming_sources: []
});
const createdStreams = await saveStreams(newMatch, streamingData);
await newMatch.save();
return res.status(200).json({
status: true,
message: "Match created successfully!",
data: { match: newMatch, streams: createdStreams }
});
} catch (error) {
console.error(error);
if (error.isJoi) {
return res.status(400).json({ status: false, message: error.details[0].message });
}
next(error);
}
};
exports.updateMatch = async (req, res, next) => {
try {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ status: false, errors: errors.array() });
}
const matchId = req.params.matchId;
const matchData = req.body;
const existingMatch = await LiveMatch.findOne({ id: matchId });
if (!existingMatch) {
return res.status(404).json({ status: false, message: "Live Match not found!" });
}
await Stream.deleteMany({ match_id: existingMatch.id });
// Update match fields
Object.assign(existingMatch, {
match_title: matchData.match_title,
time: matchData.time,
match_time: matchData.utcTime,
fixture_id: matchData.fixture_id,
sports_type: matchData.sports_type,
sports_type_name: matchData.sports_type_name,
league_name: matchData.league_name,
league: matchData.league,
league_image: matchData.league_image,
is_hot: matchData.is_hot,
status: matchData.status,
team_one_name: matchData.team_one_name,
team_one_image: matchData.team_one_image,
team_two_name: matchData.team_two_name,
team_two_image: matchData.team_two_image,
streaming_sources: []
});
const streamingData = createStreaming(matchData);
const createdStreams = await saveStreams(existingMatch, streamingData);
await existingMatch.save();
return res.status(200).json({
status: true,
message: "Live Match and Streams updated successfully!",
match: existingMatch
});
} catch (error) {
console.error(error);
next(error);
}
};
// Get all live matches
exports.getAllLiveMatches = async (req, res, next) => {
try {
const matches = await LiveMatch.find().sort({ position: "asc" }).populate("streaming_sources");
return res.status(200).json({ status: true, data: matches });
} catch (error) {
console.error(error);
next(error);
}
};
// Get a single live match by ID with streaming sources populated
exports.getLiveMatchById = async (req, res, next) => {
try {
const liveMatch = await LiveMatch.findById(req.params.id).populate("streaming_sources").exec();
if (!liveMatch) return res.status(404).send("Live Match not found");
res.send(liveMatch);
} catch (error) {
console.error(error);
next(error);
}
};
exports.deleteMatchWithStreams = async (req, res, next) => {
try {
const matchId = req.params.matchId; // Assuming you're passing the match ID as a URL parameter
const existingMatch = await LiveMatch.findOne({ id: matchId });
if (!existingMatch) {
return res.status(404).json({ status: false, message: "Live Match not found!" });
}
// Delete associated streams
await Stream.deleteMany({ match_id: matchId });
// Delete the match
await LiveMatch.deleteOne({ id: matchId });
return res.status(200).json({
status: true,
message: "Live match deleted successfully!"
});
} catch (error) {
console.error(error);
next(error);
}
};
exports.sortMatch = async (req, res, next) => {
try {
const matchData = req.body;
await Promise.all(
matchData.map(async match => {
const liveMatch = await LiveMatch.findById(match._id);
liveMatch.position = match.position;
await liveMatch.save();
return liveMatch;
})
);
return res.status(200).json({
status: true,
message: "Live Match Sorted Successfully!"
});
} catch (error) {
console.error(error);
next(error);
}
};
exports.sortStreamingSource = async (req, res, next) => {
try {
const sourceData = req.body;
await Promise.all(
sourceData.map(async source => {
const streamSources = await Stream.findByIdAndUpdate(source._id, {
position: source.position
});
return streamSources;
})
);
return res.status(200).json({
status: true,
message: "Stream Source Sorted Successfully!"
});
} catch (error) {
console.error(error);
next(error);
}
};
Editor is loading...
Leave a Comment