Добавил создание Товара

This commit is contained in:
Владимир Шалимов 2024-06-06 14:37:44 +05:00
parent f9a63e7fce
commit 17a6019ea0
8 changed files with 158 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
"golang-test/message"
"golang-test/validators"
"log"
"strconv"
//"golang-test/migrations"
)
@ -25,6 +26,12 @@ func CreateProduct(c *gin.Context, product database.Product) {
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 {
@ -44,3 +51,84 @@ func CreateProduct(c *gin.Context, product database.Product) {
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

@ -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
}

20
main.go
View File

@ -12,8 +12,10 @@ import (
func main() {
// Создание сервера
r := gin.Default()
//View для создание товара
r.POST("/product", func(c *gin.Context) {
var product database.Product
if err := c.ShouldBind(&product); err != nil {
@ -23,6 +25,21 @@ func main() {
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 {
@ -32,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