Добавил создание Товара
This commit is contained in:
parent
555ab78aaa
commit
bd50756728
|
@ -2,16 +2,45 @@ package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
"golang-test/database"
|
"golang-test/database"
|
||||||
|
"golang-test/libs"
|
||||||
|
"golang-test/message"
|
||||||
|
"golang-test/validators"
|
||||||
|
"log"
|
||||||
//"golang-test/migrations"
|
//"golang-test/migrations"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateProduct(product database.Product) gin.H {
|
func CreateProduct(c *gin.Context, product database.Product) {
|
||||||
//db := migrations.Migrate()
|
db := database.Connector()
|
||||||
|
validate := validators.Validate
|
||||||
|
response := message.Response{Status: 200}
|
||||||
|
|
||||||
//db.Create(&product)
|
err := validate.Struct(product)
|
||||||
|
|
||||||
return gin.H{
|
if err != nil {
|
||||||
"message": "Created",
|
response.Error = libs.GetValidationErrors(err.(validator.ValidationErrors))
|
||||||
|
response.Status = 400
|
||||||
|
message.SendResponse(c, response)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = db.Create(&product).Error
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err.Error())
|
||||||
|
response.Error = gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Status = 500
|
||||||
|
message.SendResponse(c, response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Message = gin.H{
|
||||||
|
"message": "Product created",
|
||||||
|
}
|
||||||
|
|
||||||
|
message.SendResponse(c, response)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,9 @@ type Manufacturer struct {
|
||||||
|
|
||||||
type Product struct {
|
type Product struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Name string
|
Name string `validate:"required`
|
||||||
Price uint
|
Price uint `validate:"required"`
|
||||||
ManufacturerID uint
|
ManufacturerID uint `validate:"required"`
|
||||||
Manufacturer *Manufacturer `gorm:"foreignKey:ManufacturerID;references:ID"`
|
Manufacturer *Manufacturer `gorm:"foreignKey:ManufacturerID;references:ID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
main.go
9
main.go
|
@ -16,16 +16,11 @@ func main() {
|
||||||
|
|
||||||
r.POST("/product", func(c *gin.Context) {
|
r.POST("/product", func(c *gin.Context) {
|
||||||
var product database.Product
|
var product database.Product
|
||||||
err := c.ShouldBind(&product)
|
if err := c.ShouldBind(&product); err != nil {
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//c.JSON(http.StatusOK, &product)
|
controllers.CreateProduct(c, product)
|
||||||
|
|
||||||
response := controllers.CreateProduct(product)
|
|
||||||
c.JSON(http.StatusOK, response)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
r.POST("/manufacturer", func(c *gin.Context) {
|
r.POST("/manufacturer", func(c *gin.Context) {
|
||||||
|
|
Loading…
Reference in New Issue