add: custom kpi features
This commit is contained in:
113
features/pm/kpi_c_title/controller.go
Normal file
113
features/pm/kpi_c_title/controller.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package kpi_c_title
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (k *KpiCTitle) Get(c *gin.Context) {
|
||||
var titles []KpiCTitle
|
||||
var conditions []string
|
||||
var params []any
|
||||
|
||||
// construct condition to get
|
||||
if neType := c.Query("neType"); neType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.ToUpper(neType))
|
||||
}
|
||||
if status := c.Query("status"); status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, status)
|
||||
}
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += strings.Join(conditions, " and ")
|
||||
}
|
||||
if err := datasource.DefaultDB().Where(whereSql, params...).Find(&titles).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
//c.JSON(http.StatusOK, map[string]any{"data": titles})
|
||||
c.JSON(http.StatusOK, titles)
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) Total(c *gin.Context) {
|
||||
var conditions []string
|
||||
var params []any
|
||||
|
||||
// construct condition to get
|
||||
if neType := c.Query("neType"); neType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.ToUpper(neType))
|
||||
}
|
||||
if status := c.Query("status"); status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, status)
|
||||
}
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += strings.Join(conditions, " and ")
|
||||
}
|
||||
var total int64 = 0
|
||||
if err := datasource.DefaultDB().Table(k.TableName()).Where(whereSql, params...).Count(&total).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, map[string]any{"total": total})
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) Post(c *gin.Context) {
|
||||
var title KpiCTitle
|
||||
|
||||
if err := c.ShouldBindJSON(&title); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := datasource.DefaultDB().Create(&title).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, title)
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) Put(c *gin.Context) {
|
||||
var title KpiCTitle
|
||||
id := c.Param("id")
|
||||
|
||||
if err := datasource.DefaultDB().First(&title, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Title not found"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&title); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
datasource.DefaultDB().Save(&title)
|
||||
c.JSON(http.StatusOK, title)
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) Delete(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if err := datasource.DefaultDB().Delete(&KpiCTitle{}, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Title not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNoContent, nil) // 204 No Content
|
||||
}
|
||||
|
||||
func GetActiveKPICList(neType string) []KpiCTitle {
|
||||
k := new([]KpiCTitle)
|
||||
|
||||
err := datasource.DefaultDB().Where("`ne_type` = ? and `status` = 'Active'", neType).Find(&k).Error
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return *k
|
||||
}
|
||||
19
features/pm/kpi_c_title/model.go
Normal file
19
features/pm/kpi_c_title/model.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package kpi_c_title
|
||||
|
||||
import "time"
|
||||
|
||||
type KpiCTitle struct {
|
||||
ID int `gorm:"column:id;primary_key;auto_increment" json:"id"`
|
||||
NeType *string `gorm:"column:ne_type;default:NULL," json:"ne_type,omitempty"`
|
||||
KpiID *string `gorm:"column:kpi_id;default:NULL," json:"kpi_id,omitempty"`
|
||||
Title *string `gorm:"column:title;default:NULL," json:"title,omitempty"`
|
||||
Expression *string `gorm:"column:expression;default:NULL," json:"expression,omitempty"`
|
||||
Status *string `gorm:"column:status" json:"status,omitempty"`
|
||||
Description *string `gorm:"column:description;default:NULL," json:"description,omitempty"`
|
||||
CreatedBy *string `gorm:"column:created_by;default:NULL," json:"created_by,omitempty"`
|
||||
UpdatedAt *time.Time `gorm:"column:updated_at;default:current_timestamp()," json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) TableName() string {
|
||||
return "kpi_c_title"
|
||||
}
|
||||
35
features/pm/kpi_c_title/route.go
Normal file
35
features/pm/kpi_c_title/route.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package kpi_c_title
|
||||
|
||||
import (
|
||||
"be.ems/src/framework/middleware"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Register Routes for kpi_c_title
|
||||
func Register(r *gin.RouterGroup) {
|
||||
|
||||
pmKPIC := r.Group("/kpiC")
|
||||
{
|
||||
var k *KpiCTitle
|
||||
pmKPIC.GET("/title",
|
||||
middleware.PreAuthorize(nil),
|
||||
k.Get,
|
||||
)
|
||||
pmKPIC.GET("/title/total",
|
||||
middleware.PreAuthorize(nil),
|
||||
k.Total,
|
||||
)
|
||||
pmKPIC.POST("/title",
|
||||
middleware.PreAuthorize(nil),
|
||||
k.Post,
|
||||
)
|
||||
pmKPIC.PUT("/title/:id",
|
||||
middleware.PreAuthorize(nil),
|
||||
k.Put,
|
||||
)
|
||||
pmKPIC.DELETE("/title/:id",
|
||||
middleware.PreAuthorize(nil),
|
||||
k.Delete,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user