git-svn-id: svn://losandesgames.com/alfheim-website@49 15359d88-9307-4e75-a9c1-e5686e5897df
This commit is contained in:
1 parent
afb402223f
commit
391becf14e
6 files changed
+321
-299
No files matched your search
@@ -0,0 +1,150 @@
|
||||
package postgresql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
//import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
"log"
|
||||
|
||||
"github.com/alexedwards/argon2id"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/stripe/stripe-go/v78"
|
||||
"github.com/stripe/stripe-go/v78/customer"
|
||||
|
||||
"alfheimgame.com/alfheim/pkg/models"
|
||||
)
|
||||
|
||||
type AccountModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func (m *AccountModel) Insert(username string, password string, firstname string, lastname string, email string) (int32, error) {
|
||||
|
||||
//hashedpassword, err := bcrypt.GenerateFromPassword([]byte(password), 12)
|
||||
hashedpassword, err := argon2id.CreateHash(password, argon2id.DefaultParams)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
//log.Println(hashedpassword)
|
||||
stmt := `INSERT INTO accounts (username, password, firstname, lastname, email, created) VALUES ($1, $2, $3, $4, $5, NOW()) 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)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
params := &stripe.CustomerParams{
|
||||
Name: stripe.String(fmt.Sprintf("%s %s", firstname, lastname)),
|
||||
Email: stripe.String(email),
|
||||
}
|
||||
customer, err := customer.New(params)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
stmt = `UPDATE accounts SET stripe_id = $1 WHERE id = $2`
|
||||
|
||||
//log.Println(customer.ID, insertid)
|
||||
|
||||
_, err = m.DB.Exec(stmt, customer.ID, insertid)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return insertid, nil
|
||||
}
|
||||
|
||||
func (m *AccountModel) Delete(id int32) error {
|
||||
account, err := m.GetAccount(id)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if account.StripeID != "" {
|
||||
/*result*/ _, err := customer.Del(account.StripeID, nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
//log.Println(result)
|
||||
}
|
||||
|
||||
stmt := `DELETE FROM accounts WHERE id = $1`
|
||||
|
||||
_, err = m.DB.Exec(stmt, id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return models.Account{}, sql.ErrNoRows
|
||||
} else if err != nil {
|
||||
return models.Account{}, err
|
||||
}
|
||||
|
||||
return account, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
return 0, models.ErrInvalidCredentials
|
||||
}
|
||||
|
||||
match, err := argon2id.ComparePasswordAndHash(password, hashedpassword)
|
||||
if !match {
|
||||
return 0, models.ErrInvalidCredentials
|
||||
} else if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
row.Scan(&exists)
|
||||
|
||||
//log.Println(exists)
|
||||
|
||||
return exists
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package postgresql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
//import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
"log"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/stripe/stripe-go/v78"
|
||||
|
||||
"alfheimgame.com/alfheim/pkg/models"
|
||||
)
|
||||
|
||||
type SubscriptionModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
stmt = `INSERT INTO subscriptions (account_id, stripe_subscription_id, stripe_checkout_id, status) VALUES ($1, $2, $3, $4::subscription_status) RETURNING id`
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return insertid, nil
|
||||
}
|
||||
|
||||
func (m *SubscriptionModel) Delete(id int32) error {
|
||||
stmt := `DELETE FROM accounts WHERE id = $1`
|
||||
|
||||
_, err := m.DB.Exec(stmt, id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *SubscriptionModel) GetSubscription(id int32) (models.Subscription, error) {
|
||||
if id == 0 {
|
||||
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)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return models.Subscription{}, sql.ErrNoRows
|
||||
} else if err != nil {
|
||||
return models.Subscription{}, err
|
||||
}
|
||||
|
||||
//log.Println(subscription.Status)
|
||||
|
||||
return subscription, nil
|
||||
}
|
||||
|
||||
func (m *SubscriptionModel) GetSubscriptionsFromAccount(accountid int32) ([]models.Subscription, error) {
|
||||
if accountid == 0 {
|
||||
return nil, models.ErrNoRecord
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var subscriptions []models.Subscription
|
||||
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
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
subscriptions = append(subscriptions, subscription)
|
||||
}
|
||||
|
||||
return subscriptions, nil
|
||||
}
|
||||
|
||||
func (m *SubscriptionModel) HasActiveSubscription(accountid int32) bool {
|
||||
subscriptions, err := m.GetSubscriptionsFromAccount(accountid)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, v := range subscriptions {
|
||||
switch v.Status {
|
||||
case models.Active:
|
||||
return true
|
||||
case models.Trialing:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user