Created a new services.RepositoryService to roll up all the db calls

This commit is contained in:
James Tombleson 2024-04-28 11:40:51 -07:00
parent ef15af6cbd
commit bcbdfcbc5b
2 changed files with 45 additions and 46 deletions

View File

@ -15,22 +15,24 @@ import (
) )
type Handler struct { type Handler struct {
Router *echo.Echo Router *echo.Echo
Db *database.Queries Db *database.Queries
dto *dto.DtoClient dto *dto.DtoClient
config services.Configs config services.Configs
sqlConnection *sql.DB repo services.RepositoryService
} }
const ( const (
HeaderContentType = "Content-Type" HeaderContentType = "Content-Type"
ApplicationJson = "application/json" //ApplicationJson = "application/json"
ErrParameterIdMissing = "The requested parameter ID was not found." ErrParameterIdMissing = "The requested parameter ID was not found."
ErrParameterMissing = "The requested parameter was found found:" ErrParameterMissing = "The requested parameter was found found:"
ErrUnableToParseId = "Unable to parse the requested ID." ErrUnableToParseId = "Unable to parse the requested ID."
ErrRecordMissing = "The requested record was not found" ErrRecordMissing = "The requested record was not found"
ResponseMessageSuccess = "Success"
) )
var ( var (
@ -42,18 +44,12 @@ var (
func NewServer(ctx context.Context, db *database.Queries, configs services.Configs, conn *sql.DB) *Handler { func NewServer(ctx context.Context, db *database.Queries, configs services.Configs, conn *sql.DB) *Handler {
s := &Handler{ s := &Handler{
Db: db, Db: db,
dto: dto.NewDtoClient(db), dto: dto.NewDtoClient(db),
config: configs, config: configs,
sqlConnection: conn, repo: services.NewRepositoryService(conn),
} }
db, err := openDatabase(ctx)
if err != nil {
panic(err)
}
s.Db = db
router := echo.New() router := echo.New()
router.GET("/swagger/*", swagger.WrapHandler) 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/disable", s.disableDiscordWebHook)
dwh.POST("/:ID/enable", s.enableDiscordWebHook) dwh.POST("/:ID/enable", s.enableDiscordWebHook)
queue := v1.Group("/queue") //queue := v1.Group("/queue")
queue.GET("/discord/webhooks", s.ListDiscordWebhookQueue) // TODO this needs to be reworked //queue.GET("/discord/webhooks", s.ListDiscordWebhookQueue) // TODO this needs to be reworked
settings := v1.Group("/settings") //settings := v1.Group("/settings")
settings.GET("/", s.getSettings) //settings.GET("/", s.getSettings)
sources := v1.Group("/sources") sources := v1.Group("/sources")
sources.GET("/", s.listSources) sources.GET("/", s.listSources)
@ -103,28 +99,6 @@ func NewServer(ctx context.Context, db *database.Queries, configs services.Confi
return s 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 { type ApiStatusModel struct {
StatusCode int `json:"status"` StatusCode int `json:"status"`
Message string `json:"message"` Message string `json:"message"`
@ -134,8 +108,14 @@ type ApiError struct {
*ApiStatusModel *ApiStatusModel
} }
func (s *Handler) WriteError(c echo.Context, errMessage string, HttpStatusCode int) error { func (s *Handler) WriteError(c echo.Context, errMessage error, HttpStatusCode int) error {
return c.JSON(HttpStatusCode, domain.ErrorResponse{ return c.JSON(HttpStatusCode, domain.BaseResponse{
Message: errMessage, Message: errMessage.Error(),
})
}
func (s *Handler) WriteMessage(c echo.Context, msg string, HttpStatusCode int) error {
return c.JSON(HttpStatusCode, domain.BaseResponse{
Message: msg,
}) })
} }

View File

@ -1,6 +1,25 @@
package services 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),
}
}