Добавил создание Товара
This commit is contained in:
parent
f9a63e7fce
commit
17a6019ea0
|
@ -8,6 +8,7 @@ import (
|
||||||
"golang-test/message"
|
"golang-test/message"
|
||||||
"golang-test/validators"
|
"golang-test/validators"
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
//"golang-test/migrations"
|
//"golang-test/migrations"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,6 +26,12 @@ func CreateProduct(c *gin.Context, product database.Product) {
|
||||||
return
|
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
|
err = db.Create(&product).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -44,3 +51,84 @@ func CreateProduct(c *gin.Context, product database.Product) {
|
||||||
|
|
||||||
message.SendResponse(c, response)
|
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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -12,6 +12,6 @@ post {
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
{
|
{
|
||||||
"name": "ЧЯЗ"
|
"name": "Тест 1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
meta {
|
meta {
|
||||||
name: ProductCreate
|
name: Create Product
|
||||||
type: http
|
type: http
|
||||||
seq: 1
|
seq: 1
|
||||||
}
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
meta {
|
||||||
|
name: Delete Product
|
||||||
|
type: http
|
||||||
|
seq: 2
|
||||||
|
}
|
||||||
|
|
||||||
|
delete {
|
||||||
|
url: {{host}}/product/{{productID}}
|
||||||
|
body: none
|
||||||
|
auth: none
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
meta {
|
||||||
|
name: Get Product Info
|
||||||
|
type: http
|
||||||
|
seq: 4
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url:
|
||||||
|
body: none
|
||||||
|
auth: none
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
vars {
|
vars {
|
||||||
host: http://localhost:8080
|
host: http://localhost:8080
|
||||||
manufacturerID: 1
|
manufacturerID: 1
|
||||||
|
productID: 2
|
||||||
}
|
}
|
||||||
|
|
20
main.go
20
main.go
|
@ -12,8 +12,10 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// Создание сервера
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
|
//View для создание товара
|
||||||
r.POST("/product", func(c *gin.Context) {
|
r.POST("/product", func(c *gin.Context) {
|
||||||
var product database.Product
|
var product database.Product
|
||||||
if err := c.ShouldBind(&product); err != nil {
|
if err := c.ShouldBind(&product); err != nil {
|
||||||
|
@ -23,6 +25,21 @@ func main() {
|
||||||
controllers.CreateProduct(c, product)
|
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) {
|
r.POST("/manufacturer", func(c *gin.Context) {
|
||||||
var manufacturer database.Manufacturer
|
var manufacturer database.Manufacturer
|
||||||
if err := c.ShouldBind(&manufacturer); err != nil {
|
if err := c.ShouldBind(&manufacturer); err != nil {
|
||||||
|
@ -32,14 +49,17 @@ func main() {
|
||||||
controllers.CreateManufacturer(c, manufacturer)
|
controllers.CreateManufacturer(c, manufacturer)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
//View для удаления производителя
|
||||||
r.DELETE("/manufacturer/:id", func(c *gin.Context) {
|
r.DELETE("/manufacturer/:id", func(c *gin.Context) {
|
||||||
controllers.DeleteManufacturer(c)
|
controllers.DeleteManufacturer(c)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
//View получения списка всех производителей
|
||||||
r.GET("/manufacturer", func(c *gin.Context) {
|
r.GET("/manufacturer", func(c *gin.Context) {
|
||||||
controllers.GetManufacturers(c)
|
controllers.GetManufacturers(c)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
//View для редактирования производителя
|
||||||
r.PATCH("/manufacturer/:id", func(c *gin.Context) {
|
r.PATCH("/manufacturer/:id", func(c *gin.Context) {
|
||||||
var manufacturer types.ManufacturerPatchRequest
|
var manufacturer types.ManufacturerPatchRequest
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue