Flare-k

[Modified] Searching videos

/* eslint-disable no-console */
import routes from "../routes";
import Video from "../models/Video";
......@@ -15,10 +16,18 @@ export const home = async (req, res) => {
}
};
export const search = (req, res) => {
export const search = async (req, res) => {
const {
query: { term: searchingBy },
} = req; // == const searchingBy = req.query.term;
let videos = [];
try {
videos = await Video.find({
title: { $regex: searchingBy, $options: "i" }, // i를 옵션으로 추가하면 insensitive.. 대소문자 구분 안함.
});
} catch (error) {
console.log(error);
}
res.render("search", { pageTitle: "Search", searchingBy, videos });
};
......
/* eslint-disable no-console */
import mongoose from "mongoose";
import dotenv from "dotenv";
dotenv.config();
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useFindAndModify: false,
useNewUrlParser: true,
useFindAndModify: false,
});
const db = mongoose.connection;
const handleOpen = () => {
console.log("✅ Connected to DB");
console.log("✅ Connected to DB");
};
const handleError = (error) =>
console.log(`🔴 Error on DB Connection: ${error}`);
console.log(`🔴 Error on DB Connection: ${error}`);
db.once("open", handleOpen); //connection을 열고 성공여부를 확인할 수 있는 function을 만들 것이다.
db.on("error", handleError);
\ No newline at end of file
db.once("open", handleOpen); // connection을 열고 성공여부를 확인할 수 있는 function을 만들 것이다.
db.on("error", handleError);
......
......@@ -5,9 +5,13 @@ block content
.search__header
h3 Searching for: #{searchingBy}
.search__videos
if videos.length === 0
h5 No Videos Found
each item in videos
+videoBlock({
title : item.title,
views: item.views,
videoFile:item.videoFile
videoFile:item.videoFile,
id: item.id
})
\ No newline at end of file
......
......@@ -7,4 +7,9 @@ block content
a(href=routes.editVideo(video.id)) Edit video
h5.video__title=video.title
span.video__views=video.views
p.video__description=video.description
\ No newline at end of file
p.video__description=video.description
.video__comment
if video.comments.length === 1
span.video__comment-number 1 comment
else
span.video__comment-number #{video.comments.length} comments
\ No newline at end of file
......