allowanddenyip.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package handler
  2. import (
  3. "github.com/gin-gonic/gin"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  6. "net/http"
  7. )
  8. type AllowAndDenyIpHandler struct {
  9. *Handler
  10. allowAndDenyIpService service.AllowAndDenyIpService
  11. }
  12. func NewAllowAndDenyIpHandler(
  13. handler *Handler,
  14. allowAndDenyIpService service.AllowAndDenyIpService,
  15. ) *AllowAndDenyIpHandler {
  16. return &AllowAndDenyIpHandler{
  17. Handler: handler,
  18. allowAndDenyIpService: allowAndDenyIpService,
  19. }
  20. }
  21. func (h *AllowAndDenyIpHandler) GetAllowAndDenyIp(ctx *gin.Context) {
  22. var req v1.AllowAndDenyIpRequest
  23. if err := ctx.ShouldBind(req); err != nil {
  24. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  25. return
  26. }
  27. res, err := h.allowAndDenyIpService.GetAllowAndDenyIp(ctx, int64(req.Id))
  28. if err != nil {
  29. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  30. return
  31. }
  32. v1.HandleSuccess(ctx, res)
  33. }
  34. func (h *AllowAndDenyIpHandler) GetAllowAndDenyIpList(ctx *gin.Context) {
  35. var req v1.AllowAndDenyIpRequest
  36. if err := ctx.ShouldBind(req); err != nil {
  37. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  38. return
  39. }
  40. res, err := h.allowAndDenyIpService.GetAllowAndDenyIpsAllByHostId(ctx, int64(req.HostId))
  41. if err != nil {
  42. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  43. return
  44. }
  45. v1.HandleSuccess(ctx, res)
  46. }
  47. func (h *AllowAndDenyIpHandler) AddAllowAndDenyIp(ctx *gin.Context) {
  48. var req v1.AllowAndDenyIpRequest
  49. if err := ctx.ShouldBind(req); err != nil {
  50. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  51. return
  52. }
  53. err := h.allowAndDenyIpService.AddAllowAndDenyIps(ctx,req)
  54. if err != nil {
  55. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  56. return
  57. }
  58. v1.HandleSuccess(ctx, nil)
  59. }
  60. func (h *AllowAndDenyIpHandler) EditAllowAndDenyIp(ctx *gin.Context) {
  61. var req v1.AllowAndDenyIpRequest
  62. if err := ctx.ShouldBind(req); err != nil {
  63. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  64. return
  65. }
  66. err := h.allowAndDenyIpService.EditAllowAndDenyIps(ctx,req)
  67. if err != nil {
  68. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  69. return
  70. }
  71. v1.HandleSuccess(ctx, nil)
  72. }
  73. func (h *AllowAndDenyIpHandler) DeleteAllowAndDenyIp(ctx *gin.Context) {
  74. var req v1.DelAllowAndDenyIpRequest
  75. if err := ctx.ShouldBind(req); err != nil {
  76. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  77. return
  78. }
  79. err := h.allowAndDenyIpService.DeleteAllowAndDenyIps(ctx,req)
  80. if err != nil {
  81. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  82. return
  83. }
  84. v1.HandleSuccess(ctx, nil)
  85. }