-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee398e7
commit 6173b95
Showing
10 changed files
with
140 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
APP_SECRET="" | ||
|
||
ACCEPTED_DOMAINS="*" | ||
|
||
DB_NAME=databasse | ||
DB_USER=test | ||
DB_PORT=1234 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
var AppSecret = &cobra.Command{ | ||
Use: "make:secret", | ||
Short: "Generate app secret", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("App secret:") | ||
fmt.Println(generateBase64()) | ||
}, | ||
} | ||
|
||
// generateBase64 genera un valor base64 aleatorio | ||
func generateBase64() string { | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
// Genera 32 bytes aleatorios | ||
bytes := make([]byte, 32) | ||
rand.Read(bytes) | ||
|
||
// Codifica los bytes en base64 | ||
base64Value := base64.StdEncoding.EncodeToString(bytes) | ||
return base64Value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,34 @@ import ( | |
"fmt" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/render" | ||
"github.com/go-playground/locales/es" | ||
ut "github.com/go-playground/universal-translator" | ||
"github.com/go-playground/validator/v10" | ||
es_translations "github.com/go-playground/validator/v10/translations/es" | ||
"github.com/lfcifuentes/go-repository-pattern/app/http/handler/responses" | ||
"github.com/lfcifuentes/go-repository-pattern/app/model" | ||
"github.com/lfcifuentes/go-repository-pattern/app/repository" | ||
|
||
"io" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
var uni *ut.UniversalTranslator | ||
var validate *validator.Validate | ||
var trans ut.Translator | ||
|
||
func init() { | ||
esTranslator := es.New() | ||
uni = ut.New(esTranslator, esTranslator) | ||
trans, _ = uni.GetTranslator("es") | ||
validate = validator.New() | ||
err := es_translations.RegisterDefaultTranslations(validate, trans) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
type UserController struct { | ||
UserRepository repository.UserRepository | ||
} | ||
|
@@ -32,20 +53,28 @@ func (uc *UserController) GetAll(w http.ResponseWriter, r *http.Request) { | |
} | ||
|
||
func (uc *UserController) Create(w http.ResponseWriter, r *http.Request) { | ||
newUser := &model.User{ | ||
Name: "NombreUsuario", | ||
Email: "[email protected]", | ||
|
||
var newUser = model.User{} | ||
reqBody, _ := io.ReadAll(r.Body) | ||
newUser, _ = model.UnmarshalUser(reqBody) | ||
|
||
err := validate.Struct(newUser) | ||
if err != nil { | ||
// translate all error at once | ||
errs := err.(validator.ValidationErrors) | ||
responses.ResponseError(w, r, errs.Translate(trans), "Upps, no hemos podido procesar tu solicitud") | ||
return | ||
} | ||
|
||
err := uc.UserRepository.Create(newUser) | ||
err = uc.UserRepository.Create(&newUser) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
http.Error(w, "User not found", http.StatusNotFound) | ||
return | ||
} | ||
|
||
response := responses.ResponseOk(nil, "pong") | ||
response := responses.ResponseOk(nil, "usuario creado correctamente!") | ||
render.JSON(w, r, response) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters