casdoor/controllers/product.go

194 lines
5.2 KiB
Go
Raw Normal View History

2022-02-27 18:20:58 +08:00
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"encoding/json"
2022-03-12 20:03:48 +08:00
"fmt"
2022-02-27 18:20:58 +08:00
2022-09-29 19:44:08 +08:00
"github.com/beego/beego/utils/pagination"
2022-02-27 18:20:58 +08:00
"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/util"
)
// GetProducts
// @Title GetProducts
// @Tag Product API
// @Description get products
// @Param owner query string true "The owner of products"
// @Success 200 {array} object.Product The Response object
// @router /get-products [get]
func (c *ApiController) GetProducts() {
owner := c.Input().Get("owner")
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")
2022-02-27 18:20:58 +08:00
if limit == "" || page == "" {
products, err := object.GetProducts(owner)
if err != nil {
c.ResponseError(err.Error())
return
}
c.Data["json"] = products
2022-02-27 18:20:58 +08:00
c.ServeJSON()
} else {
limit := util.ParseInt(limit)
count, err := object.GetProductCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}
paginator := pagination.SetPaginator(c.Ctx, limit, count)
products, err := object.GetPaginationProducts(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}
2022-02-27 18:20:58 +08:00
c.ResponseOk(products, paginator.Nums())
}
}
2022-08-09 16:50:49 +08:00
// GetProduct
2022-02-27 18:20:58 +08:00
// @Title GetProduct
// @Tag Product API
// @Description get product
// @Param id query string true "The id ( owner/name ) of the product"
2022-02-27 18:20:58 +08:00
// @Success 200 {object} object.Product The Response object
// @router /get-product [get]
func (c *ApiController) GetProduct() {
id := c.Input().Get("id")
product, err := object.GetProduct(id)
if err != nil {
c.ResponseError(err.Error())
return
}
err = object.ExtendProductWithProviders(product)
if err != nil {
c.ResponseError(err.Error())
return
}
2022-08-07 15:44:57 +08:00
c.Data["json"] = product
2022-02-27 18:20:58 +08:00
c.ServeJSON()
}
2022-08-09 16:50:49 +08:00
// UpdateProduct
2022-02-27 18:20:58 +08:00
// @Title UpdateProduct
// @Tag Product API
// @Description update product
// @Param id query string true "The id ( owner/name ) of the product"
2022-02-27 18:20:58 +08:00
// @Param body body object.Product true "The details of the product"
// @Success 200 {object} controllers.Response The Response object
// @router /update-product [post]
func (c *ApiController) UpdateProduct() {
id := c.Input().Get("id")
var product object.Product
err := json.Unmarshal(c.Ctx.Input.RequestBody, &product)
if err != nil {
c.ResponseError(err.Error())
return
2022-02-27 18:20:58 +08:00
}
c.Data["json"] = wrapActionResponse(object.UpdateProduct(id, &product))
c.ServeJSON()
}
2022-08-09 16:50:49 +08:00
// AddProduct
2022-02-27 18:20:58 +08:00
// @Title AddProduct
// @Tag Product API
// @Description add product
// @Param body body object.Product true "The details of the product"
// @Success 200 {object} controllers.Response The Response object
// @router /add-product [post]
func (c *ApiController) AddProduct() {
var product object.Product
err := json.Unmarshal(c.Ctx.Input.RequestBody, &product)
if err != nil {
c.ResponseError(err.Error())
return
2022-02-27 18:20:58 +08:00
}
c.Data["json"] = wrapActionResponse(object.AddProduct(&product))
c.ServeJSON()
}
2022-08-09 16:50:49 +08:00
// DeleteProduct
2022-02-27 18:20:58 +08:00
// @Title DeleteProduct
// @Tag Product API
// @Description delete product
// @Param body body object.Product true "The details of the product"
// @Success 200 {object} controllers.Response The Response object
// @router /delete-product [post]
func (c *ApiController) DeleteProduct() {
var product object.Product
err := json.Unmarshal(c.Ctx.Input.RequestBody, &product)
if err != nil {
c.ResponseError(err.Error())
return
2022-02-27 18:20:58 +08:00
}
c.Data["json"] = wrapActionResponse(object.DeleteProduct(&product))
c.ServeJSON()
}
2022-03-06 22:46:02 +08:00
2022-08-09 16:50:49 +08:00
// BuyProduct
2022-03-06 22:46:02 +08:00
// @Title BuyProduct
// @Tag Product API
// @Description buy product
// @Param id query string true "The id ( owner/name ) of the product"
2022-03-14 02:07:55 +08:00
// @Param providerName query string true "The name of the provider"
2022-03-06 22:46:02 +08:00
// @Success 200 {object} controllers.Response The Response object
// @router /buy-product [post]
func (c *ApiController) BuyProduct() {
id := c.Input().Get("id")
2022-03-14 02:07:55 +08:00
providerName := c.Input().Get("providerName")
2022-03-06 22:46:02 +08:00
host := c.Ctx.Request.Host
2022-03-12 20:03:48 +08:00
userId := c.GetSessionUsername()
if userId == "" {
2023-01-06 20:12:32 +08:00
c.ResponseError(c.T("general:Please login first"))
2022-03-12 20:03:48 +08:00
return
}
user, err := object.GetUser(userId)
if err != nil {
c.ResponseError(err.Error())
return
}
2022-03-12 20:03:48 +08:00
if user == nil {
2023-01-06 20:12:32 +08:00
c.ResponseError(fmt.Sprintf(c.T("general:The user: %s doesn't exist"), userId))
2022-03-12 20:03:48 +08:00
return
}
payUrl, orderId, err := object.BuyProduct(id, providerName, user, host)
2022-03-06 22:46:02 +08:00
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(payUrl, orderId)
2022-03-06 22:46:02 +08:00
}