Esempio n. 1
0
File: init.go Progetto: henrydo/ags
func updateAdminUser() {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
		fmt.Println("dbHandler failed", err)
	}
	defer dbHandler.Close()
	updateAdminSql := fmt.Sprintf("UPDATE user set name=?, pass=?, email=? WHERE type=0")
	dbHandler.Exec(updateAdminSql, config.GetValue("AdminName"), config.GetValue("AdminPassword"), config.GetValue("AdminEmail"))
}
Esempio n. 2
0
func dbCreateArticle(article *Article) int {
	log.Debug("try to create article %v", article)

	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	stmt, err := dbHandler.Prepare("INSERT INTO article (title, navname, cover_photo, intro, content, create_by_user_id, last_modify_time) VALUES (?,?,?,?,?,?, datetime('now','localtime','utc'))")
	if err != nil {
		log.Error("%v", err)
		return http.StatusInternalServerError
	}
	defer stmt.Close()

	r, err := stmt.Exec(article.Title, article.NavName, article.CoverPhoto, article.Intro, article.Content, article.UserId)
	if err != nil {
		log.Error("%v", err)
		return http.StatusBadRequest
	}
	id, _ := r.LastInsertId()
	article.Id = strconv.FormatInt(id, 10)

	return http.StatusOK
}
Esempio n. 3
0
File: users.go Progetto: henrydo/ags
func dbSearchCustomers(t string, p int) ([]Customer, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	offset := customerPageLimit * (p - 1)
	querySql := fmt.Sprintf("select id, name, cover_photo, phone, desc, email from user where type=1 and (name like '%%%s%%' or phone like '%%%s%%' or email like '%%%s%%') order by id limit %d offset %d", t, t, t, customerPageLimit, offset)

	stmt, err := dbHandler.Prepare(querySql)
	if err != nil {
		log.Debug("querySql: %s", querySql)
		log.Error("Prepare failed : %v", err)
		return nil, http.StatusInternalServerError
	}

	defer stmt.Close()
	rows, err := stmt.Query()
	if err != nil {
		log.Fatal("Query customers failed, something changed on db schema? : %v ", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	customers := make([]Customer, 0)
	for rows.Next() {
		var customerId sql.NullInt64
		var name, coverPhoto, phone, desc, email sql.NullString
		rows.Scan(&customerId, &name, &coverPhoto, &phone, &desc, &email)
		customers = append(customers, Customer{strconv.FormatInt(customerId.Int64, 10), name.String, coverPhoto.String, desc.String, phone.String, email.String, nil})
	}
	return customers, http.StatusOK

}
Esempio n. 4
0
File: users.go Progetto: henrydo/ags
func dbVerifyRecover(magic string) (int, int64) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	var getSql = "SELECT u.id FROM user u, user_recover_pass urp WHERE u.email=urp.email AND urp.magic=?"
	var value sql.NullInt64
	err = dbHandler.QueryRow(getSql, magic).Scan(&value)
	if err != nil {
		if err == sql.ErrNoRows {
			log.Info("No magic found for %s", magic)
			return http.StatusNotFound, 0
		} else {
			log.Error("DB query failed: %v", err)
			return http.StatusInternalServerError, 0
		}
	}

	var delSql = "DELETE from user_recover_pass WHERE magic=?"
	_, err = dbHandler.Exec(delSql, magic)
	if err != nil {
		log.Error("SQL: %s", delSql)
		return http.StatusInternalServerError, 0
	}
	return http.StatusOK, value.Int64
}
Esempio n. 5
0
func dbGetAllArticles() ([]Article, int) {
	log.Debug("get all articles")
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	stmt, err := dbHandler.Prepare("SELECT a.id, a.title, a.navname,a.cover_photo, a.intro, a.content, a.create_by_user_id, u.name, a.create_time, a.last_modify_time from article a, user u WHERE a.create_by_user_id=u.id ORDER BY a.id DESC")
	if err != nil {
		log.Error("%v", err)
		return nil, http.StatusInternalServerError
	}
	defer stmt.Close()
	rows, err := stmt.Query()
	if err != nil {
		log.Fatal("%v", err)
		return nil, http.StatusInternalServerError
	}
	defer rows.Close()

	allarticles := make([]Article, 10)
	for rows.Next() {
		var title, navname, cover_photo, intro, content, username sql.NullString
		var articleid, userid sql.NullInt64
		var createtime, modifytime time.Time
		rows.Scan(&articleid, &title, &navname, &cover_photo, &intro, &content, &userid, &username, &createtime, &modifytime)

		allarticles = append(allarticles, Article{strconv.FormatInt(articleid.Int64, 10), title.String, navname.String, intro.String, content.String, userid.Int64, username.String, "", createtime.Format(timeLayout), modifytime.Format(timeLayout), cover_photo.String})
	}
	rows.Close()
	return allarticles, http.StatusOK
}
Esempio n. 6
0
File: users.go Progetto: henrydo/ags
func dbSaveCustomer(c *Customer) int {
	log.Debug("try to save user %v", c)
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	var userType = 1
	if len(c.Email) == 0 {
		userType = 2
	}
	stmt, err := dbHandler.Prepare("UPDATE user SET type=?, name=?, email=?, cover_photo=?, phone=?, desc=? WHERE id=?")
	if err != nil {
		log.Error("%v", err)
		return http.StatusInternalServerError
	}
	defer stmt.Close()

	_, err = stmt.Exec(userType, c.Name, c.Email, c.CoverPhoto, c.Phone, c.Desc, c.Id)
	if err != nil {
		log.Error("%v", err)
		return http.StatusBadRequest
	}
	return http.StatusOK
}
Esempio n. 7
0
//usertype: 1:customer, 2:visitor
func dbGetReviewboardDetail(usertype int, userid int64) ([]ReviewboardProduct, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	querySql := "SELECT id, product_id, product_navname, product_name, cover_photo, price FROM reviewboard WHERE customer_type=? AND customer_id=? "

	stmt, err := dbHandler.Prepare(querySql)
	if err != nil {
		log.Debug("querySql: %s", querySql)
		log.Error("Prepare failed: %v", err)
		return nil, http.StatusInternalServerError
	}
	defer stmt.Close()
	rows, err := stmt.Query(usertype, userid)
	if err != nil {
		log.Error("Query reviewboard detail failed, error : %v", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	ps := make([]ReviewboardProduct, 0)
	for rows.Next() {
		var id, product_id sql.NullInt64
		var product_name, product_navname, cover_photo sql.NullString
		var price sql.NullFloat64
		rows.Scan(&id, &product_id, &product_navname, &product_name, &cover_photo, &price)
		ps = append(ps, ReviewboardProduct{product_id.Int64, product_navname.String, product_name.String, cover_photo.String, price.Float64})
	}
	return ps, http.StatusOK
}
Esempio n. 8
0
func dbGetEnquireProducts(id string) []ReviewboardProduct {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	querySql := "SELECT id, product_id, product_navname, product_name, cover_photo, price FROM enquire_product WHERE enquire_id=?"

	stmt, err := dbHandler.Prepare(querySql)
	if err != nil {
		log.Debug("querySql: %s", querySql)
		log.Error("Prepare failed: %v", err)
		return nil
	}
	defer stmt.Close()
	rows, err := stmt.Query(id)
	if err != nil {
		log.Error("Query enquire products failed, error : %v", err)
		return nil
	}
	defer rows.Close()

	ps := make([]ReviewboardProduct, 0)
	for rows.Next() {
		var id, product_id sql.NullInt64
		var product_name, product_navname, cover_photo sql.NullString
		var price sql.NullFloat64
		rows.Scan(&id, &product_id, &product_navname, &product_name, &cover_photo, &price)
		ps = append(ps, ReviewboardProduct{product_id.Int64, product_navname.String, product_name.String, cover_photo.String, price.Float64})
	}
	return ps
}
Esempio n. 9
0
func dbSearchProducts(t string, p int) ([]Product, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	offset := productPageLimit * (p - 1)
	querySql := fmt.Sprintf("select id, nav_name, status, en_name, cover_photo, price from product WHERE en_name like '%%%s%%' order by id limit %d offset %d", t, productPageLimit, offset)

	stmt, err := dbHandler.Prepare(querySql)
	if err != nil {
		log.Debug("querySql: %s", querySql)
		log.Error("Prepare failed : %v", err)
		return nil, http.StatusInternalServerError
	}

	defer stmt.Close()
	rows, err := stmt.Query()
	if err != nil {
		log.Fatal("Query products failed, something changed on db schema? : %v ", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	products := make([]Product, 0)
	for rows.Next() {
		var productId, status sql.NullInt64
		var navName, enName, coverPhoto sql.NullString
		var price sql.NullFloat64
		rows.Scan(&productId, &navName, &status, &enName, &coverPhoto, &price)
		products = append(products, Product{productId.Int64, navName.String, status.Int64, enName.String, "", coverPhoto.String, "", "", price.Float64, 0, nil, nil, nil, nil, nil, nil})
	}
	return products, http.StatusOK
}
Esempio n. 10
0
File: users.go Progetto: henrydo/ags
func dbFindCustomersByCond(cond string) ([]Customer, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	sqlCond, limit, offset := buildSqlCond(cond)
	log.Debug("get customers for %s", cond)
	querySql := fmt.Sprintf("SELECT id, name, cover_photo, desc, phone, email FROM user WHERE type=1 AND %s LIMIT %d OFFSET %d ", sqlCond, limit, offset)

	stmt, err := dbHandler.Prepare(querySql)
	if err != nil {
		log.Debug("querySql: %s", querySql)
		log.Error("Prepare failed : %v", err)
		return nil, http.StatusInternalServerError
	}
	defer stmt.Close()
	rows, err := stmt.Query()
	if err != nil {
		log.Fatal("Query customers failed, something changed on db schema? : %v ", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	customers := make([]Customer, 0, limit)
	for rows.Next() {
		var customerId sql.NullInt64
		var name, coverPhoto, desc, phone, email sql.NullString
		rows.Scan(&customerId, &name, &coverPhoto, &desc, &phone, &email)
		customers = append(customers, Customer{strconv.FormatInt(customerId.Int64, 10), name.String, coverPhoto.String, "", phone.String, email.String, nil})
	}
	return customers, http.StatusOK
}
Esempio n. 11
0
File: users.go Progetto: henrydo/ags
func dbCreateCustomer(c *Customer) int {
	log.Debug("try to create user %v", c)
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	var userType = 1
	if len(c.Email) == 0 {
		userType = 2
	}
	stmt, err := dbHandler.Prepare("INSERT INTO user (type, name, email, cover_photo, phone, desc) VALUES (?,?,?,?,?,?)")
	if err != nil {
		log.Error("%v", err)
		return http.StatusInternalServerError
	}
	defer stmt.Close()

	_, err = stmt.Exec(userType, c.Name, c.Email, c.CoverPhoto, c.Phone, c.Desc)
	if err != nil {
		log.Error("%v", err)
		return http.StatusBadRequest
	}
	return http.StatusOK
}
Esempio n. 12
0
func dbSearchEnquires(t string, p int) ([]Enquire, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	offset := enquirePageLimit * (p - 1)
	querySql := fmt.Sprintf("select id, status, customer_id, customer_name, subject, message, followup, create_time, last_modify_time from enquires where subject like '%%%s%%' or customer_name like '%%%s%%' order by id desc limit %d offset %d", t, t, enquirePageLimit, offset)

	stmt, err := dbHandler.Prepare(querySql)
	if err != nil {
		log.Debug("querySql: %s", querySql)
		log.Error("Prepare failed : %v", err)
		return nil, http.StatusInternalServerError
	}

	defer stmt.Close()
	rows, err := stmt.Query()
	if err != nil {
		log.Fatal("Query products failed, something changed on db schema? : %v ", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	es := make([]Enquire, 0)
	for rows.Next() {
		var enquire_id, status, customer_id sql.NullInt64
		var customer_name, subject, message, followup sql.NullString
		var create_time, last_modify_time time.Time
		rows.Scan(&enquire_id, &status, &customer_id, &customer_name, &subject, &message, &followup, &create_time, &last_modify_time)
		es = append(es, Enquire{auth.Encode(strconv.FormatInt(enquire_id.Int64, 10)), status.Int64, customer_id.Int64, customer_name.String, subject.String, message.String, 1, followup.String, nil, create_time.Format(timeLayout), last_modify_time.Format(timeLayout)})
	}
	return es, http.StatusOK
}
Esempio n. 13
0
func dbAddProduct(p *Product) int {
	log.Debug("try to add new product %v", p)
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	if p.Photos != nil && len(p.Photos) > 0 {
		p.CoverPhoto = p.Photos[0]
	}

	tx, err := dbHandler.Begin()
	insertSql := "INSERT INTO product (nav_name, status, en_name, cn_name, cover_photo, introduction, spec, price, discount) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
	r, err := dbHandler.Exec(insertSql, p.NavName, p.Status, p.EnName, p.CnName, p.CoverPhoto, p.Introduction, p.Spec, p.Price, p.Discount)
	if err != nil {
		tx.Rollback()
		log.Error("SQL: %s, err: %v", insertSql, err)
		return http.StatusInternalServerError
	}
	id, _ := r.LastInsertId()

	stmt, err := dbHandler.Prepare("INSERT INTO product_photo (product_id, url) VALUES (?,?)")
	if err != nil {
		tx.Rollback()
		log.Error("prepare failed : %v", err)
		return http.StatusInternalServerError
	}
	defer stmt.Close()

	for _, url := range p.Photos {
		_, err = stmt.Exec(id, url)
		if err != nil {
			tx.Rollback()
			log.Error("insert product_photo failed: %v", err)
			return http.StatusInternalServerError
		}
	}

	stmt2, err := dbHandler.Prepare("INSERT INTO product_saleurl (product_id, url) VALUES (?,?)")
	if err != nil {
		tx.Rollback()
		log.Error("prepare product_saleurl failed : %v", err)
		return http.StatusInternalServerError
	}
	defer stmt2.Close()

	for _, url := range p.SaleURL {
		_, err = stmt2.Exec(id, url)
		if err != nil {
			tx.Rollback()
			log.Error("insert product_photo failed: %v", err)
			return http.StatusInternalServerError
		}
	}
	tx.Commit()

	return http.StatusOK
}
Esempio n. 14
0
func dbGetPageArticles(pagenumber int64) ([]Article, int) {
	log.Debug("get page articles for %d", pagenumber)

	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	pageArticlesLimit, err := strconv.ParseInt(config.GetValue("DealsPerPage"), 10, 64)
	if err != nil {
		return nil, http.StatusInternalServerError
	}
	if pagenumber <= 0 {
		return nil, http.StatusBadRequest
	}
	offset := (pagenumber - 1) * pageArticlesLimit

	stmt, err := dbHandler.Prepare("SELECT a.id, a.title, a.navname, a.cover_photo, a.intro,  a.create_by_user_id, u.name, a.create_time, a.last_modify_time from article a, user u WHERE a.create_by_user_id=u.id ORDER BY a.id DESC  limit ? offset ?")
	if err != nil {
		log.Error("%v", err)
		return nil, http.StatusInternalServerError
	}
	defer stmt.Close()
	rows, err := stmt.Query(pageArticlesLimit, offset)
	if err != nil {
		log.Fatal("%v", err)
		return nil, http.StatusInternalServerError
	}
	defer rows.Close()

	allarticles := make([]Article, 0)
	for rows.Next() {
		var title, navname, cover_photo, intro, username sql.NullString
		var articleid, userid sql.NullInt64
		var createtime, modifytime time.Time
		rows.Scan(&articleid, &title, &navname, &cover_photo, &intro, &userid, &username, &createtime, &modifytime)

		allarticles = append(allarticles, Article{strconv.FormatInt(articleid.Int64, 10), title.String, navname.String, intro.String, "", userid.Int64, username.String, "", createtime.Format(timeLayout), modifytime.Format(timeLayout), cover_photo.String})
	}
	rows.Close()
	return allarticles, http.StatusOK
}
Esempio n. 15
0
File: init.go Progetto: henrydo/ags
func addAdminUser() {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
		fmt.Println("dbHandler failed", err)
	}
	defer dbHandler.Close()
	addAdminSql := fmt.Sprintf("INSERT INTO user (name, type, pass, email) VALUES ('%s', 0, '%s', '%s')", config.GetValue("AdminName"), config.GetValue("AdminPassword"), config.GetValue("AdminEmail"))
	dbHandler.Exec(addAdminSql)
}
Esempio n. 16
0
File: users.go Progetto: henrydo/ags
func buildSqlCond(cond string) (string, int, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	var offset = 0
	var limit = 100
	conds := strings.Split(cond, "&")
	if len(conds) == 0 || len(conds) > 10 {
		return "id>0", limit, offset
	}

	var sqlString = make([]string, 0, 10)
	for _, c := range conds {
		v := strings.Split(c, "=")
		if len(v) != 2 {
			return "id>0", limit, offset
		}
		switch v[0] {
		case "id":
			sqlString = append(sqlString, "id="+v[1])
		case "name":
			sqlString = append(sqlString, fmt.Sprintf("name like '%%%s%%'", v[1]))
		case "desc":
			sqlString = append(sqlString, fmt.Sprintf("desc like '%%%s%%'", v[1]))
		case "phone":
			sqlString = append(sqlString, fmt.Sprintf("phone like '%%%s%%'", v[1]))
		case "email":
			sqlString = append(sqlString, fmt.Sprintf("email like '%%%s%%'", v[1]))
		case "offset":
			o, err := strconv.Atoi(v[1])
			if err != nil {
				offset = 0
			} else {
				offset = o
			}

		case "limit":
			l, err := strconv.Atoi(v[1])
			if err != nil {
				limit = 100
			} else {
				limit = l
			}

		}
	}

	if len(sqlString) == 0 {
		return "id>0", limit, offset
	}
	return strings.Join(sqlString, " AND "), limit, offset
}
Esempio n. 17
0
func dbGetPageDeals(pagenumber int64) ([]Product, int) {
	log.Debug("get page deals for %d", pagenumber)
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	pageDealsLimit, err := strconv.ParseInt(config.GetValue("DealsPerPage"), 10, 64)
	if err != nil {
		return nil, http.StatusInternalServerError
	}
	if pagenumber <= 0 {
		return nil, http.StatusBadRequest
	}

	//show latest deals only
	//offset := (pagenumber - 1) * pageDealsLimit
	offset := 0
	stmt, err := dbHandler.Prepare("SELECT id, nav_name, status, en_name, cn_name, cover_photo, price, discount FROM product WHERE status!=0 ORDER BY id desc limit ? offset ?")
	if err != nil {
		log.Error("Prepare to get page deal failed : %v", err)
		return nil, http.StatusInternalServerError
	}
	defer stmt.Close()
	rows, err := stmt.Query(pageDealsLimit, offset)
	if err != nil {
		log.Fatal("Query page deals failed: %v ", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	products := make([]Product, 0)
	for rows.Next() {
		var productId, status sql.NullInt64
		var navName, enName, cnName, coverPhoto sql.NullString
		var price, discount sql.NullFloat64
		rows.Scan(&productId, &navName, &status, &enName, &cnName, &coverPhoto, &price, &discount)
		products = append(products, Product{productId.Int64, navName.String, status.Int64, enName.String, cnName.String, coverPhoto.String, "", "", price.Float64, discount.Float64, nil, nil, nil, nil, nil, nil})
	}
	return products, http.StatusOK
}
Esempio n. 18
0
func dbGetTotalPageNumber() (float64, int) {
	log.Debug("get total page number")

	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	pageArticlesLimit, err := strconv.ParseInt(config.GetValue("DealsPerPage"), 10, 64)
	if err != nil {
		return 1, http.StatusOK
	}

	var n sql.NullFloat64
	queryLogSql := "SELECT count(*) FROM article"
	dbHandler.QueryRow(queryLogSql).Scan(&n)

	return math.Ceil(float64(n.Float64 / float64(pageArticlesLimit))), http.StatusOK
}
Esempio n. 19
0
func dbFollowupEnquire(e *Enquire) int {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	updateSql := "UPDATE enquires SET employee_id=?, status=?, followup=? where id=?"
	_, err = dbHandler.Exec(updateSql, e.EmployeeId, e.Status, e.Followup, e.Id)
	if err != nil {
		log.Error("Sql: %s", updateSql)
		log.Error("DB followup enquire failed, %v", err)
		return http.StatusInternalServerError
	}
	return http.StatusOK
}
Esempio n. 20
0
File: users.go Progetto: henrydo/ags
func dbInsertRecoverInfo(email, magic string) int {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	var setSql = "INSERT OR REPLACE INTO user_recover_pass (email, magic) VALUES (?,?) "
	_, err = dbHandler.Exec(setSql, email, magic)
	if err != nil {
		log.Error("SQL: %s", setSql)
		log.Error("DB exec failed, insert recover info, email: %s, magic %s, failed : %v", email, magic, err)
		return http.StatusInternalServerError
	}
	return http.StatusOK
}
Esempio n. 21
0
File: users.go Progetto: henrydo/ags
func dbUpdateUserType(user *User, user_type int) int {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	var setSql = "UPDATE user set type=? WHERE id=? "
	_, err = dbHandler.Exec(setSql, user_type, user.Id)
	if err != nil {
		log.Error("SQL: %s", setSql)
		log.Error("DB exec failed : %v", err)
		return http.StatusInternalServerError
	}
	return http.StatusOK
}
Esempio n. 22
0
File: share.go Progetto: henrydo/ags
func setSessionUploadFileUrls(session, file_urls string) int {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	var setSql = "INSERT OR REPLACE INTO session_upload_files (session, urls) VALUES (?,?) "
	_, err = dbHandler.Exec(setSql, session, file_urls)
	if err != nil {
		log.Error("SQL: %s", setSql)
		log.Error("DB exec failed, insert session: %s, urls %s, failed : %v", session, file_urls, err)
		return http.StatusInternalServerError
	}
	return http.StatusOK
}
Esempio n. 23
0
func dbGetAllProducts() ([]Product, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	var getSql = "SELECT count(*) FROM product ORDER BY id DESC "
	var product_count sql.NullInt64
	err = dbHandler.QueryRow(getSql).Scan(&product_count)
	if err != nil {
		if err == sql.ErrNoRows {
			log.Info("sql error")
			return nil, http.StatusInternalServerError
		} else {
			log.Error("DB query failed: %v", err)
			return nil, http.StatusInternalServerError
		}
	}
	if product_count.Int64 == 0 {
		log.Error("No product in db")
		return nil, http.StatusNotFound
	}

	stmt, err := dbHandler.Prepare("SELECT id, nav_name, status, en_name, cn_name, cover_photo, introduction, spec, price, discount FROM product ORDER BY id desc ")
	if err != nil {
		log.Error("Prepare all product failed : %v", err)
		return nil, http.StatusInternalServerError
	}
	defer stmt.Close()
	rows, err := stmt.Query()
	if err != nil {
		log.Fatal("Query all product failed, something changed on db schema? : %v ", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	products := make([]Product, 0, product_count.Int64)
	for rows.Next() {
		var productId, status sql.NullInt64
		var navName, enName, cnName, coverPhoto, introduction, spec sql.NullString
		var price, discount sql.NullFloat64
		rows.Scan(&productId, &navName, &status, &enName, &cnName, &coverPhoto, &introduction, &spec, &price, &discount)
		products = append(products, Product{productId.Int64, navName.String, status.Int64, enName.String, cnName.String, coverPhoto.String, introduction.String, spec.String, price.Float64, discount.Float64, nil, nil, nil, nil, nil, nil})
	}
	return products, http.StatusOK
}
Esempio n. 24
0
func dbGetEnquire(e *Enquire, userId int64) int {
	log.Debug("get enquire detail for %s", e.Id)
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	querySql := "SELECT id, status, customer_id, customer_name,  subject, message, employee_id, followup,  create_time, last_modify_time FROM enquires WHERE id=?"
	var enquire_id, status, customer_id, employee_id sql.NullInt64
	var customer_name, subject, message, followup sql.NullString
	var create_time, last_modify_time time.Time
	err = dbHandler.QueryRow(querySql, e.Id).Scan(&enquire_id, &status, &customer_id, &customer_name, &subject, &message, &employee_id, &followup, &create_time, &last_modify_time)
	if err != nil {
		if err == sql.ErrNoRows {
			log.Info("No enquire found for %s", e.Id)
			return http.StatusNotFound
		} else {
			log.Debug("sql : %s", querySql)
			log.Error("DB query failed: %v", err)
			return http.StatusInternalServerError
		}
	}
	if userId != customer_id.Int64 {
		u := users.DbFindUser(strconv.FormatInt(userId, 10))
		if u == nil || (u.Type == 1 || u.Type == 2) {
			return http.StatusForbidden
		}
	}

	if !status.Valid {
		return http.StatusNotFound
	} else {
		e.Products = dbGetEnquireProducts(e.Id)
		e.Status = status.Int64
		e.CustomerId = customer_id.Int64
		e.CustomerName = customer_name.String
		e.Subject = subject.String
		e.Message = message.String
		e.EmployeeId = employee_id.Int64
		e.Followup = followup.String
		e.CreateTime = create_time.Format(timeLayout)
		e.ModifyTime = last_modify_time.Format(timeLayout)

		return http.StatusOK
	}
}
Esempio n. 25
0
func dbAddProductToReviewboard(usertype int, userid int64, p *ReviewboardProduct) int {

	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	insertSql := "INSERT INTO reviewboard (customer_type, customer_id, product_id, product_navname, product_name, cover_photo, price) VALUES (?, ?, ?, ?, ?,  ?, ?) "
	_, err = dbHandler.Exec(insertSql, usertype, userid, p.Id, p.NavName, p.Name, p.CoverPhoto, p.Price)
	if err != nil {
		log.Error("Sql: %s", insertSql)
		log.Error("DB Insert product to reviewboard failed, %v", err)
		return http.StatusInternalServerError
	}
	return http.StatusOK
}
Esempio n. 26
0
File: users.go Progetto: henrydo/ags
func dbSearchCustomersCount(t string) (int64, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	querySql := fmt.Sprintf("select count(id) from user where type=1 and (name like '%%%s%%' or phone like '%%%s%%' or email like '%%%s%%') ", t, t, t)
	var n sql.NullInt64
	err = dbHandler.QueryRow(querySql).Scan(&n)
	if err != nil {
		log.Debug("sql : %s", querySql)
		log.Error("DB query failed: %v", err)
		return 0, http.StatusInternalServerError
	}
	return n.Int64, http.StatusOK
}
Esempio n. 27
0
func dbGenDataSaleAmount(reportData *ReportData) (*ReportData, int) {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	var querySql string
	switch reportData.Timeframe {
	case "0":
		querySql = "SELECT sum(paid_amount), date(create_time) from orders WHERE status>0 AND  create_time >= (SELECT date(julianday(date('now'))-7)) AND create_time <= (SELECT date(julianday(date('now')))) group by date(create_time)"
	case "1":
		querySql = "SELECT sum(paid_amount), date(create_time) from orders WHERE status>0 AND create_time >= (SELECT date(julianday(date('now'))-30)) AND create_time <= (SELECT date(julianday(date('now')))) group by date(create_time)"
	case "2":
		querySql = "SELECT sum(paid_amount), date(create_time) from orders WHERE status>0 AND create_time >= (SELECT date(julianday(date('now'))-180)) AND create_time <= (SELECT date(julianday(date('now')))) group by date(create_time)"
	case "3":
		querySql = "SELECT sum(paid_amount), date(create_time) from orders WHERE status>0 group by date(create_time)"

	}

	stmt, err := dbHandler.Prepare(querySql)
	if err != nil {
		log.Debug("querySql: %s", querySql)
		log.Error("Prepare failed : %v", err)
		return nil, http.StatusInternalServerError
	}
	defer stmt.Close()
	rows, err := stmt.Query()
	if err != nil {
		log.Fatal("Query orders failed, something changed on db schema? : %v ", err)
		return nil, http.StatusNotFound
	}
	defer rows.Close()

	for rows.Next() {
		//var id_count sql.NullInt64
		var saleamount sql.NullFloat64
		var date_day sql.NullString
		rows.Scan(&saleamount, &date_day)
		reportData.Xvalues = append(reportData.Xvalues, date_day.String)
		reportData.Yvalues = append(reportData.Yvalues, saleamount.Float64)
	}

	return reportData, http.StatusOK
}
Esempio n. 28
0
File: users.go Progetto: henrydo/ags
func dbFindCustomer(c *Customer) int {
	log.Debug("get customer detail for %d", c.Id)
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	querySql := "SELECT id, name, cover_photo, desc, phone, email FROM user WHERE type in (1,2) AND id=? "
	var id sql.NullInt64
	var name, coverPhoto, desc, phone, email sql.NullString
	err = dbHandler.QueryRow(querySql, c.Id).Scan(&id, &name, &coverPhoto, &desc, &phone, &email)
	if err != nil {
		if err == sql.ErrNoRows {
			log.Info("No customer found for %d", c.Id)
			return http.StatusNotFound
		} else {
			log.Debug("sql : %s", querySql)
			log.Error("DB query failed: %v", err)
			return http.StatusInternalServerError
		}
	}

	c.Name = name.String
	c.CoverPhoto = coverPhoto.String
	c.Desc = desc.String
	c.Phone = phone.String
	c.Email = email.String

	queryLogSql := "SELECT operation_type, operation_detail, operation_time FROM user_log WHERE user_id=? ORDER BY id DESC LIMIT 100"
	rows, err := dbHandler.Query(queryLogSql, c.Id)
	defer rows.Close()

	logs := make([]CustomerLog, 0, 100)
	for rows.Next() {
		var operation_type, operation_detail sql.NullString
		var operation_time time.Time
		rows.Scan(&operation_type, &operation_detail, &operation_time)
		logs = append(logs, CustomerLog{c.Id, operation_type.String, operation_detail.String, operation_time.Format(timeLayout)})
	}
	c.Logs = logs

	return http.StatusOK
}
Esempio n. 29
0
File: users.go Progetto: henrydo/ags
func GetUserAddr(userid int64) *UserAddress {
	dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile"))
	if err != nil {
		log.Fatal("%v", err)
	}
	defer dbHandler.Close()

	querySql := "SELECT id, receiver, address, city, province, postal, country, phone from user_address where is_default=1 and user_id=? "
	var uaid sql.NullInt64
	var receiver, address, city, province, postal, country, phone sql.NullString
	err = dbHandler.QueryRow(querySql, userid).Scan(&uaid, &receiver, &address, &city, &province, &postal, &country, &phone)
	if err != nil {
		log.Error("Sql: %s", querySql)
		log.Error("DB choose shipping failed, %v", err)
		return nil
	}

	return &UserAddress{strconv.FormatInt(uaid.Int64, 10), receiver.String, address.String, city.String, province.String, postal.String, country.String, phone.String, 1}
}
Esempio n. 30
0
File: init.go Progetto: henrydo/ags
func RegService() {
	config.InitConfigs("data/ags.config")
	auth.SetSysMagicNumber([]byte(config.GetValue("SysMagicNumber")))
	inited := config.IsConfigInited()
	if !inited {
		createDb()
		addAdminUser()
		config.SetValue("dbInited", "Y")
	} else {
		updateAdminUser()
	}

	users.Register()
	articles.Register()
	share.Register()
	products.Register()
	enquires.Register()
	reports.Register()
}