package v1 import ( "context" "encoding/json" "fmt" "net/http" "strconv" "strings" "git.jamestombleson.com/jtom38/newsbot-api/internal/database" "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models" "git.jamestombleson.com/jtom38/newsbot-api/internal/services" "github.com/google/uuid" "github.com/labstack/echo/v4" ) type ListSources struct { ApiStatusModel Payload []models.SourceDto `json:"payload"` } type GetSource struct { ApiStatusModel Payload models.SourceDto `json:"payload"` } // ListSources // @Summary Lists the top 50 records // @Param page query string false "page number" // @Produce application/json // @Tags Source // @Router /sources [get] // @Success 200 {object} domain.SourcesResponse "ok" // @Failure 400 {object} domain.BaseResponse "Unable to reach SQL or Data problems" func (s *Handler) listSources(c echo.Context) error { resp := domain.SourcesResponse { BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } page, err := strconv.Atoi(c.QueryParam("page")) if err != nil { page = 0 } // Default way of showing all sources items, err := s.repo.Sources.List(c.Request().Context(), page, 25) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } resp.Payload = services.SourcesToDto(items) return c.JSON(http.StatusOK, resp) } // ListSourcesBySource // @Summary Lists the top 50 records based on the name given. Example: reddit // @Param source query string true "Source Name" // @Param page query string false "page number" // @Produce application/json // @Tags Source // @Router /sources/by/source [get] // @Success 200 {object} domain.SourcesResponse "ok" // @Failure 400 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) listSourcesBySource(c echo.Context) error { resp := domain.SourcesResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } source := c.QueryParam("source") if source == "" { s.WriteMessage(c, fmt.Sprintf("%s source", ErrParameterMissing), http.StatusBadRequest) } page, err := strconv.Atoi(c.QueryParam("page")) if err != nil { page = 0 } // Shows the list by Sources.source items, err := s.repo.Sources.ListBySource(c.Request().Context(), page, 25, source) if err != nil { return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } resp.Payload = services.SourcesToDto(items) return c.JSON(http.StatusOK, resp) } // GetSource // @Summary Returns a single entity by ID // @Param id path int true "uuid" // @Produce application/json // @Tags Source // @Router /sources/{id} [get] // @Success 200 {object} domain.SourcesResponse "ok" // @Failure 400 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) getSource(c echo.Context) error { resp := domain.SourcesResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } id, err := strconv.Atoi(c.Param("ID")) if err != nil { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: ErrUnableToParseId, }) } item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dto []domain.SourceDto dto = append(dto, services.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } // GetSourceByNameAndSource // @Summary Returns a single entity by ID // @Param name query string true "dadjokes" // @Param source query string true "reddit" // @Produce application/json // @Tags Source // @Router /sources/by/sourceAndName [get] // @Success 200 {object} domain.SourcesResponse "ok" // @Failure 400 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) GetSourceBySourceAndName(c echo.Context) error { resp := domain.SourcesResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } var param domain.GetSourceBySourceAndNameParamRequest err := c.Bind(¶m) if err != nil { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), param.Source, param.Name) if err != nil { return c.JSON(http.StatusInternalServerError, err.Error()) } var dto []domain.SourceDto dto = append(dto, services.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } // NewRedditSource // @Summary Creates a new reddit source to monitor. // @Param name query string true "name" // @Param url query string true "url" // @Tags Source // @Router /sources/new/reddit [post] // @Success 200 {object} domain.SourcesResponse "ok" // @Failure 400 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) newRedditSource(c echo.Context) error { resp := domain.SourcesResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } var param domain.NewSourceParamRequest err := c.Bind(¶m) if err != nil { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } if param.Url == "" { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "Url is missing a value", }) } if !strings.Contains(param.Url, "reddit.com") { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "Invalid URL given", }) } tags := fmt.Sprintf("twitch, %v, %s", param.Name, param.Tags) rows, err := s.repo.Sources.Create(c.Request().Context(), domain.SourceCollectorReddit, param.Name, param.Url, tags, true) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } if rows != 1 { s.WriteMessage(c, ErrFailedToCreateRecord, http.StatusInternalServerError) } item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorReddit, param.Name) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dto []domain.SourceDto dto = append(dto, services.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } // NewYoutubeSource // @Summary Creates a new youtube source to monitor. // @Param name query string true "name" // @Param url query string true "url" // @Tags Source // @Router /sources/new/youtube [post] func (s *Handler) newYoutubeSource(c echo.Context) error { var param domain.NewSourceParamRequest err := c.Bind(¶m) if err != nil { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } //query := r.URL.Query() //_name := query["name"][0] //_url := query["url"][0] ////_tags := query["tags"][0] if param.Url == "" { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "url is missing a value", }) } if !strings.Contains(param.Url, "youtube.com") { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "Invalid URL", }) } /* if _tags == "" { tags = fmt.Sprintf("twitch, %v", _name) } else { } */ tags := fmt.Sprintf("twitch, %v", param.Name) params := database.CreateSourceParams{ ID: uuid.New(), Site: "youtube", Name: param.Name, Source: "youtube", Type: "feed", Enabled: true, Url: param.Url, Tags: tags, } err = s.Db.CreateSource(context.Background(), params) if err != nil { return c.JSON(http.StatusInternalServerError, err.Error()) } bJson, err := json.Marshal(¶ms) if err != nil { return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } return c.JSON(http.StatusOK, bJson) } // NewTwitchSource // @Summary Creates a new twitch source to monitor. // @Param name query string true "name" // @Tags Source // @Router /sources/new/twitch [post] func (s *Handler) newTwitchSource(c echo.Context) error { var param domain.NewSourceParamRequest err := c.Bind(¶m) if err != nil { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } //query := r.URL.Query() //_name := query["name"][0] tags := fmt.Sprintf("twitch, %v", param.Name) _url := fmt.Sprintf("https://twitch.tv/%v", param.Name) params := database.CreateSourceParams{ ID: uuid.New(), Site: "twitch", Name: param.Name, Source: "twitch", Type: "api", Enabled: true, Url: _url, Tags: tags, } err = s.Db.CreateSource(c.Request().Context(), params) if err != nil { return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } bJson, err := json.Marshal(¶ms) if err != nil { return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } return c.JSON(http.StatusOK, bJson) } // NewRssSource // @Summary Creates a new rss source to monitor. // @Param name query string true "Site Name" // @Param url query string true "RSS Url" // @Tags Source // @Router /sources/new/rss [post] // @Success 200 {object} domain.SourcesResponse "ok" // @Failure 400 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) newRssSource(c echo.Context) error { resp := domain.SourcesResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } var param domain.NewSourceParamRequest err := c.Bind(¶m) if err != nil { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } if param.Url == "" { return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "Url is missing a value", }) } tags := fmt.Sprintf("rss, %v, %s", param.Name, param.Tags) rows, err := s.repo.Sources.Create(c.Request().Context(), domain.SourceCollectorRss, param.Name, param.Url, tags, true) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } if rows != 1 { s.WriteMessage(c, ErrFailedToCreateRecord, http.StatusInternalServerError) } item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorRss, param.Name) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dto []domain.SourceDto dto = append(dto, services.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } // DeleteSource // @Summary Marks a source as deleted based on its ID value. // @Param id path string true "id" // @Tags Source // @Router /sources/{id} [POST] func (s *Handler) deleteSources(c echo.Context) error { id := c.Param("ID") uuid, err := uuid.Parse(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.Db.GetSourceByID(c.Request().Context(), uuid) if err != nil { return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } // Delete the record err = s.Db.DeleteSource(c.Request().Context(), uuid) if err != nil { return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } p := ApiStatusModel{ Message: "OK", StatusCode: http.StatusOK, } b, err := json.Marshal(p) if err != nil { return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } return c.JSON(http.StatusOK, b) } // DisableSource // @Summary Disables a source from processing. // @Param id path int true "id" // @Tags Source // @Router /sources/{id}/disable [post] // @Success 200 {object} domain.SourcesResponse "ok" // @Failure 400 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) disableSource(c echo.Context) error { resp := domain.SourcesResponse { BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } 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.Sources.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusBadRequest) } _, err = s.repo.Sources.Disable(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dto []domain.SourceDto dto = append(dto, services.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } // EnableSource // @Summary Enables a source to continue processing. // @Param id path string true "id" // @Tags Source // @Router /sources/{id}/enable [post] // @Success 200 {object} domain.SourcesResponse "ok" // @Failure 400 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse func (s *Handler) enableSource(c echo.Context) error { resp := domain.SourcesResponse { BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, }, } 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.Sources.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusBadRequest) } _, err = s.repo.Sources.Enable(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id)) if err != nil { s.WriteError(c, err, http.StatusInternalServerError) } var dto []domain.SourceDto dto = append(dto, services.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) }