Compare commits

...

3 Commits

9 changed files with 197 additions and 17 deletions

View File

@ -2,16 +2,133 @@ package controllers
import (
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"golang-test/database"
"golang-test/libs"
"golang-test/message"
"golang-test/validators"
"log"
"strconv"
//"golang-test/migrations"
)
func CreateProduct(product database.Product) gin.H {
//db := migrations.Migrate()
func CreateProduct(c *gin.Context, product database.Product) {
db := database.Connector()
validate := validators.Validate
response := message.Response{Status: 200}
//db.Create(&product)
err := validate.Struct(product)
return gin.H{
"message": "Created",
if err != nil {
response.Error = libs.GetValidationErrors(err.(validator.ValidationErrors))
response.Status = 400
message.SendResponse(c, response)
return
}
if err := db.First(&database.Manufacturer{}, product.ManufacturerID).Error; err != nil {
response.Error = gin.H{
"error": "Manufacturer with id '" + strconv.Itoa(int(product.ManufacturerID)) + "' not found",
}
}
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)
}
func DeleteProduct(c *gin.Context) {
db := database.Connector()
response := message.Response{Status: 200}
id := c.Param("id")
var product database.Product
if db.First(&product, id).Error != nil {
response.Error = gin.H{
"error": "Product with id '" + id + "' not found",
}
response.Status = 404
message.SendResponse(c, response)
return
}
db.Delete(&product, id)
response.Message = gin.H{
"message": "Product deleted",
}
message.SendResponse(c, response)
}
//func GetProducts(c *gin.Context) {
//
// db := database.Connector()
// response := message.Response{Status: 200}
// var product []database.Product
//
// resp := []interface{}{}
//
// db.Find(&product)
//
// for _, productItem := range product {
// resp = append(resp, gin.H{
// "id": productItem.ID,
// "name": productItem.Name,
// "price": productItem.Price,
// "manufacturer_ID": productItem.Manufacturer.ID,
// "manufacturer_Name": productItem.Manufacturer.Name,
// })
// log.Println(productItem.Manufacturer.Name)
// }
//
// response.Message = gin.H{
// "products": resp,
// }
//
// message.SendResponse(c, response)
//}
func GetProductInfo(c *gin.Context) {
db := database.Connector()
response := message.Response{Status: 200}
id := c.Param("id")
var product database.Product
if err := db.First(&product, id).Error; err != nil {
response.Error = gin.H{
"error": "Product with id '" + id + "' not found",
}
response.Status = 404
message.SendResponse(c, response)
return
}
response.Message = gin.H{
"product": gin.H{
"id": product.ID,
"name": product.Name,
"price": product.Price,
"manufacturer_ID": product.ManufacturerID,
"manufacturer_Name": product.Manufacturer.Name,
},
}
}

View File

@ -9,9 +9,9 @@ type Manufacturer struct {
type Product struct {
gorm.Model
Name string
Price uint
ManufacturerID uint
Name string `validate:"required`
Price uint `validate:"required"`
ManufacturerID uint `validate:"required"`
Manufacturer *Manufacturer `gorm:"foreignKey:ManufacturerID;references:ID"`
}

View File

@ -12,6 +12,6 @@ post {
body:json {
{
"name": "ЧЯЗ"
"name": "Тест 1"
}
}

View File

@ -1,5 +1,5 @@
meta {
name: ProductCreate
name: Create Product
type: http
seq: 1
}

View File

@ -0,0 +1,11 @@
meta {
name: Delete Product
type: http
seq: 2
}
delete {
url: {{host}}/product/{{productID}}
body: none
auth: none
}

View File

@ -0,0 +1,11 @@
meta {
name: Get Product Info
type: http
seq: 4
}
get {
url:
body: none
auth: none
}

View File

@ -0,0 +1,25 @@
meta {
name: Get Products
type: http
seq: 3
}
post {
url: {{host}}/product
body: json
auth: none
}
body:json {
{
"name": "Яблоко",
"price": 5,
"manufacturerID": 1
}
}
body:multipart-form {
name: Яблоко
price: 5
manufacturer: 1
}

View File

@ -1,4 +1,5 @@
vars {
host: http://localhost:8080
manufacturerID: 1
productID: 2
}

29
main.go
View File

@ -12,22 +12,34 @@ import (
func main() {
// Создание сервера
r := gin.Default()
//View для создание товара
r.POST("/product", func(c *gin.Context) {
var product database.Product
err := c.ShouldBind(&product)
if err != nil {
if err := c.ShouldBind(&product); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
//c.JSON(http.StatusOK, &product)
response := controllers.CreateProduct(product)
c.JSON(http.StatusOK, response)
controllers.CreateProduct(c, product)
})
//View для удаления товара
r.DELETE("/product/:id", func(c *gin.Context) {
controllers.DeleteProduct(c)
})
//View для просмотра всех продуктов
r.GET("/product", func(c *gin.Context) {
controllers.GetProducts(c)
})
r.GET("/product/:id", func(c *gin.Context) {
controllers.GetProductInfo(c)
})
//View для создания производителя
r.POST("/manufacturer", func(c *gin.Context) {
var manufacturer database.Manufacturer
if err := c.ShouldBind(&manufacturer); err != nil {
@ -37,14 +49,17 @@ func main() {
controllers.CreateManufacturer(c, manufacturer)
})
//View для удаления производителя
r.DELETE("/manufacturer/:id", func(c *gin.Context) {
controllers.DeleteManufacturer(c)
})
//View получения списка всех производителей
r.GET("/manufacturer", func(c *gin.Context) {
controllers.GetManufacturers(c)
})
//View для редактирования производителя
r.PATCH("/manufacturer/:id", func(c *gin.Context) {
var manufacturer types.ManufacturerPatchRequest