Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
2
Indexable
Never
const axios = require("axios");

if (process.env.NODE_ENV !== "production") {
  require("dotenv").config();
}

module.exports.index = async (req, res) => {
  const { API_RESOURCE_URL } = process.env;
  const { accessToken } = req.session;
  const {
    data: { hotels },
  } = await axios.get(API_RESOURCE_URL + "hotels", {
    headers: {
      Authorization: "Bearer " + accessToken,
    },
  });
  res.render("hotels/index", { hotels });
};

module.exports.showHotel = async (req, res) => {
  const { API_RESOURCE_URL } = process.env;
  const { accessToken } = req.session;
  const {
    data: { hotel },
  } = await axios.get(API_RESOURCE_URL + `hotels/${req.params.id}`, {
    headers: {
      Authorization: "Bearer " + accessToken,
    },
  });
  if (!hotel) {
    return res.redirect("/hotels");
  }
  res.render("hotels/show", { hotel });
};

module.exports.renderNewForm = (req, res) => {
  res.render("hotels/new");
};

module.exports.createHotel = async (req, res) => {
  const { data } = await axios.post(
    process.env.API_RESOURCE_URL + "hotels",
    req.body,
    {
      headers: {
        Authorization: "Bearer " + req.session.accessToken,
      },
    }
  );
  res.redirect(`/hotels/${data.hotel._id}`);
};