Pages

Tuesday, 4 July 2023

input tambahan

app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended: false }))

 Asosiasi

static associate(models) {
      // define association here
      Employee.belongsTo(models.Store, { foreignKey: "StoreId" })
    }

untuk model Store dan employee

static associate(models) {
      // define association here
      Store.hasMany(models.Employee, {foreignKey: "StoreId"})
    }

Untuk validasi

Store.init({
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {
          msg: 'Name is required',
        },
        notEmpty: {
          msg: 'Name is required',
        }
      }
    },
    position: {
      type : DataTypes.STRING,
      allowNull : false,
      validate: {
        notNull : {
          msg: "Position is require"
        },
        notEmpty: {
          msg: "Position is require"
        },
        validPosition(value){
          if((this.position === 'S2' || this.position === 'S3') &&
          (value !== 'manager' && value !== 'CEO')){
            throw new Error('S2 & S3 only can be Manager or CEO')
          }
        }
      }
    },
    //untuk hooks
    sequelize,
    modelName: 'Store',
    hooks : {
      beforeCreate : function (store, opt){
        if(store.category === 'Mart'){
          store.code = `001-${new Date().getTime()}`
        }
        if(store.category === 'Midi'){
          store.code = `002-${new Date().getTime()}`
        }
        if(store.category === 'Express'){
          store.code = `003-${new Date().getTime()}`
        }
      },
      afterCreate : function(after, opt){
      }
    }
})

untuk errors
.catch((err) => {
            if (err.name === 'SequelizeValidationError'){
                const errors = err.errors.map(error => {
                    return error.message
                })
                res.send(errors)
                // console.log(errors);
               
            }else{
                res.send(err)
            }
        })
atau

.catch((err) => {
            if (err.name === 'SequelizeValidationError') {
                let errors = err.errors.map(el => el.message)
                res.redirect(`/stores/${storeId}/employees/${employeeId}/edit?errors=${errors}`)
            } else {
                res.send(err);
            }
          });

untuk update
Employee.update(
          { firstName, lastName, dateOfBirth, education, position, salary },
          { where: { id: employeeId } }
        )

utnuk delete
Art.destroy({
            where: {
                id: id
            }
        })

untuk tambah data

const {name, artist, date, photo, placeOfOrigin, description} = req.body
        Art.create({name, artist, date, photo, placeOfOrigin, description})

untuk search
static showArts(req, res){
        let {title, artist} = req.query
        let dataArt
        let option = {
        }

        if(title){
            option.where = {
                name:{
                    [Op.iLike] : `%${title}%`
                }
            }
        }

        if(artist){
            option.where = {
                artist:{
                    [Op.like] : `%${artist}%`
                }
            }
        }

        option.order = [['date', 'asc']]
        Art.findAll(option)
        .then((data) => {
            dataArt = data
            return Art.alert()
        })
        .then((alert) => {
            const { sumArt, oldYear, latestYear } = alert[0].dataValues;
            res.render('home',{data : dataArt, alert :{ sumArt, oldYear, latestYear }, title: 'List Of Art'})
        })
        .catch((err) => {
            res.send(err)
        })
    }

No comments:

Post a Comment