Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
route 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ion05 committed Jul 30, 2022
1 parent 82932a7 commit 2ffbf7a
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 5 deletions.
108 changes: 108 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"homepage": "https://github.com/techsyndicate/minet-make#readme",
"dependencies": {
"axios": "^0.27.2",
"bcrypt": "^5.0.1",
"cookie-parser": "^1.4.6",
"dotenv": "^16.0.1",
Expand Down
7 changes: 4 additions & 3 deletions public/js/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var q1Btn = document.querySelector("#q1Btn");
q1Btn.addEventListener("click", (e) => {
e.preventDefault();
let prompt = document.querySelector("#prompt").value;
fetch("/market/q1", {
fetch("/market/pref/q1", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -18,10 +18,11 @@ q1Btn.addEventListener("click", (e) => {
if (data.status == 400) {
notyf.error(data.msg);
} else {
console.log("data", data);
notyf.success(data.msg);
setTimeout(() => {
notyf.success(data.msg);
window.location.href = "/market/pref/2";
}, 3000);
window.location.href = "/market/pref/2";
}
});
});
64 changes: 62 additions & 2 deletions routes/marketRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const router = require("express").Router();
const { checkUser } = require("../middleware/auth");
const template = require("../schemas/templateSchema");
const user = require("../schemas/userSchema");
const axios = require("axios");

router.get("/", checkUser, (req, res) => {
console.log(req.user);
Expand Down Expand Up @@ -36,7 +37,7 @@ router.post("/buy", checkUser, async (req, res) => {
credits: 30,
});
await newTemplate.save();

res.cookie("modelId", newTemplate._id);
return res.status(200).send({
msg: "Model purchased",
});
Expand Down Expand Up @@ -66,10 +67,69 @@ router.post("/credit", checkUser, async (req, res) => {
});
}
});
router.get("/pref/1", checkUser, async (req, res) => {
res.render("market/pref/1");
});

router.post("q1", checkUser, async (req, res) => {
router.post("/pref/q1", checkUser, async (req, res) => {
console.log("hello");
const prompt = req.body.prompt;
const userId = req.user["_id"];
const savedUser = await user.findById(userId);
let data = JSON.stringify({
kind: "KeyPhraseExtraction",
parameters: {
modelVersion: "latest",
},
analysisInput: {
documents: [
{
id: "1",
language: "en",
text: prompt,
},
],
},
});

let config = {
method: "post",
url: process.env.AZURE_ENDPOINT,
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": process.env.AZURE_KEY,
},
data: data,
};
axios(config)
.then(async (response) => {
try {
for (
let i = 0;
i < response.data.results.documents[0]["keyPhrases"].length;
i++
) {
const newUser = await user.findByIdAndUpdate(userId, {
$push: {
preferences: response.data.results.documents[0]["keyPhrases"][i],
},
});
}
return res.status(200).json({
msg: "Preferences Updated",
});
} catch (e) {
console.log(e);
return res.status(400).json({
msg: "Some Error Occurred",
});
}
})
.catch((err) => {
console.log(err);
return res.status(400).json({
msg: "Some Error Occurred",
});
});
});
module.exports = router;

0 comments on commit 2ffbf7a

Please sign in to comment.