How to recover an idUsuario(user_id) from an idAnuncio(ad_id) models by Discount(Discount) Discounts? REST API

Asked

Viewed 27 times

0

I need to do a query to recover a User’s discounts(idUsuario = user_id) by the Discount models' numbers, however, discount is only related to an idAnuncio(ad_id), so it’s like recovering an id via another id...

The API architecture follows the MVR standard and models and Routes are in the Express structure.

Database is the sqlite.

Follows the model structure:

inserir a descrição da imagem aqui

Follow the get of a Discount, which I need to do by idUsuario(user_id):

module.exports = app => {

const Discount = app.db.models.Discount;

//rota para todos descontos somente de um usuario/fornecedor 
app.route("/discounts/provider/:ad_id")
    .get((req, res) => {
        Discount.findAll({ where: req.params })
            .then(result => {
                if (result) {
                    res.json(result);
                } else {
                    res.sendStatus(404);
                }
            })
            .catch(error => {
                res.status(412).json({ msg: error.message });
            });
    })
}

Follow the db.js database file:

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
let db = null;

module.exports = app => {
if (!db) {
    const config = app.libs.config;
    const sequelize = new Sequelize(
        config.database,
        config.username,
        config.password,
        config.params
    );
    db = {
        sequelize,
        Sequelize,
        models: {}
    };
    const dir = path.join(__dirname, "models");
    fs.readdirSync(dir).forEach(file => {
        const modelDir = path.join(dir, file);
        const model = sequelize.import(modelDir);
        db.models[model.name] = model;
    });
    Object.keys(db.models).forEach(key => {
        db.models[key].associate(db.models);
    });
}
    return db;
};
  • What library are you using to manipulate the database? What is the variable Ads?

  • I’m using the sequelize to manipulate the bank and Ads this wrong, it’s actually const Discount = app.db.models.Discount;

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.