Peter Kurfer
9ea9a8f658
Some checks failed
Go build / build (push) Failing after 1m58s
- setup ent scheme - add command to create users - document API - add helpers to create migrations - add command to run migrations - add basic compose file
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/alecthomas/kong"
|
|
|
|
"code.icb4dc0.de/prskr/searcherside/core/cq"
|
|
"code.icb4dc0.de/prskr/searcherside/core/ports"
|
|
"code.icb4dc0.de/prskr/searcherside/core/services"
|
|
"code.icb4dc0.de/prskr/searcherside/infrastructure/config"
|
|
"code.icb4dc0.de/prskr/searcherside/infrastructure/repository"
|
|
)
|
|
|
|
type CreateUserHandler struct {
|
|
Email string `arg:""`
|
|
Password []byte `arg:"" type:"password"`
|
|
Admin bool `name:"admin" help:"Should the user be an admin" default:"false"`
|
|
DB config.DB `embed:"" prefix:"db."`
|
|
}
|
|
|
|
func (h *CreateUserHandler) Run(ctx context.Context, userRepo ports.UserRepository) (err error) {
|
|
defer func() {
|
|
err = errors.Join(err, userRepo.Close())
|
|
}()
|
|
|
|
_, err = userRepo.CreateUser(ctx, cq.CreateUserRequest{
|
|
Email: h.Email,
|
|
Password: h.Password,
|
|
Admin: h.Admin,
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
func (h *CreateUserHandler) AfterApply(kctx *kong.Context) error {
|
|
client, err := h.DB.Client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
kctx.BindTo(
|
|
repository.NewEntUserRepository(client, new(services.Argon2IDHashAlgorithm)),
|
|
(*ports.UserRepository)(nil),
|
|
)
|
|
|
|
return nil
|
|
}
|