git-svn-id: svn://losandesgames.com/alfheim-website@53 15359d88-9307-4e75-a9c1-e5686e5897df

This commit is contained in:
vfs committed 2025-10-04 08:59:53 +00:00
1 parent eb222d8677
commit 65e87129a6
10 files changed
+114 -128

No files matched your search

+20 -32
View File
@@ -1,23 +1,21 @@
package postgresql
import (
"database/sql"
"context"
"fmt"
//import "golang.org/x/crypto/bcrypt"
"log"
"github.com/jackc/pgx/v5"
"github.com/alexedwards/argon2id"
_ "github.com/lib/pq"
"github.com/stripe/stripe-go/v78"
"github.com/stripe/stripe-go/v78/customer"
"github.com/stripe/stripe-go/v83"
"github.com/stripe/stripe-go/v83/customer"
"alfheimgame.com/alfheim/pkg/models"
)
type AccountModel struct {
DB *sql.DB
DB *pgx.Conn
}
func (m *AccountModel) Insert(username string, password string, firstname string, lastname string, email string) (int32, error) {
@@ -31,17 +29,11 @@ func (m *AccountModel) Insert(username string, password string, firstname string
}
//log.Println(hashedpassword)
stmt := `INSERT INTO accounts (username, password, firstname, lastname, email, created) VALUES ($1, $2, $3, $4, $5, NOW()) RETURNING id`
stmt := `INSERT INTO accounts (username, password, firstname, lastname, email) VALUES ($1, $2, $3, $4, $5) RETURNING id`
var insertid int32
row := m.DB.QueryRow(stmt, username, string(hashedpassword), firstname, lastname, email)
if row.Err() != nil {
log.Println(row.Err())
return 0, row.Err()
}
err = row.Scan(&insertid)
err = m.DB.QueryRow(context.Background(), stmt, username, string(hashedpassword), firstname, lastname, email).Scan(&insertid)
if err != nil {
log.Println(err)
return 0, err
@@ -60,9 +52,7 @@ func (m *AccountModel) Insert(username string, password string, firstname string
stmt = `UPDATE accounts SET stripe_id = $1 WHERE id = $2`
//log.Println(customer.ID, insertid)
_, err = m.DB.Exec(stmt, customer.ID, insertid)
_, err = m.DB.Exec(context.Background(), stmt, customer.ID, insertid)
if err != nil {
log.Println(err)
return 0, err
@@ -89,7 +79,7 @@ func (m *AccountModel) Delete(id int32) error {
stmt := `DELETE FROM accounts WHERE id = $1`
_, err = m.DB.Exec(stmt, id)
_, err = m.DB.Exec(context.Background(), stmt, id)
if err != nil {
log.Println(err)
}
@@ -101,14 +91,13 @@ func (m *AccountModel) GetAccount(id int32) (models.Account, error) {
if id == 0 {
return models.Account{}, models.ErrNoRecord
}
stmt := `SELECT id, username, password, color, firstname, lastname, email, created, stripe_id FROM accounts WHERE id = $1`
row := m.DB.QueryRow(stmt, id)
var account models.Account
err := row.Scan(&account.ID, &account.Username, &account.Password, &account.Color, &account.Firstname, &account.Lastname, &account.Email, &account.Created, &account.StripeID)
stmt := `SELECT id, username, password, colour, firstname, lastname, email, created, stripe_id FROM accounts WHERE id = $1`
err := m.DB.QueryRow(context.Background(), stmt, id).Scan(&account.ID, &account.Username, &account.Password, &account.Colour, &account.Firstname, &account.Lastname, &account.Email, &account.Created, &account.StripeID)
if err == sql.ErrNoRows {
return models.Account{}, sql.ErrNoRows
if err == pgx.ErrNoRows {
return models.Account{}, pgx.ErrNoRows
} else if err != nil {
return models.Account{}, err
}
@@ -119,9 +108,9 @@ func (m *AccountModel) GetAccount(id int32) (models.Account, error) {
func (m *AccountModel) Authenticate(username string, password string) (int32, error) {
var id int32
var hashedpassword string
row := m.DB.QueryRow("SELECT id, password FROM accounts WHERE username = $1", username)
err := row.Scan(&id, &hashedpassword)
if err == sql.ErrNoRows {
stmt := `SELECT id, password FROM accounts WHERE username = $1`
err := m.DB.QueryRow(context.Background(), stmt, username).Scan(&id, &hashedpassword)
if err == pgx.ErrNoRows {
return 0, models.ErrInvalidCredentials
}
@@ -138,11 +127,10 @@ func (m *AccountModel) Authenticate(username string, password string) (int32, er
func (m *AccountModel) ExistsAccount(id int32) bool {
var exists bool
stmt := `SELECT EXISTS(SELECT 1 FROM accounts WHERE id = $1)`
row := m.DB.QueryRow(stmt, id)
if row.Err() != nil {
log.Println(row.Err())
err := m.DB.QueryRow(context.Background(), stmt, id).Scan(&exists)
if err != nil {
log.Println(err)
}
row.Scan(&exists)
//log.Println(exists)
+14 -31
View File
@@ -1,33 +1,26 @@
package postgresql
import (
"database/sql"
"context"
//import "golang.org/x/crypto/bcrypt"
"github.com/jackc/pgx/v5"
"log"
_ "github.com/lib/pq"
"github.com/stripe/stripe-go/v78"
"github.com/stripe/stripe-go/v83"
"alfheimgame.com/alfheim/pkg/models"
)
type SubscriptionModel struct {
DB *sql.DB
DB *pgx.Conn
}
func (m *SubscriptionModel) Insert(stripeid string, stripesubscriptionid string, stripecheckoutid string, status stripe.SubscriptionStatus) (int32, error) {
var id int32
stmt := `SELECT id FROM accounts WHERE stripe_id = $1`
row := m.DB.QueryRow(stmt, stripeid)
if row.Err() != nil {
log.Println(row.Err())
return 0, row.Err()
}
err := row.Scan(&id)
err := m.DB.QueryRow(context.Background(), stmt, stripeid).Scan(&id)
if err != nil {
log.Println(err)
return 0, err
@@ -37,13 +30,7 @@ func (m *SubscriptionModel) Insert(stripeid string, stripesubscriptionid string,
var insertid int32
row = m.DB.QueryRow(stmt, id, string(stripesubscriptionid), string(stripecheckoutid), string(status))
if row.Err() != nil {
log.Println(row.Err())
return 0, row.Err()
}
err = row.Scan(&insertid)
err = m.DB.QueryRow(context.Background(), stmt, id, string(stripesubscriptionid), string(stripecheckoutid), string(status)).Scan(&insertid)
if err != nil {
log.Println(err)
return 0, err
@@ -55,7 +42,7 @@ func (m *SubscriptionModel) Insert(stripeid string, stripesubscriptionid string,
func (m *SubscriptionModel) Delete(id int32) error {
stmt := `DELETE FROM accounts WHERE id = $1`
_, err := m.DB.Exec(stmt, id)
_, err := m.DB.Exec(context.Background(), stmt, id)
if err != nil {
log.Println(err)
return err
@@ -69,20 +56,16 @@ func (m *SubscriptionModel) GetSubscription(id int32) (models.Subscription, erro
return models.Subscription{}, models.ErrNoRecord
}
stmt := `SELECT id, account_id, stripe_subscription_id, stripe_checkout_id, status FROM subscriptions WHERE id = $1`
row := m.DB.QueryRow(stmt, id)
var subscription models.Subscription
err := row.Scan(&subscription.ID, &subscription.AccountID, &subscription.StripeSubscriptionID, &subscription.StripeCheckoutID, &subscription.Status)
stmt := `SELECT id, account_id, stripe_subscription_id, stripe_checkout_id, status FROM subscriptions WHERE id = $1`
err := m.DB.QueryRow(context.Background(), stmt, id).Scan(&subscription.ID, &subscription.AccountID, &subscription.StripeSubscriptionID, &subscription.StripeCheckoutID, &subscription.Status)
if err == sql.ErrNoRows {
return models.Subscription{}, sql.ErrNoRows
if err == pgx.ErrNoRows {
return models.Subscription{}, pgx.ErrNoRows
} else if err != nil {
return models.Subscription{}, err
}
//log.Println(subscription.Status)
return subscription, nil
}
@@ -92,7 +75,7 @@ func (m *SubscriptionModel) GetSubscriptionsFromAccount(accountid int32) ([]mode
}
stmt := `SELECT id, account_id, stripe_subscription_id, stripe_checkout_id, status FROM subscriptions WHERE account_id = $1`
rows, err := m.DB.Query(stmt, accountid)
rows, err := m.DB.Query(context.Background(), stmt, accountid)
if err != nil {
return nil, err
}
@@ -102,8 +85,8 @@ func (m *SubscriptionModel) GetSubscriptionsFromAccount(accountid int32) ([]mode
for rows.Next() {
var subscription models.Subscription
err := rows.Scan(&subscription.ID, &subscription.AccountID, &subscription.StripeSubscriptionID, &subscription.StripeCheckoutID, &subscription.Status)
if err == sql.ErrNoRows {
return nil, sql.ErrNoRows
if err == pgx.ErrNoRows {
return nil, pgx.ErrNoRows
} else if err != nil {
return nil, err
}