add user db functions

This commit is contained in:
vinchent 2024-08-23 14:10:44 +02:00
parent 1971169e2f
commit d16793c01c

View File

@ -150,7 +150,7 @@ func (m *DBModel) InsertTransaction(txn Transaction) (int, error) {
(amount, currency, last_four, expiry_month, expiry_year, (amount, currency, last_four, expiry_month, expiry_year,
payment_intent, payment_method, bank_return_code, payment_intent, payment_method, bank_return_code,
transaction_status_id, created_at, updated_at) transaction_status_id, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
result, err := m.DB.ExecContext(ctx, stmt, result, err := m.DB.ExecContext(ctx, stmt,
txn.Amount, txn.Amount,
@ -183,7 +183,7 @@ func (m *DBModel) InsertOrder(order Order) (int, error) {
stmt := `INSERT INTO orders stmt := `INSERT INTO orders
(widget_id, transaction_id, customer_id, status_id, quantity, (widget_id, transaction_id, customer_id, status_id, quantity,
amount, created_at, updated_at) amount, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)` VALUES (?, ?, ?, ?, ?, ?, ?, ?);`
result, err := m.DB.ExecContext(ctx, stmt, result, err := m.DB.ExecContext(ctx, stmt,
order.WidgetID, order.WidgetID,
@ -212,7 +212,7 @@ func (m *DBModel) InsertCustomer(customer Customer) (int, error) {
stmt := `INSERT INTO customers stmt := `INSERT INTO customers
(first_name, last_name, email, created_at, updated_at) (first_name, last_name, email, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)` VALUES (?, ?, ?, ?, ?);`
result, err := m.DB.ExecContext(ctx, stmt, result, err := m.DB.ExecContext(ctx, stmt,
customer.FirstName, customer.FirstName,
@ -241,7 +241,7 @@ func (m *DBModel) GetUserByEmail(email string) (User, error) {
query := `SELECT id, first_name, last_name, email, password, created_at, updated_at query := `SELECT id, first_name, last_name, email, password, created_at, updated_at
FROM users FROM users
WHERE email = ?` WHERE email = ?;`
row := m.DB.QueryRowContext(ctx, query, email) row := m.DB.QueryRowContext(ctx, query, email)
@ -268,7 +268,7 @@ func (m *DBModel) Authenticate(email, password string) (int, error) {
var id int var id int
var hashedPassword string var hashedPassword string
row := m.DB.QueryRowContext(ctx, "SELECT id, password from users WHERE email = ?", email) row := m.DB.QueryRowContext(ctx, "SELECT id, password from users WHERE email = ?;", email)
err := row.Scan(&id, &hashedPassword) err := row.Scan(&id, &hashedPassword)
if err != nil { if err != nil {
return 0, err return 0, err
@ -288,7 +288,7 @@ func (m *DBModel) UpdatePasswordForUser(u User, hash string) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel() defer cancel()
stmt := `UPDATE users SET password = ? where id = ?` stmt := `UPDATE users SET password = ? where id = ?;`
_, err := m.DB.ExecContext(ctx, stmt, hash, u.ID) _, err := m.DB.ExecContext(ctx, stmt, hash, u.ID)
if err != nil { if err != nil {
@ -317,7 +317,7 @@ func (m *DBModel) GetAllOrders(isRecurring bool) ([]*Order, error) {
WHERE WHERE
w.is_recurring = ? w.is_recurring = ?
ORDER BY ORDER BY
o.created_at DESC o.created_at DESC;
` `
rows, err := m.DB.QueryContext(ctx, query, isRecurring) rows, err := m.DB.QueryContext(ctx, query, isRecurring)
@ -387,7 +387,7 @@ func (m *DBModel) GetAllOrdersPaginated(
w.is_recurring = ? w.is_recurring = ?
ORDER BY ORDER BY
o.created_at DESC o.created_at DESC
LIMIT ? OFFSET ? LIMIT ? OFFSET ?;
` `
rows, err := m.DB.QueryContext(ctx, query, isRecurring, pageSize, offset) rows, err := m.DB.QueryContext(ctx, query, isRecurring, pageSize, offset)
@ -433,7 +433,7 @@ func (m *DBModel) GetAllOrdersPaginated(
SELECT COUNT(o.id) SELECT COUNT(o.id)
FROM orders o FROM orders o
LEFT JOIN widgets w on (o.widget_id = w.id) LEFT JOIN widgets w on (o.widget_id = w.id)
WHERE w.is_recurring = ? WHERE w.is_recurring = ?;
` `
var totalRecords int var totalRecords int
@ -467,7 +467,7 @@ func (m *DBModel) GetOrderByID(ID int) (Order, error) {
LEFT JOIN transactions t on (o.transaction_id = t.id) LEFT JOIN transactions t on (o.transaction_id = t.id)
LEFT JOIN customers c on (o.customer_id = c.id) LEFT JOIN customers c on (o.customer_id = c.id)
WHERE WHERE
o.id = ? o.id = ?;
` `
var o Order var o Order
@ -516,3 +516,118 @@ func (m *DBModel) UpdateOrderStatus(id, statusID int) error {
} }
return nil return nil
} }
func (m *DBModel) GetAllUsers() ([]*User, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var users []*User
query := `
SELECT id, last_name, first_name, email, created_at, updated_at
FROM users
ORDER BY last_name, first_name;
`
rows, err := m.DB.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var u User
err = rows.Scan(
&u.ID,
&u.LastName,
&u.FirstName,
&u.Email,
&u.CreatedAt,
&u.UpdatedAt,
)
if err != nil {
return nil, err
}
users = append(users, &u)
}
return users, nil
}
func (m *DBModel) GetOneUser(id int) (User, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var u User
query := `
SELECT id, last_name, first_name, email, created_at, updated_at
FROM users
WHERE id = ?;
`
row := m.DB.QueryRowContext(ctx, query, id)
err := row.Scan(
&u.ID,
&u.LastName,
&u.FirstName,
&u.Email,
&u.CreatedAt,
&u.UpdatedAt,
)
if err != nil {
return u, err
}
return u, nil
}
func (m *DBModel) EditUser(u User) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `
UPDATE users
SET last_name = ?, first_name = ?, email = ?, updated_at = ?
WHERE id = ?;
`
_, err := m.DB.ExecContext(ctx, stmt, u.LastName, u.FirstName, u.Email, time.Now(), u.ID)
if err != nil {
return err
}
return nil
}
func (m *DBModel) AddUser(u User, hash string) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `
INSERT INTO users
(last_name, first_name, email, password, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?);
`
_, err := m.DB.ExecContext(ctx, stmt,
u.LastName, u.FirstName, u.Email, hash, time.Now(), time.Now())
if err != nil {
return err
}
return nil
}
func (m *DBModel) DeleteUser(id int) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `
DELETE FROM users
WHERE id = ?;
`
_, err := m.DB.ExecContext(ctx, stmt, id)
if err != nil {
return err
}
return nil
}