[クローンコード]YouTube 8日目
20971 ワード
💕 進捗
[スタック]オイルチューブクローンコード
3.5~3.12時間
😁 Multer
🤣 その他のコード // videoController.js
import routes from "../routes";
import Video from "../models/Video";
export const home = async (req, res) => {
try {
const videos = await Video.find({}).sort({
'_id': -1
});
res.render("home", {
pageTitle: "Home",
videos,
}); // 이건 프레임워크 규약
} catch (error) {
console.log(error);
res.render("home", {
pageTitle: "Home",
videos: [],
}); // 이건 프레임워크 규약
}
};
export const search = async (req, res) => {
const {
query: {
term: searchingBy
},
} = req;
let videos = []
try {
videos = await Video.find({
title: {
$regex: searchingBy,
$options: "i"
}
})
} catch (error) {
console.log(error)
}
res.render("search", {
pageTitle: "Search",
searchingBy,
videos
});
};
export const getUpload = (req, res) =>
res.render("upload", {
pageTitle: "Upload",
});
export const postUpload = async (req, res) => {
const {
body: {
title,
description
},
file: {
path
},
} = req;
// To do: upload and save video
const newVideo = await Video.create({
fileUrl: path,
title,
description,
});
res.redirect(routes.videoDetail(newVideo.id));
};
export const videoDetail = async (req, res) => {
const {
params: {
id
},
} = req;
try {
const video = await Video.findById(id);
res.render("videoDetail", {
pageTitle: video.title,
video,
});
} catch (error) {
console.log(error);
res.redirect(routes.home);
}
};
export const getEditVideo = async (req, res) => {
const {
params: {
id
},
} = req;
try {
const video = await Video.findById(id);
res.render("editVideo", {
pageTitle: `Edit ${video.title}`,
video,
});
} catch (error) {
console.log(error);
res.redirect(routes.home);
}
};
export const postEditVideo = async (req, res) => {
const {
params: {
id
},
body: {
title,
description
},
} = req;
try {
await Video.findOneAndUpdate({
_id: id,
}, {
title,
description,
});
res.redirect(routes.videoDetail(id));
} catch (error) {
console.log(error);
res.redirect(routes.home);
}
};
export const deleteVideo = async (req, res) => {
const {
params: {
id
}
} = req
try {
await Video.findOneAndDelete({
_id: id
})
} catch (error) {
console.log(error)
}
res.redirect(routes.home)
}
Reference
この問題について([クローンコード]YouTube 8日目), 我々は、より多くの情報をここで見つけました
https://velog.io/@devgosunman/클론코딩-유튜브-8일차-9nk6gp8f
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
// videoController.js
import routes from "../routes";
import Video from "../models/Video";
export const home = async (req, res) => {
try {
const videos = await Video.find({}).sort({
'_id': -1
});
res.render("home", {
pageTitle: "Home",
videos,
}); // 이건 프레임워크 규약
} catch (error) {
console.log(error);
res.render("home", {
pageTitle: "Home",
videos: [],
}); // 이건 프레임워크 규약
}
};
export const search = async (req, res) => {
const {
query: {
term: searchingBy
},
} = req;
let videos = []
try {
videos = await Video.find({
title: {
$regex: searchingBy,
$options: "i"
}
})
} catch (error) {
console.log(error)
}
res.render("search", {
pageTitle: "Search",
searchingBy,
videos
});
};
export const getUpload = (req, res) =>
res.render("upload", {
pageTitle: "Upload",
});
export const postUpload = async (req, res) => {
const {
body: {
title,
description
},
file: {
path
},
} = req;
// To do: upload and save video
const newVideo = await Video.create({
fileUrl: path,
title,
description,
});
res.redirect(routes.videoDetail(newVideo.id));
};
export const videoDetail = async (req, res) => {
const {
params: {
id
},
} = req;
try {
const video = await Video.findById(id);
res.render("videoDetail", {
pageTitle: video.title,
video,
});
} catch (error) {
console.log(error);
res.redirect(routes.home);
}
};
export const getEditVideo = async (req, res) => {
const {
params: {
id
},
} = req;
try {
const video = await Video.findById(id);
res.render("editVideo", {
pageTitle: `Edit ${video.title}`,
video,
});
} catch (error) {
console.log(error);
res.redirect(routes.home);
}
};
export const postEditVideo = async (req, res) => {
const {
params: {
id
},
body: {
title,
description
},
} = req;
try {
await Video.findOneAndUpdate({
_id: id,
}, {
title,
description,
});
res.redirect(routes.videoDetail(id));
} catch (error) {
console.log(error);
res.redirect(routes.home);
}
};
export const deleteVideo = async (req, res) => {
const {
params: {
id
}
} = req
try {
await Video.findOneAndDelete({
_id: id
})
} catch (error) {
console.log(error)
}
res.redirect(routes.home)
}
Reference
この問題について([クローンコード]YouTube 8日目), 我々は、より多くの情報をここで見つけました https://velog.io/@devgosunman/클론코딩-유튜브-8일차-9nk6gp8fテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol