From bcbdfcbc5b6054aaf485075d1cafe2110a1b4006 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 28 Apr 2024 11:40:51 -0700 Subject: [PATCH] Created a new services.RepositoryService to roll up all the db calls --- internal/handler/v1/handler.go | 70 ++++++++++++---------------------- internal/services/database.go | 21 +++++++++- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/internal/handler/v1/handler.go b/internal/handler/v1/handler.go index 26947c2..655f72a 100644 --- a/internal/handler/v1/handler.go +++ b/internal/handler/v1/handler.go @@ -15,22 +15,24 @@ import ( ) type Handler struct { - Router *echo.Echo - Db *database.Queries - dto *dto.DtoClient - config services.Configs - sqlConnection *sql.DB + Router *echo.Echo + Db *database.Queries + dto *dto.DtoClient + config services.Configs + repo services.RepositoryService } const ( HeaderContentType = "Content-Type" - ApplicationJson = "application/json" + //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" + + ResponseMessageSuccess = "Success" ) var ( @@ -42,18 +44,12 @@ var ( func NewServer(ctx context.Context, db *database.Queries, configs services.Configs, conn *sql.DB) *Handler { s := &Handler{ - Db: db, - dto: dto.NewDtoClient(db), - config: configs, - sqlConnection: conn, + Db: db, + dto: dto.NewDtoClient(db), + config: configs, + repo: services.NewRepositoryService(conn), } - db, err := openDatabase(ctx) - if err != nil { - panic(err) - } - s.Db = db - router := echo.New() router.GET("/swagger/*", swagger.WrapHandler) @@ -73,11 +69,11 @@ func NewServer(ctx context.Context, db *database.Queries, configs services.Confi 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 + //queue := v1.Group("/queue") + //queue.GET("/discord/webhooks", s.ListDiscordWebhookQueue) // TODO this needs to be reworked - settings := v1.Group("/settings") - settings.GET("/", s.getSettings) + //settings := v1.Group("/settings") + //settings.GET("/", s.getSettings) sources := v1.Group("/sources") sources.GET("/", s.listSources) @@ -103,28 +99,6 @@ func NewServer(ctx context.Context, db *database.Queries, configs services.Confi 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"` @@ -134,8 +108,14 @@ 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) WriteError(c echo.Context, errMessage error, HttpStatusCode int) error { + return c.JSON(HttpStatusCode, domain.BaseResponse{ + Message: errMessage.Error(), + }) +} + +func (s *Handler) WriteMessage(c echo.Context, msg string, HttpStatusCode int) error { + return c.JSON(HttpStatusCode, domain.BaseResponse{ + Message: msg, }) } diff --git a/internal/services/database.go b/internal/services/database.go index 80eb9ab..9d11878 100644 --- a/internal/services/database.go +++ b/internal/services/database.go @@ -1,6 +1,25 @@ package services -type RepositoryService struct { +import ( + "database/sql" + "git.jamestombleson.com/jtom38/newsbot-api/internal/repository" +) + +type RepositoryService struct { + Articles repository.ArticlesRepo + DiscordWebHooks repository.DiscordWebHookRepo + Sources repository.Sources + Users repository.Users + RefreshTokens repository.RefreshToken } +func NewRepositoryService(conn *sql.DB) RepositoryService { + return RepositoryService{ + Articles: repository.NewArticleRepository(conn), + DiscordWebHooks: repository.NewDiscordWebHookRepository(conn), + Sources: repository.NewSourceRepository(conn), + Users: repository.NewUserRepository(conn), + RefreshTokens: repository.NewRefreshTokenRepository(conn), + } +}