daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Oct 21st, 2018 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // model.go
  2.  
  3. package main
  4.  
  5. import (
  6.     "database/sql"
  7.     "fmt"
  8. )
  9.  
  10. type user struct {
  11.     ID       int    `json:"id"`
  12.     UserName string `json:"username"`
  13.     Email    string `json:"email"`
  14.     Password string `json:"password"`
  15.     Points   string `json:"points"`
  16. }
  17.  
  18. func (u *user) getUser(db *sql.DB) error {
  19.     statement := fmt.Sprintf("SELECT username, email, points FROM users WHERE id=%d", u.ID)
  20.     return db.QueryRow(statement).Scan(&u.UserName, &u.Email, &u.Points)
  21. }
  22.  
  23. func (u *user) updateUser(db *sql.DB) error {
  24.     statement := fmt.Sprintf("UPDATE users SET username='%s', email='%s', password='%s', points='%s'WHERE id=%d", u.UserName, u.Email, u.Password, u.Points, u.ID)
  25.     _, err := db.Exec(statement)
  26.     return err
  27. }
  28.  
  29. func (u *user) deleteUser(db *sql.DB) error {
  30.     statement := fmt.Sprintf("DELETE FROM users WHERE id=%d", u.ID)
  31.     _, err := db.Exec(statement)
  32.     return err
  33. }
  34.  
  35. func (u *user) checkEmail(db *sql.DB) error {
  36.     statement := fmt.Sprintf("SELECT id FROM users WHERE email='%s'", u.Email)
  37.     return db.QueryRow(statement).Scan(&u.ID)
  38. }
  39.  
  40. func (u *user) createUser(db *sql.DB) error {
  41.     statement := fmt.Sprintf("INSERT INTO users(username, email, password, points) VALUES('%s', '%s', '%s', '%s')", u.UserName, u.Email, u.Password, u.Points)
  42.     _, err := db.Exec(statement)
  43.  
  44.     if err != nil {
  45.         return err
  46.     }
  47.  
  48.     err = db.QueryRow("SELECT LAST_INSERT_ID()").Scan(&u.ID)
  49.  
  50.     if err != nil {
  51.         return err
  52.     }
  53.  
  54.     return nil
  55. }
  56.  
  57. func getUsers(db *sql.DB, start, count int) ([]user, error) {
  58.     statement := fmt.Sprintf("SELECT id, username, email, points FROM users LIMIT %d OFFSET %d", count, start)
  59.     rows, err := db.Query(statement)
  60.  
  61.     if err != nil {
  62.         return nil, err
  63.     }
  64.  
  65.     defer rows.Close()
  66.  
  67.     users := []user{}
  68.  
  69.     for rows.Next() {
  70.         var u user
  71.         if err := rows.Scan(&u.ID, &u.UserName, &u.Email, &u.Points); err != nil {
  72.             return nil, err
  73.         }
  74.         users = append(users, u)
  75.     }
  76.  
  77.     return users, nil
  78. }
  79.  
  80.  
  81. // user auth
  82.  
  83. func (u *user) authUser(db *sql.DB) error {
  84.     statement := fmt.Sprintf("SELECT id, username, email FROM users WHERE email='%s' AND password='%s'", u.Email, u.Password)
  85.     return db.QueryRow(statement).Scan(&u.ID, &u.UserName, &u.Email)
  86. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top