🚀 Initial commit for rzmk/smart-brain-api!
This commit is contained in:
commit
16e22cda96
10 changed files with 4048 additions and 0 deletions
29
controllers/image.js
Normal file
29
controllers/image.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
const Clarifai = require("clarifai"); // Face detection model API
|
||||
|
||||
const app = new Clarifai.App({
|
||||
apiKey: process.env.API_CLARIFAI,
|
||||
});
|
||||
|
||||
const handleApiCall = (req, res) => {
|
||||
app.models
|
||||
.predict(Clarifai.FACE_DETECT_MODEL, req.body.input)
|
||||
.then((data) => {
|
||||
res.json(data);
|
||||
})
|
||||
.catch((err) => res.status(400).json("Unable to work with API."));
|
||||
};
|
||||
|
||||
const handleImage = (req, res, db) => {
|
||||
const { id } = req.body;
|
||||
db("users")
|
||||
.where("id", "=", id)
|
||||
.increment("entries", 1) // "entries" is the total number of image uploads the user has made
|
||||
.returning("entries")
|
||||
.then((entries) => res.json(entries[0]))
|
||||
.catch((err) => res.status(400).json("Unable to get entries."));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
handleImage: handleImage,
|
||||
handleApiCall: handleApiCall,
|
||||
};
|
||||
20
controllers/profile.js
Normal file
20
controllers/profile.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
const handleProfileGet = (req, res, db) => {
|
||||
const { id } = req.params;
|
||||
db.select("*")
|
||||
.from("users")
|
||||
.where({
|
||||
id: id,
|
||||
})
|
||||
.then((user) => {
|
||||
if (user.length) {
|
||||
res.json(user[0]);
|
||||
} else {
|
||||
res.status(400).json("Not found.");
|
||||
}
|
||||
})
|
||||
.catch((err) => res.status(400).json("Error getting user."));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
handleProfileGet: handleProfileGet,
|
||||
};
|
||||
43
controllers/register.js
Normal file
43
controllers/register.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
const handleRegister = (req, res, db, bcrypt) => {
|
||||
const { name, email, password } = req.body;
|
||||
// Validate the input to not be empty
|
||||
if (!email || !name || !password) {
|
||||
return res.status(400).json("Incorrect form submission.");
|
||||
}
|
||||
|
||||
// Hash (encrypt) the password
|
||||
const hash = bcrypt.hashSync(password);
|
||||
|
||||
/*
|
||||
* Database transaction to make sure all the queries are executed together
|
||||
* First query is to insert the user into the login table
|
||||
* Second query is to insert the user into the users table
|
||||
* Then we return the user
|
||||
*/
|
||||
db.transaction((trx) => {
|
||||
trx.insert({
|
||||
hash: hash,
|
||||
email: email,
|
||||
})
|
||||
.into("login")
|
||||
.returning("email")
|
||||
.then((loginEmail) => {
|
||||
return trx("users")
|
||||
.returning("*")
|
||||
.insert({
|
||||
email: loginEmail[0],
|
||||
name: name,
|
||||
joined: new Date(),
|
||||
})
|
||||
.then((user) => {
|
||||
res.json(user[0]);
|
||||
})
|
||||
.then(trx.commit);
|
||||
})
|
||||
.catch(trx.rollback);
|
||||
}).catch((err) => res.status(400).json("Unable to register."));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
handleRegister: handleRegister,
|
||||
};
|
||||
34
controllers/signin.js
Normal file
34
controllers/signin.js
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
const handleSignIn = (req, res, db, bcrypt) => {
|
||||
const { email, password } = req.body;
|
||||
// Validate the input to not be empty
|
||||
if (!email || !password) {
|
||||
return res.status(400).json("Incorrect form submission.");
|
||||
}
|
||||
// Check if the user exists in the database
|
||||
db.select("email", "hash")
|
||||
.from("login")
|
||||
.where("email", "=", email)
|
||||
.then((data) => {
|
||||
const isValid = bcrypt.compareSync(password, data[0].hash);
|
||||
// If the user exists and the password is correct, return the user
|
||||
if (isValid) {
|
||||
return db
|
||||
.select("*")
|
||||
.from("users")
|
||||
.where("email", "=", email)
|
||||
.then((user) => {
|
||||
res.json(user[0]);
|
||||
})
|
||||
.catch((err) =>
|
||||
res.status(400).json("Unable to get user.")
|
||||
);
|
||||
} else {
|
||||
res.status(400).json("Invalid credentials.");
|
||||
}
|
||||
})
|
||||
.catch((err) => res.status(400).json("Invalid credentials."));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
handleSignIn: handleSignIn,
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue