This commit is contained in:
@@ -1,17 +1,27 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/rdarius/boot-dev-blog-aggregator/internal/config"
|
||||
"os"
|
||||
)
|
||||
|
||||
func HandlerLogin(s *config.State, cmd config.Command) error {
|
||||
func LoginHandler(s *config.State, cmd config.Command) error {
|
||||
if len(cmd.Args) < 1 {
|
||||
return errors.New("missing required argument USERNAME")
|
||||
}
|
||||
|
||||
err := s.Config.SetUser(cmd.Args[0])
|
||||
ctx := context.Background()
|
||||
|
||||
u, err := s.DB.GetUser(ctx, cmd.Args[0])
|
||||
if err != nil {
|
||||
fmt.Println("User does not exist")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = s.Config.SetUser(u.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to set current user: %w", err)
|
||||
}
|
||||
|
||||
46
internal/handlers/registerHandler.go
Normal file
46
internal/handlers/registerHandler.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rdarius/boot-dev-blog-aggregator/internal/config"
|
||||
"github.com/rdarius/boot-dev-blog-aggregator/internal/database"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RegisterHandler(s *config.State, cmd config.Command) error {
|
||||
if len(cmd.Args) < 1 {
|
||||
return errors.New("missing required argument USERNAME")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := s.DB.GetUser(ctx, cmd.Args[0])
|
||||
if err == nil {
|
||||
fmt.Println("User already exists")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
u, err := s.DB.CreateUser(ctx, database.CreateUserParams{
|
||||
ID: uuid.New(),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
Name: cmd.Args[0],
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create user %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = s.Config.SetUser(u.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to set current user: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("User Created %v\n", u)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user