Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ env:
MINIO_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY_ID }}
MINIO_ACCESS_KEY_SECRET: ${{ secrets.MINIO_ACCESS_KEY_SECRET }}
MINIO_BUCKET: ${{ secrets.MINIO_BUCKET }}
CLOUDINARY_ACCESS_KEY_ID: ${{ secrets.CLOUDINARY_ACCESS_KEY_ID }}
CLOUDINARY_ACCESS_KEY_SECRET: ${{ secrets.CLOUDINARY_ACCESS_KEY_SECRET }}
CLOUDINARY_CLOUD: ${{ secrets.CLOUDINARY_CLOUD }}
jobs:
ubuntu:
strategy:
Expand Down
21 changes: 19 additions & 2 deletions app/http/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"

"goravel/app/http/requests"
"goravel/app/models"
)

Expand Down Expand Up @@ -44,9 +45,25 @@ func (r *UserController) Show(ctx http.Context) http.Response {
}

func (r *UserController) Store(ctx http.Context) http.Response {
var userCreate requests.UserCreate
errors, err := ctx.Request().ValidateRequest(&userCreate)
if err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"message": err.Error(),
})
}
if errors != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"message": errors.All(),
})
}

user := models.User{
Name: ctx.Request().Input("name"),
Avatar: ctx.Request().Input("avatar"),
Name: userCreate.Name,
Avatar: userCreate.Avatar,
Alias: userCreate.Alias,
Mail: userCreate.Mail,
Tags: userCreate.Tags,
}
if err := facades.Orm().Query().Create(&user); err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
Expand Down
14 changes: 7 additions & 7 deletions app/http/controllers/validation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (r *ValidationController) Json(ctx http.Context) http.Response {
}

func (r *ValidationController) Request(ctx http.Context) http.Response {
var userCreate requests.UserCreate
errors, err := ctx.Request().ValidateRequest(&userCreate)
var validationCreate requests.ValidationCreate
errors, err := ctx.Request().ValidateRequest(&validationCreate)
if err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"message": err.Error(),
Expand All @@ -89,11 +89,11 @@ func (r *ValidationController) Request(ctx http.Context) http.Response {
}

return ctx.Response().Success().Json(http.Json{
"name": userCreate.Name,
"tags": userCreate.Tags,
"scores": userCreate.Scores,
"date": userCreate.Date.ToDateTimeString(),
"code": userCreate.Code,
"name": validationCreate.Name,
"tags": validationCreate.Tags,
"scores": validationCreate.Scores,
"date": validationCreate.Date.ToDateTimeString(),
"code": validationCreate.Code,
})
}

Expand Down
43 changes: 8 additions & 35 deletions app/http/requests/user_create.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package requests

import (
"goravel/app/models"

"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
"github.com/goravel/framework/support/carbon"
"github.com/spf13/cast"
)

type UserCreate struct {
Name string `form:"name" json:"name"`
Tags []string `form:"tags" json:"tags"`
Scores []int `form:"scores" json:"scores"`
Date carbon.Carbon `form:"date" json:"date"`
Code int `form:"code" json:"code"`
Name string `form:"name" json:"name"`
Avatar string `form:"avatar" json:"avatar"`
Alias string `form:"alias" json:"alias"`
Mail string `form:"mail" json:"mail"`
Tags []models.UserTag `form:"tags" json:"tags"`
}

func (r *UserCreate) Authorize(ctx http.Context) error {
Expand All @@ -21,32 +20,6 @@ func (r *UserCreate) Authorize(ctx http.Context) error {

func (r *UserCreate) Rules(ctx http.Context) map[string]string {
return map[string]string{
"name": "required",
"tags.*": "required|string",
"scores.*": "required|int",
"date": "required|date",
"code": `required|regex:^\d{4,6}$`,
}
}

func (r *UserCreate) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UserCreate) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UserCreate) PrepareForValidation(ctx http.Context, data validation.Data) error {
if scores, exist := data.Get("scores"); exist {
return data.Set("scores", cast.ToIntSlice(scores))
}

return nil
}

func (r *UserCreate) Filters(ctx http.Context) map[string]string {
return map[string]string{
"name": "trim",
"name": "required",
}
}
52 changes: 52 additions & 0 deletions app/http/requests/validation_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
"github.com/goravel/framework/support/carbon"
"github.com/spf13/cast"
)

type ValidationCreate struct {
Name string `form:"name" json:"name"`
Tags []string `form:"tags" json:"tags"`
Scores []int `form:"scores" json:"scores"`
Date carbon.Carbon `form:"date" json:"date"`
Code int `form:"code" json:"code"`
}

func (r *ValidationCreate) Authorize(ctx http.Context) error {
return nil
}

func (r *ValidationCreate) Rules(ctx http.Context) map[string]string {
return map[string]string{
"name": "required",
"tags.*": "required|string",
"scores.*": "required|int",
"date": "required|date",
"code": `required|regex:^\d{4,6}$`,
}
}

func (r *ValidationCreate) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *ValidationCreate) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *ValidationCreate) PrepareForValidation(ctx http.Context, data validation.Data) error {
if scores, exist := data.Get("scores"); exist {
return data.Set("scores", cast.ToIntSlice(scores))
}

return nil
}

func (r *ValidationCreate) Filters(ctx http.Context) map[string]string {
return map[string]string{
"name": "trim",
}
}
27 changes: 27 additions & 0 deletions app/models/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package models

import (
"database/sql/driver"
"errors"

"github.com/goravel/framework/database/orm"
"github.com/goravel/framework/support/json"
)

type User struct {
Expand All @@ -10,5 +14,28 @@ type User struct {
Avatar string
Alias string
Mail string
Tags []UserTag `gorm:"serializer:json"`
orm.SoftDeletes
}

type UserTag struct {
Key string `json:"key"`
Val int `json:"value"`
}

func (r *UserTag) Scan(value any) error {
if value == nil {
return nil
}

bytes, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}

return json.Unmarshal(bytes, r)
}

func (r *UserTag) Value() (driver.Value, error) {
return json.Marshal(r)
}
14 changes: 7 additions & 7 deletions config/app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"github.com/goravel/cloudinary"
"github.com/goravel/cos"
"github.com/goravel/fiber"
"github.com/goravel/framework/auth"
Expand All @@ -26,6 +25,7 @@ import (
"github.com/goravel/framework/testing"
"github.com/goravel/framework/translation"
"github.com/goravel/framework/validation"
"github.com/goravel/framework/view"
"github.com/goravel/gin"
"github.com/goravel/minio"
"github.com/goravel/mysql"
Expand Down Expand Up @@ -105,12 +105,7 @@ func init() {
"providers": []foundation.ServiceProvider{
&log.ServiceProvider{},
&console.ServiceProvider{},
&postgres.ServiceProvider{},
&mysql.ServiceProvider{},
&sqlserver.ServiceProvider{},
&sqlite.ServiceProvider{},
&database.ServiceProvider{},
&redis.ServiceProvider{},
&cache.ServiceProvider{},
&http.ServiceProvider{},
&route.ServiceProvider{},
Expand All @@ -127,6 +122,7 @@ func init() {
&session.ServiceProvider{},
&translation.ServiceProvider{},
&testing.ServiceProvider{},
&view.ServiceProvider{},
&providers.AppServiceProvider{},
&providers.AuthServiceProvider{},
&providers.RouteServiceProvider{},
Expand All @@ -136,11 +132,15 @@ func init() {
&providers.EventServiceProvider{},
&providers.ValidationServiceProvider{},
&providers.DatabaseServiceProvider{},
&postgres.ServiceProvider{},
&mysql.ServiceProvider{},
&sqlserver.ServiceProvider{},
&sqlite.ServiceProvider{},
&s3.ServiceProvider{},
&cos.ServiceProvider{},
&oss.ServiceProvider{},
&cloudinary.ServiceProvider{},
&minio.ServiceProvider{},
&redis.ServiceProvider{},
&gin.ServiceProvider{},
&fiber.ServiceProvider{},
},
Expand Down
1 change: 1 addition & 0 deletions config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func init() {
"driver": "default",
"table": "migrations",
},

"redis": map[string]any{
"default": map[string]any{
"host": config.Env("REDIS_HOST", ""),
Expand Down
10 changes: 0 additions & 10 deletions config/filesystems.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
cloudinaryfacades "github.com/goravel/cloudinary/facades"
cosfacades "github.com/goravel/cos/facades"
"github.com/goravel/framework/contracts/filesystem"
"github.com/goravel/framework/facades"
Expand Down Expand Up @@ -69,15 +68,6 @@ func init() {
return ossfacades.Oss("oss") // The `oss` value is the `disks` key
},
},
"cloudinary": map[string]any{
"driver": "custom",
"cloud": config.Env("CLOUDINARY_CLOUD"),
"key": config.Env("CLOUDINARY_ACCESS_KEY_ID"),
"secret": config.Env("CLOUDINARY_ACCESS_KEY_SECRET"),
"via": func() (filesystem.Driver, error) {
return cloudinaryfacades.Cloudinary("cloudinary") // The `cloudinary` value is the `disks` key
},
},
"minio": map[string]any{
"driver": "custom",
"key": config.Env("MINIO_ACCESS_KEY_ID"),
Expand Down
1 change: 1 addition & 0 deletions database/migrations/20210101000001_create_users_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (r *M20210101000001CreateUsersTable) Up() error {
table.BigIncrements("id")
table.String("name").Default("")
table.String("avatar").Default("")
table.Json("tags").Nullable()
table.Timestamps()
table.SoftDeletes()
table.Comment("user table")
Expand Down
Loading