Skip to content

Commit

Permalink
Merge pull request #12 from sol3uk/dev
Browse files Browse the repository at this point in the history
1.2 to Live
  • Loading branch information
sol3uk authored Dec 29, 2021
2 parents 3b816ec + 50a1221 commit 0fbcce8
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 35 deletions.
36 changes: 31 additions & 5 deletions controllers/youtubeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ module.exports = {
});
},

//#region CREATE
async createStream(streamData) {
let response = await youtube.liveBroadcasts.insert({
part,
// Request body metadata
requestBody: streamData,
});
console.log(
"ADDED STREAM ----------------- :",
response.data.id,
response.data.snippet?.title
);
return response;
},
//#endregion

//#region READ
async getStreams(status = streamStatus.UPCOMING, limit = 10) {
const livestreams = await youtube.liveBroadcasts.list({
part,
Expand All @@ -39,18 +56,16 @@ module.exports = {

return livestreams;
},
//#endregion

//#region UPDATE
async stopStreamById(id) {
let response = await youtube.liveBroadcasts.transition({
broadcastStatus: "complete",
id: id,
part,
});
console.log(
"STOPPED STREAM ----------------- :",
response.data.id,
response.data.snippet?.title
);
console.log("STOPPED STREAM ----------------- :", response);
return response;
},

Expand All @@ -67,4 +82,15 @@ module.exports = {
);
return response;
},
//#endregion

//#region DELETE
async deleteStream(streamId) {
let response = await youtube.liveBroadcasts.delete({
id: streamId,
});
console.log("DELETED STREAM ----------------- :", streamId);
return response;
},
//#endregion
};
37 changes: 21 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-handlebars": "^5.2.1",
"googleapis": "^67.1.0"
"googleapis": "^67.1.0",
"material-design-icons": "^3.0.1"
},
"devDependencies": {
"node-sass": "^5.0.0",
"nodemon": "^2.0.7"
}
}
}
57 changes: 54 additions & 3 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ function editStreamModal(id, element) {

startSpinnerAndDisable(element, true);

let url = `/modal/editStreams/${id}`;
let url = `/modal/editStream/${id}`;
const response = partialRequest(url, {
method: "GET",
cache: "no-cache",
referrerPolicy: "no-referrer",
})
.then(function (data) {
removeSpinners(element);
let modalElement = document.getElementById("editStreamsModal");
let modalElement = document.getElementById("actionModal");
setInnerHTML(modalElement, data);
let modal = new bootstrap.Modal(modalElement);
modal.show();
Expand Down Expand Up @@ -258,7 +258,58 @@ function editStream(e) {
})
.catch(function (error) {
removeSpinners();
hideAndResetModal("stopStreamsModal");
hideAndResetModal("actionModal");

addError(error);
console.log("Request failed: ", error);
});
}

function deleteStreamModal(id, element) {
//Get Bootstrap modal so we can manipulate it later

startSpinnerAndDisable(element, true);

let url = `/modal/deleteStream/${id}`;
const response = partialRequest(url, {
method: "GET",
cache: "no-cache",
referrerPolicy: "no-referrer",
})
.then(function (data) {
removeSpinners(element);
let modalElement = document.getElementById("actionModal");
setInnerHTML(modalElement, data);
let modal = new bootstrap.Modal(modalElement);
modal.show();
//TODO: dispose of old modals after they are closed
})
.catch(function (error) {
removeSpinners();

addError(error);
console.log("Request failed: ", error);
});
}

function deleteStream(id, e) {
startSpinnerAndDisable(e.target, true);

let url = `/api/streams/delete/${id}`;
const response = request(url, {
method: "DELETE",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
referrerPolicy: "no-referrer",
})
.then(function (data) {
window.location.href = data.redirectUrl; //redirect after successful stop
})
.catch(function (error) {
removeSpinners();
hideAndResetModal("actionModal");

addError(error);
console.log("Request failed: ", error);
Expand Down
24 changes: 24 additions & 0 deletions routes/apiRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ const { authCheck } = require("../middleware/authCheck");
const youtube = require("../controllers/youtubeController");
const Stream = require("../models/Stream");

//#region CREATE
//#endregion

//#region READ
//#endregion

//#region UPDATE
router.post("/streams/stop/:id", authCheck, async (req, res) => {
try {
let stoppedStream = await youtube.stopStreamById(req.params.id);
Expand Down Expand Up @@ -32,5 +39,22 @@ router.post("/streams/edit/:id", authCheck, async (req, res) => {
res.status(500).json(e);
}
});
//#endregion

//#region DELETE
router.delete("/streams/delete/:id", authCheck, async (req, res) => {
try {
const response = await youtube.deleteStream(req.params.id); //Get stream for other details we need to populate for API

res.status(200).json({
redirectUrl: "/streams",
message: response,
});
} catch (e) {
console.error("ERROR STOPPING:", e);
res.status(500).json(e);
}
});
//#endregion

module.exports = router;
14 changes: 13 additions & 1 deletion routes/partialRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { formatStreams } = require("../utils/main");
/* Routing */

// Partials -------------
router.get("/editStreams/:id", authCheck, isLoggedIn, async (req, res) => {
router.get("/editStream/:id", authCheck, isLoggedIn, async (req, res) => {
const stream = await youtube.getStreamById(req.params.id);
const [formattedStream] = formatStreams(stream);

Expand All @@ -19,4 +19,16 @@ router.get("/editStreams/:id", authCheck, isLoggedIn, async (req, res) => {
});
});

router.get("/deleteStream/:id", authCheck, isLoggedIn, async (req, res) => {
const stream = await youtube.getStreamById(req.params.id);
const [formattedStream] = formatStreams(stream);

res.render("partials/deleteStream", {
model: {
stream: formattedStream,
},
layout: false, //Need this so we just render the partial on its own
});
});

module.exports = router;
5 changes: 5 additions & 0 deletions scss/main.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$enable-caret: false;
@import "../node_modules/bootstrap/scss/bootstrap";

footer {
Expand All @@ -21,3 +22,7 @@ article {
width: 100%;
}
}

.material-icons {
vertical-align: middle;
}
5 changes: 3 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const cookieParser = require("cookie-parser");
const hbs = require("express-handlebars");
const helpers = require("./helpers/helpers");

const PORT = config.port || 8080;
const app = express();
app.use(express.static("public"));
app.use(cookieParser());
Expand All @@ -29,5 +30,5 @@ app.use("/api", apiRoutes);
app.use("/modal", partialRoutes);
app.use("/", pageRoutes);

app.listen(config.port || 8080);
console.log("Listening on port", config.port || 8080);
app.listen(PORT);
console.log(`Listening on http://localhost:${PORT}`);
1 change: 1 addition & 0 deletions views/layouts/main.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>The Livestream Console</title>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="./css/main.css">
</head>

Expand Down
16 changes: 16 additions & 0 deletions views/partials/deleteStream.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Livestream?</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-center">
<p>This will completely <b>DELETE</b> the livestream:</br>
<i>{{model.stream.title}}</i></br></p>
<button type="button" class="btn btn-danger" onClick="deleteStream('{{model.stream.id}}',this)">Yes, I'm sure, DELETE</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion views/partials/editStream.handlebars
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit Livestream</h5>
<h5 class="modal-title">Edit Livestream - Id: {{model.stream.id}}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Expand Down
2 changes: 1 addition & 1 deletion views/partials/navbar.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<a class="nav-link {{#if model.path.about}}active{{/if}}" href="/about">About</a>
</li>
</ul>
<span class="navbar-text mx-2">v1.1</span>
<span class="navbar-text mx-2">v1.2</span>
{{#unless model.path.home}}
{{#if model.loggedIn}}
<a class="btn btn-sm btn-outline-danger" href="/logout" role="button">Log out</a>
Expand Down
Loading

0 comments on commit 0fbcce8

Please sign in to comment.