com.snow.auto_monitor/app/http/controllers/whitelist/whitelist.go

170 lines
3.1 KiB
Go

package whitelist
import (
"time"
"com.snow.auto_monitor/app/constants/errorcode"
common "com.snow.auto_monitor/app/http/controllers"
wlCon "com.snow.auto_monitor/app/http/entities/whitelist"
wlMod "com.snow.auto_monitor/app/models/whitelist"
wlServ "com.snow.auto_monitor/app/services/whitelist"
"github.com/gin-gonic/gin"
)
func GetById(c *gin.Context) {
request := new(wlCon.GetListByIdReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
res, err := wlServ.GetById(request.Id)
if err != nil {
common.Error500(c)
return
}
var response *wlCon.GetListByIdResp = nil
if res != nil {
response = &wlCon.GetListByIdResp{
Id: res.Id,
MerchantId: res.MerchantId,
Ip: res.Ip,
CreatedAt: res.CreatedAt.Format(time.RFC3339),
}
}
common.Success(c, response)
}
func Search(c *gin.Context) {
request := new(wlCon.SearcgReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
start, end := "", ""
if len(request.CreatedAt) > 1 {
start = request.CreatedAt[0]
end = request.CreatedAt[1]
}
res, err := wlServ.Search(
request.Id,
request.MerchantId,
request.Ip,
start,
end,
request.PageSize,
request.PageNum,
)
if err != nil {
common.Error500(c)
return
}
var response []*wlCon.SearchResp = nil
if len(res) > 0 {
for _, item := range res {
response = append(response, &wlCon.SearchResp{
Id: item.Id,
MerchantId: item.MerchantId,
Ip: item.Ip,
CreatedAt: item.CreatedAt.Format(time.RFC3339),
})
}
}
total, err := wlServ.CountAll(
request.Id,
request.MerchantId,
request.Ip,
start,
end,
)
if err != nil {
common.Error500(c)
return
}
common.SuccessWithList(c, response, total)
}
func Create(c *gin.Context) {
request := new(wlCon.CreateReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
whitelist := &wlMod.Whitelist{
MerchantId: request.MerchantId,
Ip: request.Ip,
}
affected, err := wlServ.Create(whitelist)
if err != nil && affected > 0 {
common.Error500(c)
return
}
response := &wlCon.CreateResp{
Id: whitelist.Id,
}
common.Success(c, response)
}
func Update(c *gin.Context) {
request := new(wlCon.UpdateReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
whitelist := &wlMod.Whitelist{
Id: request.Id,
MerchantId: request.MerchantId,
Ip: request.Ip,
}
affected, err := wlServ.Update(whitelist)
if err != nil && affected > 0 {
common.Error500(c)
return
}
response := &wlCon.UpdateResp{
Id: whitelist.Id,
}
common.Success(c, response)
}
func Delete(c *gin.Context) {
request := new(wlCon.DeleteReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
affected, err := wlServ.Delete(request.Id)
if err != nil && affected > 0 {
common.Error500(c)
return
}
response := &wlCon.DeleteResp{
Id: request.Id,
}
common.Success(c, response)
}