package v1 import ( "net/http" "strconv" "strings" "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/services" "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.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) ListDiscordWebHooks(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } res, err := s.repo.DiscordWebHooks.ListByServerName(c.Request().Context(), "") if err != nil { return c.JSON(http.StatusInternalServerError, err) } p.Payload = services.DiscordWebhooksToDto(res) 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.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) GetDiscordWebHooksById(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } id, err := strconv.Atoi(c.Param("ID")) if err != nil { s.WriteError(c, err, http.StatusBadRequest) } res, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, services.DiscordWebhookToDto(res)) p.Payload = dtos 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.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) GetDiscordWebHooksByServerAndChannel(c echo.Context) error { p := domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } _server := c.QueryParam("server") if _server == "" { s.WriteMessage(c, "server was not defined", http.StatusBadRequest) } _channel := c.QueryParam("channel") if _channel == "" { s.WriteMessage(c, "channel was not defined", http.StatusBadRequest) } res, err := s.repo.DiscordWebHooks.ListByServerAndChannel(c.Request().Context(), _server, _channel) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } p.Payload = services.DiscordWebhooksToDto(res) 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.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) NewDiscordWebHook(c echo.Context) error { _url := c.QueryParam("url") _server := c.QueryParam("server") _channel := c.QueryParam("channel") if _url == "" { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "url is missing a value", }) } if !strings.Contains(_url, "discord.com/api/webhooks") { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "invalid url", }) } if _server == "" { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "server is missing", }) } if _channel == "" { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "channel is missing", }) } rows, err := s.repo.DiscordWebHooks.Create(c.Request().Context(), _url, _server, _channel, true) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } if rows != 1 { s.WriteMessage(c, "data was not written to database", http.StatusInternalServerError) } item, err := s.repo.DiscordWebHooks.GetByUrl(c.Request().Context(), _url) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, services.DiscordWebhookToDto(item)) return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, Payload: dtos, }) } // 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.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) disableDiscordWebHook(c echo.Context) error { id, err := strconv.Atoi(c.Param("ID")) if err != nil { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } // Check to make sure we can find the record _, err = s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } // flip the it updated, err := s.repo.DiscordWebHooks.Disable(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } // make sure we got a row updated if updated != 1 { s.WriteMessage(c, "unexpected number of updates found", http.StatusInternalServerError) } item, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, services.DiscordWebhookToDto(item)) return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, Payload: dtos, }) } // EnableDiscordWebHook // @Summary Enables a source to continue processing. // @Param id path int true "id" // @Tags DiscordWebhook // @Router /v1/discord/webhooks/{ID}/enable [post] func (s *Handler) enableDiscordWebHook(c echo.Context) error { id, err := strconv.Atoi(c.Param("ID")) if err != nil { s.WriteError(c, err, http.StatusBadRequest) } // Check to make sure we can find the record _, err = s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusBadRequest) } updated, err := s.repo.DiscordWebHooks.Enable(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } if updated != 1 { s.WriteMessage(c, "unexpected number of updates found", http.StatusInternalServerError) } item, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, services.DiscordWebhookToDto(item)) return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, Payload: dtos, }) } // 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.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) deleteDiscordWebHook(c echo.Context) error { id, err := strconv.Atoi(c.Param("ID")) if err != nil { return c.JSON(http.StatusBadRequest, 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.SoftDelete(c.Request().Context(), int64(id)) if err != nil { return c.JSON(http.StatusInternalServerError, err.Error()) } if updated != 1 { s.WriteMessage(c, "unexpected number of updates found", http.StatusInternalServerError) } item, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dtos []domain.DiscordWebHookDto dtos = append(dtos, services.DiscordWebhookToDto(item)) return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, Payload: dtos, }) } // 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 //}