features/repo-updates #4

Merged
jtom38 merged 13 commits from features/repo-updates into main 2024-04-28 11:42:58 -07:00
2 changed files with 45 additions and 46 deletions
Showing only changes of commit bcbdfcbc5b - Show all commits

View File

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

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