user.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package model
  2. type User struct {
  3. ID uint64 `json:"id"`
  4. Email string `json:"email"`
  5. Password string `json:"password"`
  6. Name string `json:"name"`
  7. }
  8. func CreateUser(user *User, db *Db) error {
  9. statement := "INSERT INTO users (email, password, name) values ($1, $2, $3)"
  10. _, err := db.conn.Exec(statement, user.Email, user.Password, user.Name)
  11. return err
  12. }
  13. func CheckEmail(email string, user *User, db *Db) bool {
  14. statement := "SELECT id, name, email, password FROM users WHERE email=$1 LIMIT 1"
  15. rows, err := db.conn.Query(statement, email)
  16. if err != nil {
  17. return false
  18. }
  19. for rows.Next() {
  20. err = rows.Scan(&user.ID, &user.Name, &user.Email, &user.Password)
  21. if err != nil {
  22. return false
  23. }
  24. }
  25. return true
  26. }
  27. func GetUser(email string, db *Db) (*User, error) {
  28. statement := "SELECT id, name, email FROM users WHERE email=$1 LIMIT 1"
  29. var user User
  30. rows, err := db.conn.Query(statement, email)
  31. for rows.Next() {
  32. err = rows.Scan(&user.ID, &user.Name, &user.Email)
  33. if err != nil {
  34. return nil, err
  35. }
  36. }
  37. return &user, nil
  38. }