add user authentication

This commit is contained in:
2026-03-16 20:18:37 +03:00
parent 1baa6a4806
commit 3a8bc6df2d
6 changed files with 175 additions and 7 deletions

9
model/model.go Normal file
View File

@@ -0,0 +1,9 @@
package model
import "time"
type Model struct {
ID int64 `json:"id" db:"id"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
}

50
model/user.go Normal file
View File

@@ -0,0 +1,50 @@
package model
import "github.com/jmoiron/sqlx"
type User struct {
Model
Email string `json:"email" db:"email"`
Password string `json:"-" db:"password"`
}
func (u *User) Create(db *sqlx.DB) error {
res, err := db.NamedExec("INSERT INTO users (email, password) VALUES (:email, :password)", map[string]any{
"email": u.Email,
"password": u.Password,
})
if err != nil {
return err
}
u.ID, _ = res.LastInsertId()
return nil
}
func (u *User) FindByID(db *sqlx.DB) error {
row := db.QueryRowx("SELECT * FROM users WHERE id = ?", u.ID)
if row.Err() != nil {
return row.Err()
}
if err := row.StructScan(u); err != nil {
return err
}
return nil
}
func (u *User) FindByEmail(db *sqlx.DB) error {
row := db.QueryRowx("SELECT * FROM users WHERE email = ?", u.Email)
if row.Err() != nil {
return row.Err()
}
if err := row.StructScan(u); err != nil {
return err
}
return nil
}