newsbot-api/internal/handler/v1/handler.go

152 lines
4.3 KiB
Go
Raw Normal View History

2024-04-23 22:18:07 -07:00
package v1
import (
"context"
"database/sql"
"github.com/labstack/echo/v4"
_ "github.com/lib/pq"
swagger "github.com/swaggo/echo-swagger"
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
"git.jamestombleson.com/jtom38/newsbot-api/internal/services/dto"
)
type Handler struct {
Router *echo.Echo
Db *database.Queries
dto *dto.DtoClient
//ctx *context.Context
}
const (
HeaderContentType = "Content-Type"
ApplicationJson = "application/json"
ErrParameterIdMissing = "The requested parameter ID was not found."
ErrParameterMissing = "The requested parameter was found found:"
ErrUnableToParseId = "Unable to parse the requested ID."
ErrRecordMissing = "The requested record was not found"
)
var (
ErrIdValueMissing string = "id value is missing"
ErrValueNotUuid string = "a value given was expected to be a uuid but was not correct."
ErrNoRecordFound string = "no record was found."
ErrUnableToConvertToJson string = "Unable to convert to json"
)
func NewServer(ctx context.Context, db *database.Queries) *Handler {
s := &Handler{
//ctx: &ctx,
Db: db,
dto: dto.NewDtoClient(db),
}
db, err := openDatabase(ctx)
if err != nil {
panic(err)
}
s.Db = db
router := echo.New()
router.GET("/swagger/*", swagger.WrapHandler)
v1 := router.Group("/api/v1")
articles := v1.Group("/articles")
articles.GET("/", s.listArticles)
articles.GET("/:id", s.getArticle)
articles.GET("/:id/details", s.getArticleDetails)
articles.GET("/by/source/:id", s.ListArticlesBySourceId)
dwh := v1.Group("/discord/webhooks")
dwh.GET("/", s.ListDiscordWebHooks)
dwh.POST("/new", s.NewDiscordWebHook)
dwh.GET("/by/serverAndChannel", s.GetDiscordWebHooksByServerAndChannel)
dwh.GET("/:ID", s.GetDiscordWebHooksById)
dwh.DELETE("/:ID", s.deleteDiscordWebHook)
dwh.POST("/:ID/disable", s.disableDiscordWebHook)
dwh.POST("/:ID/enable", s.enableDiscordWebHook)
queue := v1.Group("/queue")
queue.GET("/discord/webhooks", s.ListDiscordWebhookQueue) // TODO this needs to be reworked
settings := v1.Group("/settings")
settings.GET("/", s.getSettings)
sources := v1.Group("/sources")
sources.GET("/", s.listSources)
sources.GET("/by/source", s.listSourcesBySource)
sources.GET("/by/sourceAndName", s.GetSourceBySourceAndName)
sources.POST("/new/reddit", s.newRedditSource)
sources.POST("/new/youtube", s.newYoutubeSource)
sources.POST("/new/twitch", s.newTwitchSource)
sources.GET("/:ID/", s.getSources)
sources.DELETE("/:ID/", s.deleteSources)
sources.POST("/:ID/disable", s.disableSource)
sources.POST("/:ID/enable", s.enableSource)
subs := v1.Group("/subscriptions")
subs.GET("/", s.ListSubscriptions)
subs.GET("/details", s.ListSubscriptionDetails)
subs.GET("/by/discordId", s.GetSubscriptionsByDiscordId)
subs.GET("/by/sourceId", s.GetSubscriptionsBySourceId)
subs.POST("/discord/webhook/new", s.newDiscordWebHookSubscription)
subs.DELETE("/discord/webhook/delete", s.DeleteDiscordWebHookSubscription)
return s
}
func openDatabase(ctx context.Context) (*database.Queries, error) {
_env := services.NewConfig()
connString := _env.GetConfig(services.Sql_Connection_String)
db, err := sql.Open("postgres", connString)
if err != nil {
panic(err)
}
queries := database.New(db)
return queries, err
}
func (s *Handler) MountRoutes() {
//s.Router.Get("/swagger/*", httpSwagger.Handler(
// httpSwagger.URL("doc.json"), //The url pointing to API definition
//))
//s.Router.Get("/api/settings", s.getSettings)
//s.Router.Mount("/api/sources", s.GetSourcesRouter())
//s.Router.Mount("/api/subscriptions", s.GetSubscriptionsRouter())
}
type ApiStatusModel struct {
StatusCode int `json:"status"`
Message string `json:"message"`
}
type ApiError struct {
*ApiStatusModel
}
func (s *Handler) WriteError(c echo.Context, errMessage string, HttpStatusCode int) error {
return c.JSON(HttpStatusCode, domain.ErrorResponse{
Message: errMessage,
})
}
//func (s *Handler) WriteJson(w http.ResponseWriter, model interface{}) {
// w.Header().Set(HeaderContentType, ApplicationJson)
//
// bres, err := json.Marshal(model)
// if err != nil {
// s.WriteError(w, err.Error(), http.StatusInternalServerError)
// return
// }
//
// w.Write(bres)
//}