API login/register/delete-account, automatic db schema init
This commit is contained in:
parent
1846203076
commit
f7edece0af
11 changed files with 479 additions and 8 deletions
60
account/controller/account.go
Normal file
60
account/controller/account.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"arimelody-web/account/model"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
func GetAccount(db *sqlx.DB, username string) (*model.Account, error) {
|
||||
var account = model.Account{}
|
||||
|
||||
err := db.Get(&account, "SELECT * FROM account WHERE username=$1", username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
func GetAccountByEmail(db *sqlx.DB, email string) (*model.Account, error) {
|
||||
var account = model.Account{}
|
||||
|
||||
err := db.Get(&account, "SELECT * FROM account WHERE email=$1", email)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
func CreateAccount(db *sqlx.DB, account *model.Account) error {
|
||||
_, err := db.Exec(
|
||||
"INSERT INTO account (username, password, email, avatar_url) " +
|
||||
"VALUES ($1, $2, $3, $4)",
|
||||
account.Username,
|
||||
account.Password,
|
||||
account.Email,
|
||||
account.AvatarURL)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateAccount(db *sqlx.DB, account *model.Account) error {
|
||||
_, err := db.Exec(
|
||||
"UPDATE account " +
|
||||
"SET username=$2, password=$3, email=$4, avatar_url=$5) " +
|
||||
"WHERE id=$1",
|
||||
account.ID,
|
||||
account.Username,
|
||||
account.Password,
|
||||
account.Email,
|
||||
account.AvatarURL)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func DeleteAccount(db *sqlx.DB, accountID string) error {
|
||||
_, err := db.Exec("DELETE FROM account WHERE id=$1", accountID)
|
||||
return err
|
||||
}
|
54
account/model/account.go
Normal file
54
account/model/account.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
Account struct {
|
||||
ID string `json:"id" db:"id"`
|
||||
Username string `json:"username" db:"username"`
|
||||
Password []byte `json:"password" db:"password"`
|
||||
Email string `json:"email" db:"email"`
|
||||
AvatarURL string `json:"avatar_url" db:"avatar_url"`
|
||||
Privileges []AccountPrivilege `json:"privileges"`
|
||||
}
|
||||
|
||||
AccountPrivilege string
|
||||
|
||||
Invite struct {
|
||||
Code string `db:"code"`
|
||||
CreatedByID string `db:"created_by"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
ExpiresAt time.Time `db:"expires_at"`
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
Root AccountPrivilege = "root" // grants all permissions. very dangerous to grant!
|
||||
|
||||
// unused for now
|
||||
CreateInvites AccountPrivilege = "create_invites"
|
||||
ReadAccounts AccountPrivilege = "read_accounts"
|
||||
EditAccounts AccountPrivilege = "edit_accounts"
|
||||
|
||||
ReadReleases AccountPrivilege = "read_releases"
|
||||
EditReleases AccountPrivilege = "edit_releases"
|
||||
|
||||
ReadTracks AccountPrivilege = "read_tracks"
|
||||
EditTracks AccountPrivilege = "edit_tracks"
|
||||
|
||||
ReadArtists AccountPrivilege = "read_artists"
|
||||
EditArtists AccountPrivilege = "edit_artists"
|
||||
)
|
||||
|
||||
var inviteChars = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
|
||||
func GenerateInviteCode(length int) []byte {
|
||||
code := []byte{}
|
||||
for i := 0; i < length; i++ {
|
||||
code = append(code, inviteChars[rand.Intn(len(inviteChars) - 1)])
|
||||
}
|
||||
return code
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue