Add some user related commands
This commit is contained in:
parent
d5a297cb86
commit
83a2d2a31f
8 changed files with 223 additions and 11 deletions
18
server/app/app.go
Normal file
18
server/app/app.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"git.ctrlz.es/mgdelacroix/craban/model"
|
||||
"git.ctrlz.es/mgdelacroix/craban/services/store"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
config *model.Config
|
||||
store *store.Store
|
||||
}
|
||||
|
||||
func NewApp(config *model.Config, store *store.Store) *App {
|
||||
return &App{
|
||||
config: config,
|
||||
store: store,
|
||||
}
|
||||
}
|
36
server/app/user.go
Normal file
36
server/app/user.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/craban/model"
|
||||
"git.ctrlz.es/mgdelacroix/craban/utils"
|
||||
)
|
||||
|
||||
func (a *App) CreateUser(username, password, name, mail string) (*model.User, error) {
|
||||
hashedPassword, err := utils.Encrypt(password)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create user: %w", err)
|
||||
}
|
||||
|
||||
newUser := &model.User{
|
||||
Username: username,
|
||||
Password: hashedPassword,
|
||||
Name: name,
|
||||
Mail: mail,
|
||||
}
|
||||
|
||||
if err := newUser.IsValid(); err != nil {
|
||||
return nil, fmt.Errorf("invalid user for creation: %w", err)
|
||||
}
|
||||
|
||||
return a.store.User().Create(newUser)
|
||||
}
|
||||
|
||||
func (a *App) ListUsers() ([]*model.User, error) {
|
||||
return a.store.User().List()
|
||||
}
|
||||
|
||||
func (a *App) DeleteUserByUsername(username string) error {
|
||||
return a.store.User().DeleteByUsername(username)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue