Parcourir la source

feat: add authentication CLI controller using service to generate access token

Matheus Marques Polillo il y a 9 mois
Parent
commit
3448f7e6ec

+ 51 - 0
src/presentation/cli/controller/authentication.go

@@ -0,0 +1,51 @@
+package cliController
+
+import (
+	internalDbInfra "github.com/goinfinite/os/src/infra/internalDatabase"
+	cliHelper "github.com/goinfinite/os/src/presentation/cli/helper"
+	"github.com/goinfinite/os/src/presentation/service"
+	"github.com/spf13/cobra"
+)
+
+type AuthenticationController struct {
+	authenticationService *service.AuthenticationService
+}
+
+func NewAuthenticationController(
+	persistentDbSvc *internalDbInfra.PersistentDatabaseService,
+	trailDbSvc *internalDbInfra.TrailDatabaseService,
+) *AuthenticationController {
+	return &AuthenticationController{
+		authenticationService: service.NewAuthenticationService(
+			persistentDbSvc, trailDbSvc,
+		),
+	}
+}
+
+func (controller *AuthenticationController) Login() *cobra.Command {
+	var usernameStr, passwordStr, ipAddressStr string
+
+	cmd := &cobra.Command{
+		Use:   "login",
+		Short: "Login",
+		Run: func(cmd *cobra.Command, args []string) {
+			requestBody := map[string]interface{}{
+				"username":          usernameStr,
+				"password":          passwordStr,
+				"operatorIpAddress": ipAddressStr,
+			}
+
+			cliHelper.ServiceResponseWrapper(
+				controller.authenticationService.Login(requestBody),
+			)
+		},
+	}
+
+	cmd.Flags().StringVarP(&usernameStr, "username", "u", "", "Username")
+	cmd.MarkFlagRequired("username")
+	cmd.Flags().StringVarP(&passwordStr, "password", "p", "", "Password")
+	cmd.MarkFlagRequired("password")
+	cmd.Flags().StringVarP(&ipAddressStr, "ip-address", "i", "", "IpAddress")
+	cmd.MarkFlagRequired("ip-address")
+	return cmd
+}

+ 15 - 0
src/presentation/cli/router.go

@@ -53,6 +53,20 @@ func (router Router) accountRoutes() {
 	accountCmd.AddCommand(accountController.Delete())
 }
 
+func (router Router) authenticationRoutes() {
+	var accountCmd = &cobra.Command{
+		Use:   "auth",
+		Short: "Authentication",
+	}
+	rootCmd.AddCommand(accountCmd)
+
+	authenticationController := cliController.NewAuthenticationController(
+		router.persistentDbSvc, router.trailDbSvc,
+	)
+
+	accountCmd.AddCommand(authenticationController.Login())
+}
+
 func (router Router) cronRoutes() {
 	var cronCmd = &cobra.Command{
 		Use:   "cron",
@@ -223,6 +237,7 @@ func (router Router) RegisterRoutes() {
 	rootCmd.AddCommand(versionCmd)
 
 	router.accountRoutes()
+	router.authenticationRoutes()
 	router.cronRoutes()
 	router.databaseRoutes()
 	router.marketplaceRoutes()