A website inspired by Last.fm that will keep track of your listening statistics
lastfm music statistics
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Make a small program for inserting users into the database. Fix problem with insert helper.

oscar345 b2b43396 a01b02ec

+65 -6
+53
cmd/seeduser/main.go
··· 1 + package main 2 + 3 + import ( 4 + "context" 5 + "database/sql" 6 + "flag" 7 + "log" 8 + 9 + "github.com/oscar345/keeptrack/internal/config" 10 + "github.com/oscar345/keeptrack/internal/models" 11 + "github.com/oscar345/keeptrack/internal/repo/db" 12 + "github.com/oscar345/keeptrack/pkg/authentication" 13 + "github.com/oscar345/keeptrack/pkg/database" 14 + _ "modernc.org/sqlite" 15 + ) 16 + 17 + func main() { 18 + var ( 19 + email string 20 + password string 21 + isAdmin bool // currently not used 22 + ) 23 + 24 + cfg := config.Load() 25 + conn := database.Open("sqlite", cfg.AppDatabase.Path, func(d *sql.DB) {}) 26 + 27 + flag.StringVar(&email, "email", "", "Email address") 28 + flag.StringVar(&password, "password", "", "Password") 29 + flag.BoolVar(&isAdmin, "admin", false, "Is admin") 30 + flag.Parse() 31 + 32 + if email == "" || password == "" { 33 + log.Fatalln("Email and password are required") 34 + } 35 + 36 + password, err := authentication.HashPassword(password) 37 + if err != nil { 38 + log.Panicln(err) 39 + } 40 + 41 + user := models.User{ 42 + Email: email, 43 + Password: password, 44 + } 45 + 46 + userRepo := db.NewUserRepoDB(conn) 47 + userID, err := userRepo.Create(context.Background(), user) 48 + if err != nil { 49 + log.Panicln(err) 50 + } 51 + 52 + log.Println("User created with ID:", userID) 53 + }
+12 -6
pkg/database/insert.go
··· 4 4 "context" 5 5 "database/sql" 6 6 "fmt" 7 - "maps" 8 7 "slices" 9 8 "strings" 10 9 ) ··· 22 21 } 23 22 24 23 func Insert(ctx context.Context, db *sql.DB, table string, values map[string]any, opts ...InsertOption) (int, error) { 25 - var id int 24 + var ( 25 + id int 26 + args []any 27 + columns []string 28 + ) 26 29 statement := /*sql*/ ` 27 30 INSERT %s INTO %s (%s) VALUES (%s) RETURNING id 28 31 ` ··· 37 40 ignore = "OR IGNORE" 38 41 } 39 42 40 - columns := slices.Collect(maps.Keys(values)) 41 - placeholders := strings.Join(slices.Repeat([]string{"?"}, len(columns)), ", ") 42 - statement = fmt.Sprintf(statement, ignore, table, columns, placeholders) 43 - args := slices.Collect(maps.Values(values)) 43 + for key, value := range values { 44 + columns = append(columns, key) 45 + args = append(args, value) 46 + } 47 + 48 + placeholders := strings.Join(slices.Repeat([]string{"?"}, len(values)), ", ") 49 + statement = fmt.Sprintf(statement, ignore, table, strings.Join(columns, ", "), placeholders) 44 50 45 51 return QueryOne(ctx, db, statement, args, func(r *sql.Rows) (int, error) { 46 52 err := r.Scan(&id)