package v1 import ( "net/http" "strconv" "strings" "git.jamestombleson.com/jtom38/newsbot-api/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/dtoconv" "github.com/labstack/echo/v4" ) // ListDiscordWebhooks // @Summary Returns the top 100 // @Produce application/json // @Tags DiscordWebhook // @Router /v1/discord/webhooks [get] // @Success 200 {object} domain.DiscordWebhookResponse // @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse // @Security Bearer func (s *Handler) ListDiscordWebHooks(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, IsError: true, }, } _, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebhookRead) if err != nil { return c.JSON(http.StatusUnauthorized, p) } res, err := s.repo.DiscordWebHooks.ListByServerName(c.Request().Context(), "") if err != nil { return c.JSON(http.StatusInternalServerError, p) } p.Payload = dtoconv.DiscordWebhooksToDto(res) p.BaseResponse.IsError = false return c.JSON(http.StatusOK, p) } // GetDiscordWebHook // @Summary Returns the top 100 entries from the queue to be processed. // @Produce application/json // @Param id path int true "id" // @Tags DiscordWebhook // @Router /v1/discord/webhooks/{id} [get] // @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse // @Security Bearer func (s *Handler) GetDiscordWebHooksById(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, IsError: true, }, } _, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebhookRead) if err != nil { return c.JSON(http.StatusUnauthorized, p) } id, err := strconv.Atoi(c.Param("ID")) if err != nil { return c.JSON(http.StatusBadRequest, p) } res, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { return c.JSON(http.StatusInternalServerError, p) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, dtoconv.DiscordWebhookToDto(res)) p.Payload = dtos p.BaseResponse.IsError = false return c.JSON(http.StatusOK, p) } // GetDiscordWebHookByServerAndChannel // @Summary Returns all the known web hooks based on the Server and Channel given. // @Produce application/json // @Param server query string true "Fancy Server" // @Param channel query string true "memes" // @Tags DiscordWebhook // @Router /v1/discord/webhooks/by/serverAndChannel [get] // @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse // @Security Bearer func (s *Handler) GetDiscordWebHooksByServerAndChannel(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, IsError: true, }, } _, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebhookRead) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusUnauthorized, p) } _server := c.QueryParam("server") if _server == "" { p.BaseResponse.Message = "server was not defined" return c.JSON(http.StatusBadRequest, p) } _channel := c.QueryParam("channel") if _channel == "" { p.BaseResponse.Message = "channel was not defined" return c.JSON(http.StatusBadRequest, p) } res, err := s.repo.DiscordWebHooks.ListByServerAndChannel(c.Request().Context(), _server, _channel) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } p.Payload = dtoconv.DiscordWebhooksToDto(res) p.IsError = false return c.JSON(http.StatusOK, p) } // NewDiscordWebHook // @Summary Creates a new record for a discord web hook to post data to. // @Param url query string true "url" // @Param server query string true "Server name" // @Param channel query string true "Channel name" // @Tags DiscordWebhook // @Router /v1/discord/webhooks/new [post] // @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse // @Security Bearer func (s *Handler) NewDiscordWebHook(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, IsError: true, }, } token, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebHookCreate) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusUnauthorized, p) } _url := c.QueryParam("url") _server := c.QueryParam("server") _channel := c.QueryParam("channel") if _url == "" { p.Message = "url is missing a value" return c.JSON(http.StatusBadRequest, p) } if !strings.Contains(_url, "discord.com/api/webhooks") { p.Message = "invalid url" return c.JSON(http.StatusBadRequest, p) } if _server == "" { p.Message = "server is missing" return c.JSON(http.StatusBadRequest, p) } if _channel == "" { p.Message = "channel is missing" return c.JSON(http.StatusBadRequest, p) } user, err := s.repo.Users.GetUser(c.Request().Context(), token.UserName) if err != nil { p.Message = err.Error() return c.JSON(http.StatusBadRequest, p) } rows, err := s.repo.DiscordWebHooks.Create(c.Request().Context(), user.ID, _url, _server, _channel, true) if err != nil { p.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } if rows != 1 { p.Message = "data was not written to database" return c.JSON(http.StatusInternalServerError, p) } item, err := s.repo.DiscordWebHooks.GetByUrl(c.Request().Context(), _url) if err != nil { p.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, dtoconv.DiscordWebhookToDto(item)) p.Payload = dtos p.IsError = false return c.JSON(http.StatusOK, p) } // DisableDiscordWebHooks // @Summary Disables a Webhook from being used. // @Param id path int true "id" // @Tags DiscordWebhook // @Router /v1/discord/webhooks/{ID}/disable [post] // @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse // @Security Bearer func (s *Handler) disableDiscordWebHook(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, IsError: true, }, } _, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebHookCreate) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusUnauthorized, p) } id, err := strconv.Atoi(c.Param("ID")) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusBadRequest, p) } // Check to make sure we can find the record record, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } if record.UserID != s.GetUserIdFromJwtToken(c) { p.BaseResponse.Message = ErrYouDontOwnTheRecord return c.JSON(http.StatusBadRequest, p) } // flip the it updated, err := s.repo.DiscordWebHooks.Disable(c.Request().Context(), int64(id)) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } // make sure we got a row updated if updated != 1 { p.BaseResponse.Message = "unexpected number of updates found" return c.JSON(http.StatusInternalServerError, p) } item, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, dtoconv.DiscordWebhookToDto(item)) p.Payload = dtos p.IsError = false return c.JSON(http.StatusOK, p) } // EnableDiscordWebHook // @Summary Enables a source to continue processing. // @Param id path int true "id" // @Tags DiscordWebhook // @Router /v1/discord/webhooks/{ID}/enable [post] // @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse // @Security Bearer func (s *Handler) enableDiscordWebHook(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, IsError: true, }, } _, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebHookCreate) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusUnauthorized, p) } id, err := strconv.Atoi(c.Param("ID")) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusBadRequest, p) } // Check to make sure we can find the record record, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusBadRequest, p) } if record.UserID != s.GetUserIdFromJwtToken(c) { p.BaseResponse.Message = ErrYouDontOwnTheRecord return c.JSON(http.StatusBadRequest, p) } updated, err := s.repo.DiscordWebHooks.Enable(c.Request().Context(), int64(id)) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } if updated != 1 { p.BaseResponse.Message = ErrFailedToUpdateRecord return c.JSON(http.StatusInternalServerError, p) } item, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, dtoconv.DiscordWebhookToDto(item)) p.Payload = dtos p.IsError = false return c.JSON(http.StatusOK, p) } // DeleteDiscordWebHook // @Summary Deletes a record by ID. // @Param id path string true "id" // @Tags DiscordWebhook // @Router /v1/discord/webhooks/{ID} [delete] // @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Failure 400 {object} domain.DiscordWebhookResponse // @Failure 500 {object} domain.DiscordWebhookResponse // @Security Bearer func (s *Handler) deleteDiscordWebHook(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, IsError: true, }, } _, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebHookCreate) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusUnauthorized, p) } id, err := strconv.Atoi(c.Param("ID")) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusBadRequest, p) } // Check to make sure we can find the record record, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusBadRequest, p) } if record.UserID != s.GetUserIdFromJwtToken(c) { p.BaseResponse.Message = ErrYouDontOwnTheRecord return c.JSON(http.StatusBadRequest, p) } // Soft delete the record updated, err := s.repo.DiscordWebHooks.SoftDelete(c.Request().Context(), int64(id)) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } if updated != 1 { p.BaseResponse.Message = ErrFailedToUpdateRecord return c.JSON(http.StatusInternalServerError, p) } item, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { p.BaseResponse.Message = err.Error() return c.JSON(http.StatusInternalServerError, p) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, dtoconv.DiscordWebhookToDto(item)) p.Payload = dtos p.BaseResponse.IsError = false return c.JSON(http.StatusOK, p) } // UpdateDiscordWebHook // @Summary Updates a valid discord webhook ID based on the body given. // @Param id path string true "id" // @Tags DiscordWebhook // @Router /v1/discord/webhooks/{id} [patch] // @Success 200 {object} domain.DiscordWebhookResponse "OK" // @Failure 400 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse //func (s *Handler) UpdateDiscordWebHook(c echo.Context) error { // id, err := strconv.Atoi(c.Param("ID")) // if err != nil { // return c.JSON(http.StatusInternalServerError, err.Error()) // } // // // Check to make sure we can find the record // _, err = s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) // if err != nil { // return c.JSON(http.StatusInternalServerError, err.Error()) // } // // // Soft delete the record // updated, err := s.repo.DiscordWebHooks(c.Request().Context(), int64(id)) // if err != nil { // return c.JSON(http.StatusInternalServerError, err.Error()) // } // // _, err = s.Db.GetDiscordQueueByID(c.Request().Context(), uuid) // if err != nil { // return c.JSON(http.StatusInternalServerError, err.Error()) // } // // return nil //}