ADD: added basic backend function plus a mockup for a cli interface

This commit is contained in:
2025-12-13 21:44:48 +01:00
parent c6de2481e6
commit a047d57824
21 changed files with 657 additions and 51 deletions

View File

@@ -0,0 +1,38 @@
// cmd/cli/user/get.go
package user
import (
"fmt"
// "studia/internal/db"
usersvc "studia/internal/user"
"github.com/spf13/cobra"
)
var id int64
var getCmd = &cobra.Command{
Use: "get",
Short: "Get user by ID",
RunE: func(cmd *cobra.Command, args []string) error {
database, err := db.New(getDSN())
if err != nil {
return err
}
service := usersvc.NewService(database)
user, err := service.GetByID(id)
if err != nil {
return err
}
fmt.Printf("ID: %d\nEmail: %s\nName: %s\n",
user.ID, user.Email, user.Name)
return nil
},
}
func init() {
getCmd.Flags().Int64Var(&id, "id", 0, "user ID")
getCmd.MarkFlagRequired("id")
}

View File

@@ -0,0 +1,32 @@
// cmd/cli/user/list.go
package user
import (
"fmt"
"myapp/internal/db"
usersvc "myapp/internal/user"
"github.com/spf13/cobra"
)
var listCmd = &cobra.Command{
Use: "list",
Short: "List users",
RunE: func(cmd *cobra.Command, args []string) error {
database, err := db.New(getDSN())
if err != nil {
return err
}
service := usersvc.NewService(database)
users, err := service.List()
if err != nil {
return err
}
for _, u := range users {
fmt.Printf("%d | %s | %s\n", u.ID, u.Email, u.Name)
}
return nil
},
}

View File

@@ -0,0 +1,13 @@
package user
import "github.com/spf13/cobra"
var UserCmd = &cobra.Command{
Use: "user",
Short: "User management",
}
func init() {
UserCmd.AddCommand(listCmd)
UserCmd.AddCommand(getCmd)
}