Пример #1
0
// Find all user updates
func (self *UpdateLogManager) FindAll(userId int64) []*models.UserUpdate {

	db := self.db
	rows, err := db.Query("SELECT updated_at, old_data, new_data FROM user_update WHERE user_id = ? ORDER BY id DESC", userId)
	utils.CheckErr(err, nil)
	defer rows.Close()

	var oldData, newData []byte
	userUpdates := make([]*models.UserUpdate, 0)
	for rows.Next() {
		userUpdate := new(models.UserUpdate)
		err = rows.Scan(&userUpdate.UpdatedAt, &oldData, &newData)
		json.Unmarshal(oldData, &userUpdate.OldData)
		json.Unmarshal(newData, &userUpdate.NewData)

		utils.CheckErr(err, nil)
		userUpdates = append(userUpdates, userUpdate)
	}

	if err = rows.Err(); err != nil {
		utils.CheckErr(err, nil)
	}

	return userUpdates
}
Пример #2
0
// Update user
func (self *AuthManager) Update(user *models.User) {
	db := self.db
	stmt, err := db.Prepare("UPDATE users SET token=? WHERE id = ?")
	utils.CheckErr(err, nil)
	_, err = stmt.Exec(user.Token, user.Id)
	utils.CheckErr(err, nil)
}
Пример #3
0
// Update user
func (self *UserManager) Update(user *models.User) {
	db := self.db
	stmt, err := db.Prepare("UPDATE users SET full_name=?, address=?, phone=?, hash=?, is_active=? WHERE id = ?")
	utils.CheckErr(err, nil)
	_, err = stmt.Exec(user.FullName, user.Address, user.Phone, user.Hash, user.IsActive, user.Id)
	utils.CheckErr(err, nil)
}
Пример #4
0
// Create user
func (self *UserManager) Create(user *models.User) int64 {
	db := self.db

	stmt, err := db.Prepare("INSERT INTO users(full_name, address, phone, email, password, hash, is_active) VALUES(?, ?, ?, ?, ?, ?, ?)")
	utils.CheckErr(err, nil)
	res, err := stmt.Exec(user.FullName, user.Address, user.Phone, user.Email, user.Password, user.Hash, user.IsActive)
	utils.CheckErr(err, nil)
	lastId, err := res.LastInsertId()
	utils.CheckErr(err, nil)

	return lastId
}
Пример #5
0
// save new user update record
func (self *UpdateLogManager) saveToDb(userUpdate *models.UserUpdate) int64 {
	db := self.db

	stmt, err := db.Prepare("INSERT INTO user_update(user_id, old_data, new_data, updated_at) VALUES(?, ?, ?, ?)")
	utils.CheckErr(err, nil)
	oldData, err := json.Marshal(userUpdate.OldData)
	newData, err := json.Marshal(userUpdate.NewData)
	res, err := stmt.Exec(userUpdate.UserId, oldData, newData, userUpdate.UpdatedAt)
	utils.CheckErr(err, nil)
	lastId, err := res.LastInsertId()
	utils.CheckErr(err, nil)

	return lastId
}
Пример #6
0
// Get database di
func (di *DI) Db() *sql.DB {
	if di.db == nil {
		config := di.Config()
		db, err := sql.Open(config.DbDriver, config.DbUser+":"+config.DbPass+"@/"+config.DbName+config.DbParams)
		utils.CheckErr(err, "sql.Open failed")

		if err = db.Ping(); err != nil {
			utils.CheckErr(err, "sql.Ping failed")
		}
		di.db = db
		utils.Debug("DB connection open!")
	}
	return di.db
}
Пример #7
0
// Find inactive user by hash
func (self *UserManager) FindInActiveByHash(hash string) *models.User {
	db := self.db
	user := new(models.User)
	stmt, err := db.Prepare("SELECT * FROM users WHERE hash = ? AND is_active = 0")
	utils.CheckErr(err, nil)
	err = stmt.QueryRow(hash).Scan(&user.Id, &user.FullName, &user.Address, &user.Phone, &user.Email, &user.Password, &user.Hash, &user.IsActive, &user.Token)
	if err != nil && err != sql.ErrNoRows {
		utils.CheckErr(err, nil)
	}

	if err == sql.ErrNoRows {
		return nil
	} else {
		return user
	}
}
Пример #8
0
// Find user by id
func (self *UserManager) FindById(id interface{}) *models.User {
	db := self.db
	user := new(models.User)
	stmt, err := db.Prepare("SELECT * FROM users WHERE id =  ?")
	utils.CheckErr(err, nil)
	err = stmt.QueryRow(id).Scan(&user.Id, &user.FullName, &user.Address, &user.Phone, &user.Email, &user.Password, &user.Hash, &user.IsActive, &user.Token)

	if err != nil && err != sql.ErrNoRows {
		utils.CheckErr(err, nil)
	}

	if err == sql.ErrNoRows {
		return nil
	} else {
		return user
	}
}
Пример #9
0
// Find all users
func (self *UserManager) FindAll() []*models.User {

	db := self.db
	rows, err := db.Query("SELECT * FROM users")
	utils.CheckErr(err, nil)
	defer rows.Close()

	users := make([]*models.User, 0)
	for rows.Next() {
		user := new(models.User)
		err = rows.Scan(&user.Id, &user.FullName, &user.Address, &user.Phone, &user.Email, &user.Password, &user.Hash, &user.IsActive, &user.Token)
		utils.CheckErr(err, nil)
		users = append(users, user)
	}
	if err = rows.Err(); err != nil {
		utils.CheckErr(err, nil)
	}

	return users
}
Пример #10
0
// Find active user by token
func (self *AuthManager) FindActiveByToken(token string) (*models.User, error) {
	db := self.db
	user := new(models.User)
	stmt, err := db.Prepare("SELECT * FROM users WHERE token = ? AND is_active = 1")
	utils.CheckErr(err, nil)
	err = stmt.QueryRow(token).Scan(&user.Id, &user.FullName, &user.Address, &user.Phone, &user.Email, &user.Password, &user.Hash, &user.IsActive, &user.Token)
	if err != nil {
		return nil, err
	} else {
		return user, nil
	}
}