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

@ -19,18 +19,20 @@ type Handler struct {
Db *database.Queries
dto *dto.DtoClient
config services.Configs
sqlConnection *sql.DB
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 (
@ -45,15 +47,9 @@ func NewServer(ctx context.Context, db *database.Queries, configs services.Confi
Db: db,
dto: dto.NewDtoClient(db),
config: configs,
sqlConnection: conn,
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,
})
}

View File

@ -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),
}
}