diff --git a/app/http/endpoints/api/getpermissionlevel.go b/app/http/endpoints/api/getpermissionlevel.go index 6090df2..99c8122 100644 --- a/app/http/endpoints/api/getpermissionlevel.go +++ b/app/http/endpoints/api/getpermissionlevel.go @@ -1,29 +1,30 @@ package api import ( - "context" + "github.com/TicketsBot/GoPanel/app" "github.com/TicketsBot/GoPanel/utils" "github.com/gin-gonic/gin" + "net/http" "strconv" ) -func GetPermissionLevel(ctx *gin.Context) { - userId := ctx.Keys["userid"].(uint64) +func GetPermissionLevel(c *gin.Context) { + userId := c.Keys["userid"].(uint64) - guildId, err := strconv.ParseUint(ctx.Query("guild"), 10, 64) + guildId, err := strconv.ParseUint(c.Query("guild"), 10, 64) if err != nil { - ctx.JSON(400, utils.ErrorStr("Invalid guild ID")) + c.JSON(400, utils.ErrorStr("Invalid guild ID")) return } // TODO: Use proper context - permissionLevel, err := utils.GetPermissionLevel(context.Background(), guildId, userId) + permissionLevel, err := utils.GetPermissionLevel(c, guildId, userId) if err != nil { - ctx.JSON(500, utils.ErrorJson(err)) + _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err)) return } - ctx.JSON(200, gin.H{ + c.JSON(200, gin.H{ "success": true, "permission_level": permissionLevel, }) diff --git a/app/http/endpoints/api/ticket/closeticket.go b/app/http/endpoints/api/ticket/closeticket.go index baded9a..0e6de69 100644 --- a/app/http/endpoints/api/ticket/closeticket.go +++ b/app/http/endpoints/api/ticket/closeticket.go @@ -2,11 +2,13 @@ package api import ( "context" + "github.com/TicketsBot/GoPanel/app" "github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/redis" "github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/common/closerelay" "github.com/gin-gonic/gin" + "net/http" "strconv" ) @@ -14,55 +16,44 @@ type closeBody struct { Reason string `json:"reason"` } -func CloseTicket(ctx *gin.Context) { - userId := ctx.Keys["userid"].(uint64) - guildId := ctx.Keys["guildid"].(uint64) +func CloseTicket(c *gin.Context) { + userId := c.Keys["userid"].(uint64) + guildId := c.Keys["guildid"].(uint64) - ticketId, err := strconv.Atoi(ctx.Param("ticketId")) + ticketId, err := strconv.Atoi(c.Param("ticketId")) if err != nil { - ctx.JSON(400, gin.H{ - "success": true, - "error": "Invalid ticket ID", - }) + c.JSON(http.StatusBadRequest, utils.ErrorStr("Invalid ticket ID")) return } var body closeBody - if err := ctx.BindJSON(&body); err != nil { - ctx.JSON(400, gin.H{ - "success": false, - "error": "Missing reason", - }) + if err := c.BindJSON(&body); err != nil { + c.JSON(400, utils.ErrorStr("Invalid request body")) return } // Get the ticket struct - ticket, err := database.Client.Tickets.Get(ctx, ticketId, guildId) + ticket, err := database.Client.Tickets.Get(c, ticketId, guildId) if err != nil { - ctx.JSON(500, gin.H{ - "success": true, - "error": err.Error(), - }) + _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err)) return } // Verify the ticket exists if ticket.UserId == 0 { - ctx.JSON(404, gin.H{ - "success": false, - "error": "Ticket does not exist", - }) + c.JSON(http.StatusNotFound, utils.ErrorStr("Ticket not found")) return } hasPermission, requestErr := utils.HasPermissionToViewTicket(context.Background(), guildId, userId, ticket) if requestErr != nil { - ctx.JSON(requestErr.StatusCode, utils.ErrorJson(requestErr)) + // TODO + c.JSON(requestErr.StatusCode, utils.ErrorJson(requestErr)) return } if !hasPermission { - ctx.JSON(403, utils.ErrorStr("You do not have permission to close this ticket")) + c.JSON(http.StatusForbidden, utils.ErrorStr("You do not have permission to close this ticket")) return } @@ -74,9 +65,9 @@ func CloseTicket(ctx *gin.Context) { } if err := closerelay.Publish(redis.Client.Client, data); err != nil { - ctx.JSON(500, utils.ErrorJson(err)) + _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err)) return } - ctx.JSON(200, utils.SuccessResponse) + c.JSON(200, utils.SuccessResponse) } diff --git a/app/http/endpoints/api/ticket/getticket.go b/app/http/endpoints/api/ticket/getticket.go index 78a9724..5ffada1 100644 --- a/app/http/endpoints/api/ticket/getticket.go +++ b/app/http/endpoints/api/ticket/getticket.go @@ -2,6 +2,7 @@ package api import ( "context" + "github.com/TicketsBot/GoPanel/app" "github.com/TicketsBot/GoPanel/botcontext" dbclient "github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/utils" @@ -11,6 +12,7 @@ import ( "github.com/rxdn/gdl/objects/channel/embed" "github.com/rxdn/gdl/objects/user" "github.com/rxdn/gdl/rest" + "net/http" "regexp" "strconv" "time" @@ -18,83 +20,63 @@ import ( var MentionRegex, _ = regexp.Compile("<@(\\d+)>") -func GetTicket(ctx *gin.Context) { - guildId := ctx.Keys["guildid"].(uint64) - userId := ctx.Keys["userid"].(uint64) +func GetTicket(c *gin.Context) { + guildId := c.Keys["guildid"].(uint64) + userId := c.Keys["userid"].(uint64) botContext, err := botcontext.ContextForGuild(guildId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err)) return } - ticketId, err := strconv.Atoi(ctx.Param("ticketId")) + ticketId, err := strconv.Atoi(c.Param("ticketId")) if err != nil { - ctx.JSON(400, gin.H{ - "success": true, - "error": "Invalid ticket ID", - }) + c.JSON(http.StatusBadRequest, utils.ErrorStr("Invalid ticket ID")) return } // Get the ticket struct - ticket, err := dbclient.Client.Tickets.Get(ctx, ticketId, guildId) + ticket, err := dbclient.Client.Tickets.Get(c, ticketId, guildId) if err != nil { - ctx.JSON(500, gin.H{ - "success": true, - "error": err.Error(), - }) + _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err)) return } if ticket.GuildId != guildId { - ctx.JSON(403, gin.H{ - "success": false, - "error": "Guild ID doesn't match", - }) + c.JSON(http.StatusForbidden, utils.ErrorStr("Ticket does not belong to guild")) return } if !ticket.Open { - ctx.JSON(404, gin.H{ - "success": false, - "error": "Ticket does not exist", - }) + c.JSON(http.StatusNotFound, utils.ErrorStr("Ticket is closed")) return } - hasPermission, requestErr := utils.HasPermissionToViewTicket(context.Background(), guildId, userId, ticket) + hasPermission, requestErr := utils.HasPermissionToViewTicket(c, guildId, userId, ticket) if requestErr != nil { - ctx.JSON(requestErr.StatusCode, utils.ErrorJson(requestErr)) + // TODO + c.JSON(requestErr.StatusCode, utils.ErrorJson(requestErr)) return } if !hasPermission { - ctx.JSON(403, utils.ErrorStr("You do not have permission to view this ticket")) + c.JSON(http.StatusForbidden, utils.ErrorStr("You do not have permission to view this ticket")) return } if ticket.ChannelId == nil { - ctx.JSON(404, gin.H{ - "success": false, - "error": "Channel ID is nil", - }) + c.JSON(http.StatusNotFound, utils.ErrorStr("Ticket channel not found")) return } messages, err := fetchMessages(botContext, ticket) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err)) return } - ctx.JSON(200, gin.H{ + c.JSON(200, gin.H{ "success": true, "ticket": ticket, "messages": messages, diff --git a/app/http/endpoints/api/ticket/gettickets.go b/app/http/endpoints/api/ticket/gettickets.go index d5a6ce8..524d483 100644 --- a/app/http/endpoints/api/ticket/gettickets.go +++ b/app/http/endpoints/api/ticket/gettickets.go @@ -1,11 +1,12 @@ package api import ( + "github.com/TicketsBot/GoPanel/app" "github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/rpc/cache" - "github.com/TicketsBot/GoPanel/utils" "github.com/gin-gonic/gin" "github.com/rxdn/gdl/objects/user" + "net/http" "time" ) @@ -28,19 +29,19 @@ type ( } ) -func GetTickets(ctx *gin.Context) { - userId := ctx.Keys["userid"].(uint64) - guildId := ctx.Keys["guildid"].(uint64) +func GetTickets(c *gin.Context) { + userId := c.Keys["userid"].(uint64) + guildId := c.Keys["guildid"].(uint64) - tickets, err := database.Client.Tickets.GetGuildOpenTicketsWithMetadata(ctx, guildId) + tickets, err := database.Client.Tickets.GetGuildOpenTicketsWithMetadata(c, guildId) if err != nil { - ctx.JSON(500, utils.ErrorJson(err)) + _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err)) return } - panels, err := database.Client.Panel.GetByGuild(ctx, guildId) + panels, err := database.Client.Panel.GetByGuild(c, guildId) if err != nil { - ctx.JSON(500, utils.ErrorJson(err)) + _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err)) return } @@ -59,9 +60,9 @@ func GetTickets(ctx *gin.Context) { } } - users, err := cache.Instance.GetUsers(ctx, userIds) + users, err := cache.Instance.GetUsers(c, userIds) if err != nil { - ctx.JSON(500, utils.ErrorJson(err)) + _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err)) return } @@ -78,7 +79,7 @@ func GetTickets(ctx *gin.Context) { } } - ctx.JSON(200, listTicketsResponse{ + c.JSON(200, listTicketsResponse{ Tickets: data, PanelTitles: panelTitles, ResolvedUsers: users,