- test implementation of config controller;
This commit is contained in:
parent
310a3d9f55
commit
a496c1998d
47
cmd/main.go
47
cmd/main.go
|
|
@ -7,11 +7,15 @@ import (
|
|||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"ticket-pimp/bot/handler"
|
||||
discordbot "ticket-pimp/discord-bot"
|
||||
"ticket-pimp/internal/controller"
|
||||
"ticket-pimp/internal/domain"
|
||||
"ticket-pimp/internal/services"
|
||||
db "ticket-pimp/internal/storage/db/tickets"
|
||||
|
||||
discordbot "ticket-pimp/discord-bot"
|
||||
configDB "ticket-pimp/internal/storage/db/config"
|
||||
ticketDB "ticket-pimp/internal/storage/db/tickets"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/joho/godotenv"
|
||||
|
|
@ -22,7 +26,39 @@ import (
|
|||
func main() {
|
||||
log.Print("started")
|
||||
config := env("develop.env")
|
||||
run(config)
|
||||
// run(config)
|
||||
test(config)
|
||||
}
|
||||
|
||||
func test(conf domain.Config) {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
conn, err := pgx.Connect(
|
||||
ctx,
|
||||
fmt.Sprintf(
|
||||
"postgresql://%s:%s@%s:%s/%s",
|
||||
conf.DB.User,
|
||||
conf.DB.Pass,
|
||||
conf.DB.Host,
|
||||
conf.DB.Port,
|
||||
conf.DB.Name,
|
||||
))
|
||||
if err != nil {
|
||||
log.Fatalf("DB connection failed: %v", err)
|
||||
}
|
||||
|
||||
q := configDB.New(conn)
|
||||
// appconfig, err := q.GetConfig(ctx)
|
||||
|
||||
// appconfigNew, err := q.SetNewConfig(ctx)
|
||||
|
||||
// _ = appconfig
|
||||
// _ = appconfigNew
|
||||
|
||||
appController := controller.NewAppConfig(q)
|
||||
ticketConfig, err := appController.NewKey(ctx)
|
||||
_ = ticketConfig
|
||||
|
||||
}
|
||||
|
||||
func run(conf domain.Config) {
|
||||
|
|
@ -45,7 +81,7 @@ func run(conf domain.Config) {
|
|||
defer conn.Close(ctx)
|
||||
|
||||
opts := TelegramOptions{
|
||||
ticketsRepo: db.New(conn),
|
||||
ticketsRepo: ticketDB.New(conn),
|
||||
gitService: services.NewGit(conf.Git),
|
||||
cloudService: services.NewCloud(conf.Cloud),
|
||||
coda: services.NewCodaClient(conf.Coda),
|
||||
|
|
@ -56,6 +92,7 @@ func run(conf domain.Config) {
|
|||
if err := runDiscrodBot(conf); err != nil {
|
||||
log.Fatalf("discord bot cannot be runned: %v", err)
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
if err := runTgBot(ctx, opts); err != nil {
|
||||
|
|
@ -143,7 +180,7 @@ func runDiscrodBot(conf domain.Config) error {
|
|||
}
|
||||
|
||||
type TelegramOptions struct {
|
||||
ticketsRepo *db.Queries
|
||||
ticketsRepo *ticketDB.Queries
|
||||
gitService *services.Git
|
||||
cloudService *services.Cloud
|
||||
coda *services.Coda
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ticket-pimp/internal/domain"
|
||||
db "ticket-pimp/internal/storage/db/config"
|
||||
)
|
||||
|
||||
type IConfigController interface {
|
||||
Get(context.Context) (domain.ApplicationConfig, error)
|
||||
Update(context.Context) (domain.ApplicationConfig, error)
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
db *db.Queries
|
||||
}
|
||||
|
||||
func NewAppConfig(db *db.Queries) AppConfig {
|
||||
return AppConfig{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (ac *AppConfig) Get(ctx context.Context) (*domain.ApplicationConfig, error) {
|
||||
c, err := ac.db.GetConfig(ctx)
|
||||
return &domain.ApplicationConfig{
|
||||
Key: c.TicketKey.String,
|
||||
ID: int(c.TicketID.Int32),
|
||||
}, err
|
||||
}
|
||||
|
||||
func (ac *AppConfig) NewKey(ctx context.Context) (*domain.ApplicationConfig, error) {
|
||||
c, err := ac.db.SetNewConfig(ctx)
|
||||
return &domain.ApplicationConfig{
|
||||
Key: c.TicketKey.String,
|
||||
ID: int(c.TicketID.Int32),
|
||||
}, err
|
||||
}
|
||||
|
|
@ -43,3 +43,8 @@ type TelegramConfig struct {
|
|||
type DiscordConfig struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
type ApplicationConfig struct {
|
||||
Key string
|
||||
ID int
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.23.0
|
||||
// source: config.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const getConfig = `-- name: GetConfig :one
|
||||
SELECT ticket_key, ticket_id
|
||||
FROM appconfig
|
||||
`
|
||||
|
||||
func (q *Queries) GetConfig(ctx context.Context) (Appconfig, error) {
|
||||
row := q.db.QueryRow(ctx, getConfig)
|
||||
var i Appconfig
|
||||
err := row.Scan(&i.TicketKey, &i.TicketID)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const setNewConfig = `-- name: SetNewConfig :one
|
||||
UPDATE appconfig
|
||||
SET ticket_id = ticket_id + 1
|
||||
RETURNING ticket_key, ticket_id
|
||||
`
|
||||
|
||||
func (q *Queries) SetNewConfig(ctx context.Context) (Appconfig, error) {
|
||||
row := q.db.QueryRow(ctx, setNewConfig)
|
||||
var i Appconfig
|
||||
err := row.Scan(&i.TicketKey, &i.TicketID)
|
||||
return i, err
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.23.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.23.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Appconfig struct {
|
||||
TicketKey pgtype.Text
|
||||
TicketID pgtype.Int4
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
-- +migrate Up
|
||||
CREATE TABLE appconfig (
|
||||
ticket_key VARCHAR(5),
|
||||
ticket_id INT
|
||||
);
|
||||
|
||||
-- +migrate Down
|
||||
DROP TABLE appconfig;
|
||||
|
|
@ -2,9 +2,17 @@ version: "2"
|
|||
sql:
|
||||
- engine: "postgresql"
|
||||
queries: "sqlc/tickets.sql"
|
||||
schema: "migrate"
|
||||
schema: "migrate/0001_init_tickets.sql"
|
||||
gen:
|
||||
go:
|
||||
package: "db"
|
||||
sql_package: "pgx/v5"
|
||||
out: "db/tickets"
|
||||
- engine: "postgresql"
|
||||
queries: "sqlc/config.sql"
|
||||
schema: "migrate/0002_init_config.sql"
|
||||
gen:
|
||||
go:
|
||||
package: "db"
|
||||
sql_package: "pgx/v5"
|
||||
out: "db/config"
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
-- name: GetConfig :one
|
||||
SELECT ticket_key, ticket_id
|
||||
FROM appconfig;
|
||||
|
||||
-- name: SetNewConfig :one
|
||||
UPDATE appconfig
|
||||
SET ticket_id = ticket_id + 1
|
||||
RETURNING *;
|
||||
Loading…
Reference in New Issue