Browse Source

Users module, load user data

Vova Tkach 6 years ago
parent
commit
c299310f7b
2 changed files with 46 additions and 0 deletions
  1. 30 0
      modules/module_users.go
  2. 16 0
      utils/utils.go

+ 30 - 0
modules/module_users.go

@@ -78,6 +78,36 @@ func (this *Modules) RegisterModule_Users() *Module {
 				A_email:      "",
 			}
 
+			if wrap.CurrSubModule == "modify" {
+				if len(wrap.UrlArgs) != 3 {
+					return "", "", ""
+				}
+				if !utils.IsNumeric(wrap.UrlArgs[2]) {
+					return "", "", ""
+				}
+				err := wrap.DB.QueryRow(`
+					SELECT
+						id,
+						first_name,
+						last_name,
+						email
+					FROM
+						users
+					WHERE
+						id = ?
+					LIMIT 1;`,
+					utils.StrToInt(wrap.UrlArgs[2]),
+				).Scan(
+					&data.A_id,
+					&data.A_first_name,
+					&data.A_last_name,
+					&data.A_email,
+				)
+				if err != nil {
+					return "", "", ""
+				}
+			}
+
 			content += builder.DataForm(wrap, []builder.DataFormField{
 				{
 					Kind:  builder.DFKHidden,

+ 16 - 0
utils/utils.go

@@ -8,6 +8,7 @@ import (
 	"net/http"
 	"os"
 	"regexp"
+	"strconv"
 	"strings"
 
 	"golang-fave/assets"
@@ -46,6 +47,13 @@ func IsValidEmail(email string) bool {
 	return regexpe.MatchString(email)
 }
 
+func IsNumeric(str string) bool {
+	if _, err := strconv.Atoi(str); err == nil {
+		return true
+	}
+	return false
+}
+
 func FixPath(path string) string {
 	newPath := strings.TrimSpace(path)
 	if len(newPath) <= 0 {
@@ -166,3 +174,11 @@ func UrlToArray(url string) []string {
 func IntToStr(num int) string {
 	return fmt.Sprintf("%d", num)
 }
+
+func StrToInt(str string) int {
+	num, err := strconv.Atoi(str)
+	if err == nil {
+		return num
+	}
+	return 0
+}