Flare-k

[Modified] Searching videos

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