From 97f36a08c7a5e3bd7921a26af89eee8ad9b3e3d7 Mon Sep 17 00:00:00 2001 From: Andinus Date: Fri, 27 Mar 2020 18:32:10 +0530 Subject: Add AddUser func --- user/adduser.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 user/adduser.go (limited to 'user') diff --git a/user/adduser.go b/user/adduser.go new file mode 100644 index 0000000..873454b --- /dev/null +++ b/user/adduser.go @@ -0,0 +1,42 @@ +package user + +import ( + "log" + "time" + + "tildegit.org/andinus/perseus/storage/sqlite3" +) + +// AddUser adds the user to record. +func (u *User) AddUser(db *sqlite3.DB) error { + // Acquire write lock on the database. + db.Mu.Lock() + defer db.Mu.Unlock() + + // Start the transaction + tx, err := db.Conn.Begin() + if err != nil { + log.Printf("user/adduser.go: %s\n", + "failed to begin transaction") + return err + } + + usrStmt, err := db.Conn.Prepare(` +INSERT INTO users(id, username, password, regTime) values(?, ?, ?, ?)`) + if err != nil { + log.Printf("user/adduser.go: %s\n", + "failed to prepare statement") + return err + } + defer usrStmt.Close() + + _, err = usrStmt.Exec(u.id, u.username, u.password, time.Now().UTC()) + if err != nil { + log.Printf("user/adduser.go: %s\n", + "failed to execute statement") + return err + } + + tx.Commit() + return err +} -- cgit 1.4.1-2-gfad0