-
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
Achmad Rizky Syahrani
committed
Sep 14, 2022
1 parent
6a0084e
commit a32c14d
Showing
14 changed files
with
1,720 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"mvc/models" | ||
|
||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var config = map[string]string{ | ||
"user": "root", | ||
"password": "", | ||
"host": "127.0.0.1:3306", | ||
"dbname": "agmc", | ||
} | ||
|
||
func InitDB() *gorm.DB { | ||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", | ||
config["user"], | ||
config["password"], | ||
config["host"], | ||
config["dbname"], | ||
) | ||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
autoMigrate(db) | ||
|
||
return db | ||
} | ||
|
||
func autoMigrate(db *gorm.DB) { | ||
db.AutoMigrate(&models.User{}) | ||
} |
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,122 @@ | ||
package controllers | ||
|
||
import ( | ||
"mvc/lib/database" | ||
"mvc/models" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func GetAllBooks(ctx echo.Context) error { | ||
result, total, err := database.GetAllBooks() | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, map[string]interface{}{ | ||
"status": "success", | ||
"message": "success to fetch data from server", | ||
"data": result, | ||
"total": total, | ||
}) | ||
} | ||
|
||
func CreateNewBook(ctx echo.Context) error { | ||
var err error | ||
|
||
bookInput := &models.BookInput{} | ||
err = ctx.Bind(&bookInput) | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to bind body request", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
result, err := database.CreateNewBook(bookInput) | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusCreated, map[string]interface{}{ | ||
"status": "success", | ||
"message": "book has created", | ||
"data": result, | ||
}) | ||
} | ||
|
||
func GetBookByID(ctx echo.Context) error { | ||
bookID := ctx.Param("id") | ||
result, err := database.GetBookByID(bookID) | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, map[string]interface{}{ | ||
"status": "success", | ||
"message": "success to fetch data from server", | ||
"data": result, | ||
}) | ||
} | ||
|
||
func DeleteBookByID(ctx echo.Context) error { | ||
bookID := ctx.Param("id") | ||
result, err := database.DeleteBookByID(bookID) | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, map[string]interface{}{ | ||
"status": "success", | ||
"message": "book has deleted", | ||
"data": result, | ||
}) | ||
} | ||
|
||
func UpdateBookByID(ctx echo.Context) error { | ||
var err error | ||
|
||
bookID := ctx.Param("id") | ||
bookInput := &models.BookInput{} | ||
err = ctx.Bind(&bookInput) | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to bind body request", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
result, err := database.UpdateBookByID(bookInput, bookID) | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, map[string]interface{}{ | ||
"status": "success", | ||
"message": "book has updated", | ||
"data": result, | ||
}) | ||
} |
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,121 @@ | ||
package controllers | ||
|
||
import ( | ||
"mvc/lib/database" | ||
"mvc/models" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func GetAllUsers(ctx echo.Context) error { | ||
result, total, err := database.GetAllUsers() | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, map[string]interface{}{ | ||
"status": "success", | ||
"message": "success to fetch data from server", | ||
"data": result, | ||
"total": total, | ||
}) | ||
} | ||
|
||
func CreateNewUser(ctx echo.Context) error { | ||
var err error | ||
|
||
userInput := &models.UserInput{} | ||
err = ctx.Bind(&userInput) | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to bind body request", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
result, err := database.CreateNewUser(userInput) | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusCreated, map[string]interface{}{ | ||
"status": "success", | ||
"message": "user has created", | ||
"data": result, | ||
}) | ||
} | ||
|
||
func GetUserByID(ctx echo.Context) error { | ||
userID := ctx.Param("id") | ||
result, err := database.GetUserByID(userID) | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, map[string]interface{}{ | ||
"status": "success", | ||
"message": "success to fetch data from server", | ||
"data": result, | ||
}) | ||
} | ||
|
||
func DeleteUserByID(ctx echo.Context) error { | ||
userID := ctx.Param("id") | ||
err := database.DeleteUserByID(userID) | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, map[string]interface{}{ | ||
"status": "success", | ||
"message": "book has deleted", | ||
}) | ||
} | ||
|
||
func UpdateUserByID(ctx echo.Context) error { | ||
var err error | ||
|
||
userID := ctx.Param("id") | ||
userInput := &models.UserInput{} | ||
err = ctx.Bind(&userInput) | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to bind body request", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
result, err := database.UpdateUserByID(userInput, userID) | ||
if err != nil { | ||
return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||
"status": "failed", | ||
"message": "failed to fetch data from server", | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, map[string]interface{}{ | ||
"status": "success", | ||
"message": "user has updated", | ||
"data": result, | ||
}) | ||
} |
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,24 @@ | ||
module mvc | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/labstack/echo/v4 v4.9.0 | ||
gorm.io/driver/mysql v1.3.6 | ||
gorm.io/gorm v1.23.8 | ||
) | ||
|
||
require ( | ||
github.com/go-sql-driver/mysql v1.6.0 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
github.com/labstack/gommon v0.3.1 // indirect | ||
github.com/mattn/go-colorable v0.1.11 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/fasttemplate v1.2.1 // indirect | ||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect | ||
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect | ||
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
) |
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,45 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= | ||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= | ||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/labstack/echo/v4 v4.9.0 h1:wPOF1CE6gvt/kmbMR4dGzWvHMPT+sAEUJOwOTtvITVY= | ||
github.com/labstack/echo/v4 v4.9.0/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks= | ||
github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o= | ||
github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= | ||
github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs= | ||
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= | ||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= | ||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | ||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | ||
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4= | ||
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= | ||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= | ||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= | ||
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= | ||
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4= | ||
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= | ||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gorm.io/driver/mysql v1.3.6 h1:BhX1Y/RyALb+T9bZ3t07wLnPZBukt+IRkMn8UZSNbGM= | ||
gorm.io/driver/mysql v1.3.6/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c= | ||
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE= | ||
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= |
Oops, something went wrong.