-
-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add generate controller cli #39
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,19 +7,21 @@ import ( | |
|
||
"github.com/go-fuego/fuego/cmd/fuego/templates" | ||
"github.com/urfave/cli/v2" | ||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
func ControllerCommand() *cli.Command { | ||
func Controller() *cli.Command { | ||
return &cli.Command{ | ||
Name: "controller", | ||
Usage: "add a new template", | ||
Usage: "creates a new controller file", | ||
Aliases: []string{"c"}, | ||
Action: func(cCtx *cli.Context) error { | ||
controllerName := "newController" | ||
if cCtx.NArg() > 0 { | ||
controllerName = cCtx.Args().First() | ||
} else { | ||
fmt.Println("Note: You can add a controller name as an argument. Example: fuego controller yourControllerName") | ||
fmt.Println("Note: You can add a controller name as an argument. Example: `fuego controller books`") | ||
} | ||
|
||
err := createController(controllerName) | ||
|
@@ -33,22 +35,29 @@ func ControllerCommand() *cli.Command { | |
} | ||
} | ||
|
||
// createController creates a new controller file | ||
func createController(controllerName string) error { | ||
controllerDir := "./controllers/" | ||
if _, err := os.Stat(controllerDir); os.IsNotExist(err) { | ||
err = os.Mkdir(controllerDir, 0755) | ||
err = os.Mkdir(controllerDir, 0o755) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Je mets explicitement que c'est de l'octal |
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
templateContent, err := templates.FS.ReadFile("controller.template") | ||
templateContent, err := templates.FS.ReadFile("controller/controller.go") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t := language.English | ||
titler := cases.Title(t) | ||
|
||
newContent := strings.ReplaceAll(string(templateContent), "newController", controllerName) | ||
newContent = strings.ReplaceAll(newContent, "NewController", strings.Title(controllerName)) | ||
newContent = strings.ReplaceAll(newContent, "NewController", titler.String(controllerName)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. askip mon IDE me dit qu'il faut préférer utiliser |
||
|
||
controllerPath := fmt.Sprintf("%s%s.go", controllerDir, controllerName) | ||
err = os.WriteFile(controllerPath, []byte(newContent), 0644) | ||
err = os.WriteFile(controllerPath, []byte(newContent), 0o644) | ||
if err != nil { | ||
return err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package commands | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateController(t *testing.T) { | ||
err := createController("books") | ||
require.NoError(t, err) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ca fait toujours plez pour vérifier que rien ne crash :) |
This file was deleted.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renommé parce que comme ça on est sûrs de pas faire de bêtises, on a le formattting, les imports etc... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/go-fuego/fuego" | ||
) | ||
|
||
type NewControllerRessources struct { | ||
// TODO add ressources | ||
NewControllerService NewControllerService | ||
} | ||
|
||
type NewController struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
type NewControllerCreate struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
type NewControllerUpdate struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
func (rs NewControllerRessources) Routes(s *fuego.Server) { | ||
newControllerGroup := fuego.Group(s, "/newController") | ||
|
||
fuego.Get(newControllerGroup, "/", rs.getAllNewController) | ||
fuego.Post(newControllerGroup, "/", rs.postNewController) | ||
|
||
fuego.Get(newControllerGroup, "/{id}", rs.getNewController) | ||
fuego.Put(newControllerGroup, "/{id}", rs.putNewController) | ||
fuego.Delete(newControllerGroup, "/{id}", rs.deleteNewController) | ||
} | ||
|
||
func (rs NewControllerRessources) getAllNewController(c fuego.ContextNoBody) ([]NewController, error) { | ||
return rs.NewControllerService.GetAllNewController() | ||
} | ||
|
||
func (rs NewControllerRessources) postNewController(c *fuego.ContextWithBody[NewControllerCreate]) (NewController, error) { | ||
body, err := c.Body() | ||
if err != nil { | ||
return NewController{}, err | ||
} | ||
|
||
new, err := rs.NewControllerService.CreateNewController(body) | ||
if err != nil { | ||
return NewController{}, err | ||
} | ||
|
||
return new, nil | ||
} | ||
|
||
func (rs NewControllerRessources) getNewController(c fuego.ContextNoBody) (NewController, error) { | ||
return rs.NewControllerService.GetNewController(c.PathParam("id")) | ||
} | ||
|
||
func (rs NewControllerRessources) putNewController(c *fuego.ContextWithBody[NewControllerUpdate]) (NewController, error) { | ||
body, err := c.Body() | ||
if err != nil { | ||
return NewController{}, err | ||
} | ||
|
||
new, err := rs.NewControllerService.UpdateNewController(c.PathParam("id"), body) | ||
if err != nil { | ||
return NewController{}, err | ||
} | ||
|
||
return new, nil | ||
} | ||
|
||
func (rs NewControllerRessources) deleteNewController(c *fuego.ContextNoBody) (any, error) { | ||
return rs.NewControllerService.DeleteNewController(c.PathParam("id")) | ||
} | ||
|
||
type NewControllerService interface { | ||
GetNewController(id string) (NewController, error) | ||
CreateNewController(NewControllerCreate) (NewController, error) | ||
GetAllNewController() ([]NewController, error) | ||
UpdateNewController(id string, input NewControllerUpdate) (NewController, error) | ||
DeleteNewController(id string) (any, error) | ||
Comment on lines
+77
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. J'ai rajouté un corps aux fonctions et les signatures d'interface : Comme ça, plus qu'à implémenter les interfaces (faire la partie BDD) et on a notre CRUD fini ! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,5 @@ import ( | |
"embed" | ||
) | ||
|
||
//go:embed *.template | ||
//go:embed */*.go | ||
var FS embed.FS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On l'appelle de l'extérieur
commands.Controller
donc on comprend que c'est la commande pour créer des controllers