👇코드 보러가기

https://github.com/Sara-Jo/BeWild/tree/3ad009af86192647d46b0f7624e095257c389949

const CampgroundSchema = new Schema ({
    title: String,
    image: String,
    price: Number,
    description: String,
    location: String,
});
const sample = array => array[Math.floor(Math.random() * array.length)];

const seedDB = async () => {
    // 모든 DB 지우기
    await Campground.deleteMany({});
    // DB 200개 생성하기
    for (let i = 0; i < 200; i++){
        const random1000 = Math.floor(Math.random() * 1000);      // 0 - 1000
        const randomPrice = Math.floor(Math.random() * 300 + 100);     // 100 - 400
        const camp = new Campground ({
            title: `${sample(descriptors)} ${sample(places)}`,
            price: randomPrice,
            location: `${cities[random1000].city}, ${cities[random1000].state}`
        });
        await camp.save();
    }
}
app.get("/", (req, res) => {
    res.render("home");
});

// Read
app.get("/campgrounds", async (req, res) => {
    const campgrounds = await Campground.find({});
    res.render("campgrounds/campgrounds", { campgrounds });
});

app.get("/campgrounds/new", (req, res) => {
    res.render("campgrounds/new");
});

// Create
app.post("/campgrounds", async(req, res) => {
    const campground = new Campground(req.body.campground);
    await campground.save();
    res.redirect(`/campgrounds/${campground._id}`);
});

app.get("/campgrounds/:id", async(req, res) => {
    const campground = await Campground.findById(req.params.id);
    res.render("campgrounds/show", { campground });
});

app.get("/campgrounds/:id/edit", async(req, res) => {
    const campground = await Campground.findById(req.params.id);
    res.render("campgrounds/edit", { campground });
});

// Update
app.put("/campgrounds/:id", async(req, res) => {
    const {id} = req.params;
    const campground = await Campground.findByIdAndUpdate(id, {...req.body.campground});
    res.redirect(`/campgrounds/${campground._id}`);
});

// Delete
app.delete("/campgrounds/:id", async(req, res) => {
    const {id} = req.params;
    await Campground.findByIdAndDelete(id);
    res.redirect("/campgrounds");
});