Skip to content

Commit

Permalink
Merge pull request #54 from nikhilnarayanan623/feature/aws-s3
Browse files Browse the repository at this point in the history
aws s3 feature added for product item saving and geting
  • Loading branch information
nikhilnarayanan623 authored Aug 11, 2023
2 parents 68cfbd6 + 7631e0b commit 6e62eb8
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 140 deletions.
70 changes: 30 additions & 40 deletions cmd/api/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 30 additions & 40 deletions cmd/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1920,13 +1920,36 @@
"required": true
},
{
"description": "Product item input",
"name": "input",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/request.ProductItem"
}
"type": "integer",
"description": "Price",
"name": "price",
"in": "formData",
"required": true
},
{
"type": "integer",
"description": "Quantity In Stock",
"name": "qty_in_stock",
"in": "formData",
"required": true
},
{
"type": "array",
"items": {
"type": "integer"
},
"collectionFormat": "csv",
"description": "Variation Option IDs",
"name": "variation_option_ids",
"in": "formData",
"required": true
},
{
"type": "file",
"description": "Images",
"name": "images",
"in": "formData",
"required": true
}
],
"responses": {
Expand Down Expand Up @@ -3816,39 +3839,6 @@
}
}
},
"request.ProductItem": {
"type": "object",
"required": [
"images",
"price",
"qty_in_stock",
"variation_option_ids"
],
"properties": {
"images": {
"type": "array",
"minItems": 1,
"items": {
"type": "string"
}
},
"price": {
"type": "integer",
"minimum": 1
},
"qty_in_stock": {
"type": "integer",
"minimum": 1
},
"variation_option_ids": {
"type": "array",
"minItems": 1,
"items": {
"type": "integer"
}
}
}
},
"request.RefreshToken": {
"type": "object",
"properties": {
Expand Down
51 changes: 22 additions & 29 deletions cmd/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,30 +267,6 @@ definitions:
- offer_id
- product_id
type: object
request.ProductItem:
properties:
images:
items:
type: string
minItems: 1
type: array
price:
minimum: 1
type: integer
qty_in_stock:
minimum: 1
type: integer
variation_option_ids:
items:
type: integer
minItems: 1
type: array
required:
- images
- price
- qty_in_stock
- variation_option_ids
type: object
request.RefreshToken:
properties:
refresh_token:
Expand Down Expand Up @@ -1724,12 +1700,29 @@ paths:
name: product_id
required: true
type: integer
- description: Product item input
in: body
name: input
- description: Price
in: formData
name: price
required: true
schema:
$ref: '#/definitions/request.ProductItem'
type: integer
- description: Quantity In Stock
in: formData
name: qty_in_stock
required: true
type: integer
- collectionFormat: csv
description: Variation Option IDs
in: formData
items:
type: integer
name: variation_option_ids
required: true
type: array
- description: Images
in: formData
name: images
required: true
type: file
produces:
- application/json
responses:
Expand Down
51 changes: 35 additions & 16 deletions pkg/api/handler/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -398,33 +399,51 @@ func (c *ProductHandler) UpdateProduct(ctx *gin.Context) {
}

// SaveProductItem godoc
// @Summary Add a product item (Admin)
// @Description API for admin to add a product item for a specific product(should select at least one variation option from each variations)
// @ID SaveProductItem
// @Tags Admin Products
// @Accept json
// @Produce json
// @Param product_id path int true "Product ID"
// @Param input body request.ProductItem{} true "Product item input"
// @Router /admin/products/{product_id}/items [post]
// @Success 200 {object} response.Response{} "Successfully product item added"
// @Failure 400 {object} response.Response{} "invalid input"
// @Failure 409 {object} response.Response{} "Product have already this configured product items exist"
//
// @Summary Add a product item (Admin)
// @Description API for admin to add a product item for a specific product(should select at least one variation option from each variations)
// @ID SaveProductItem
// @Tags Admin Products
// @Accept json
// @Produce json
// @Param product_id path int true "Product ID"
// @Param price formData int true "Price"
// @Param qty_in_stock formData int true "Quantity In Stock"
// @Param variation_option_ids formData []int true "Variation Option IDs"
// @Param images formData file true "Images"
// @Router /admin/products/{product_id}/items [post]
// @Success 200 {object} response.Response{} "Successfully product item added"
// @Failure 400 {object} response.Response{} "invalid input"
// @Failure 409 {object} response.Response{} "Product have already this configured product items exist"
func (p *ProductHandler) SaveProductItem(ctx *gin.Context) {

productID, err := request.GetParamAsUint(ctx, "product_id")
if err != nil {
response.ErrorResponse(ctx, http.StatusBadRequest, BindParamFailMessage, err, nil)
}

var body request.ProductItem
price, err1 := request.GetFormValuesAsUint(ctx, "price")
qtyInStock, err2 := request.GetFormValuesAsUint(ctx, "qty_in_stock")
variationOptionIDS, err3 := request.GetArrayFormValueAsUint(ctx, "variation_option_ids")
imageFileHeaders, err4 := request.GetArrayOfFromFiles(ctx, "images")

if err := ctx.ShouldBindJSON(&body); err != nil {
response.ErrorResponse(ctx, http.StatusBadRequest, BindJsonFailMessage, err, nil)
err = errors.Join(err1, err2, err3, err4)

if err != nil {
response.ErrorResponse(ctx, http.StatusBadRequest, BindFormValueMessage, err, nil)
return
}

err = p.productUseCase.SaveProductItem(ctx, productID, body)
productItem := request.ProductItem{
Price: price,
VariationOptionIDs: variationOptionIDS,
QtyInStock: qtyInStock,
ImageFileHeaders: imageFileHeaders,
}

fmt.Println(productItem, productID)

err = p.productUseCase.SaveProductItem(ctx, productID, productItem)

if err != nil {

Expand Down
10 changes: 5 additions & 5 deletions pkg/api/handler/request/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ type UpdateProduct struct {

// for a new productItem
type ProductItem struct {
Price uint `json:"price" binding:"required,min=1"`
VariationOptionIDs []uint `json:"variation_option_ids" binding:"required,gte=1"`
QtyInStock uint `json:"qty_in_stock" binding:"required,min=1"`
SKU string `json:"-"`
Images []string `json:"images" binding:"required,gte=1"`
Price uint `json:"price" binding:"required,min=1"`
VariationOptionIDs []uint `json:"variation_option_ids" binding:"required,gte=1"`
QtyInStock uint `json:"qty_in_stock" binding:"required,min=1"`
SKU string `json:"-"`
ImageFileHeaders []*multipart.FileHeader `json:"images" binding:"required,gte=1"`
}

type Variation struct {
Expand Down
Loading

0 comments on commit 6e62eb8

Please sign in to comment.