initial commit

This commit is contained in:
2025-02-03 18:16:07 -05:00
commit 8ef3c029f2
19 changed files with 1354 additions and 0 deletions

8
config.yml Normal file
View File

@ -0,0 +1,8 @@
---
database:
dsn: "postgres://bun:bun@localhost:5432/bun?sslmode=disable"
timezone: America/New_York
logging:
level: DEBUG
output: JSON
source: false

61
config/config.go Normal file
View File

@ -0,0 +1,61 @@
package config
import (
"log/slog"
"os"
"github.com/spf13/viper"
)
// Config .
type Config struct {
DataBase *DataBase `json:"database" yaml:"database"`
Timezone string `json:"timezone" yaml:"timezone"`
Logging *Logging `json:"logging" yaml:"logging"`
}
// DB .
type DataBase struct {
DSN string `json:"dsn" yaml:"dsn"`
}
// Logging .
type Logging struct {
Level string `json:"level" yaml:"level"`
Output string `json:"output" yaml:"output"`
Source bool `json:"source,omitempty" yaml:"source,omitempty"`
}
// New .
func New() (*Config, error) {
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("./") // optionally look for config in the working directory
viper.AddConfigPath("/etc/tracker") // optionally look for config in the working directory
viper.AutomaticEnv()
viper.SetDefault("Timezone", "America/New_York")
viper.SetDefault("Logging.Level", "DEBUG")
viper.SetDefault("Logging.Output", "TEXT")
viper.SetDefault("Logging.Source", "false")
err := viper.ReadInConfig() // Find and read the config file
if err != nil {
slog.Error("cannot open config file", "error", err.Error())
os.Exit(1)
}
// viper.WatchConfig()
// viper.OnConfigChange(func(e fsnotify.Event) {
// log.Println("Config file changed:", e.Name)
// })
var config Config
if err := viper.Unmarshal(&config); err != nil {
slog.Error("cannot unmarshal config file", "error", err.Error())
os.Exit(1)
}
return &config, err
}

48
database/database.go Normal file
View File

@ -0,0 +1,48 @@
package database
import (
"context"
"database/sql"
"log/slog"
"git.savin.nyc/alex/go-receipt-tracker-api/config"
"git.savin.nyc/alex/go-receipt-tracker-api/models"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
)
func NewDB(cfg *config.DataBase, log *slog.Logger) (*bun.DB, error) {
ctx := context.Background()
// dsn := "postgres://bun:bun@localhost:5432/bun?sslmode=disable"
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(cfg.DSN)))
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
// Register models before loading fixtures.
db.RegisterModel((*models.GroupToUser)(nil), (*models.User)(nil), (*models.Group)(nil), (*models.Receipt)(nil), (*models.Merchant)(nil), (*models.CreditCard)(nil), (*models.Item)(nil), (*models.WhiteList)(nil), (*models.BlackList)(nil))
models := []interface{}{
(*models.User)(nil),
(*models.Group)(nil),
(*models.GroupToUser)(nil),
(*models.Receipt)(nil),
(*models.Merchant)(nil),
(*models.CreditCard)(nil),
(*models.Item)(nil),
(*models.WhiteList)(nil),
(*models.BlackList)(nil),
// (*models.Settings)(nil),
}
for _, model := range models {
if _, err := db.NewCreateTable().Model(model).Exec(ctx); err != nil {
continue
}
}
return db, nil
}

72
go.mod Normal file
View File

@ -0,0 +1,72 @@
module git.savin.nyc/alex/go-receipt-tracker-api
go 1.23
require (
github.com/gin-contrib/cors v1.7.3
github.com/gin-gonic/gin v1.10.0
github.com/rs/cors v1.11.1
github.com/spf13/viper v1.19.0
github.com/uptrace/bun v1.2.9
github.com/uptrace/bun/dialect/pgdialect v1.2.9
github.com/uptrace/bun/driver/pgdriver v1.2.9
github.com/uptrace/bun/extra/bundebug v1.2.9
)
require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/bytedance/sonic v1.12.8 // indirect
github.com/bytedance/sonic/loader v0.2.3 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gin-contrib/sse v1.0.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.24.0 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.9 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/puzpuzpuz/xsync/v3 v3.5.0 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.9-0.20240816141633-0a40785b4f41 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.13.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/protobuf v1.36.4 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mellium.im/sasl v0.3.2 // indirect
)

208
go.sum Normal file
View File

@ -0,0 +1,208 @@
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/bytedance/sonic v1.12.5 h1:hoZxY8uW+mT+OpkcUWw4k0fDINtOcVavEsGfzwzFU/w=
github.com/bytedance/sonic v1.12.5/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
github.com/bytedance/sonic v1.12.8 h1:4xYRVRlXIgvSZ4e8iVTlMF5szgpXd4AfvuWgA8I8lgs=
github.com/bytedance/sonic v1.12.8/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iCjjJv3+E=
github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.2.3 h1:yctD0Q3v2NOGfSWPLPvG2ggA2kV6TS6s4wioyEqssH0=
github.com/bytedance/sonic/loader v0.2.3/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/gabriel-vasile/mimetype v1.4.7 h1:SKFKl7kD0RiPdbht0s7hFtjl489WcQ1VyPW8ZzUMYCA=
github.com/gabriel-vasile/mimetype v1.4.7/go.mod h1:GDlAgAyIRT27BhFl53XNAFtfjzOkLaF35JdEG0P7LtU=
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw=
github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E=
github.com/gin-contrib/cors v1.7.3 h1:hV+a5xp8hwJoTw7OY+a70FsL8JkVVFTXw9EcfrYUdns=
github.com/gin-contrib/cors v1.7.3/go.mod h1:M3bcKZhxzsvI+rlRSkkxHyljJt1ESd93COUvemZ79j4=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o=
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-playground/validator/v10 v10.24.0 h1:KHQckvo8G6hlWnrPX4NJJ+aBfWNAE/HH+qdL2cBpCmg=
github.com/go-playground/validator/v10 v10.24.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/magiconair/properties v1.8.9 h1:nWcCbLq1N2v/cpNsy5WvQ37Fb+YElfq20WJ/a8RkpQM=
github.com/magiconair/properties v1.8.9/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4=
github.com/puzpuzpuz/xsync/v3 v3.4.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
github.com/puzpuzpuz/xsync/v3 v3.5.0 h1:i+cMcpEDY1BkNm7lPDkCtE4oElsYLn+EKF8kAu2vXT4=
github.com/puzpuzpuz/xsync/v3 v3.5.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk=
github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0=
github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/uptrace/bun v1.2.6 h1:lyGBQAhNiClchb97HA2cBnDeRxwTRLhSIgiFPXVisV8=
github.com/uptrace/bun v1.2.6/go.mod h1:xMgnVFf+/5xsrFBU34HjDJmzZnXbVuNEt/Ih56I8qBU=
github.com/uptrace/bun v1.2.9 h1:OOt2DlIcRUMSZPr6iXDFg/LaQd59kOxbAjpIVHddKRs=
github.com/uptrace/bun v1.2.9/go.mod h1:r2ZaaGs9Ru5bpGTr8GQfp8jp+TlCav9grYCPOu2CJSg=
github.com/uptrace/bun/dialect/pgdialect v1.2.6 h1:iNd1YLx619K+sZK+dRcWPzluurXYK1QwIkp9FEfNB/8=
github.com/uptrace/bun/dialect/pgdialect v1.2.6/go.mod h1:OL7d3qZLxKYP8kxNhMg3IheN1pDR3UScGjoUP+ivxJQ=
github.com/uptrace/bun/dialect/pgdialect v1.2.9 h1:caf5uFbOGiXvadV6pA5gn87k0awFFxL1kuuY3SpxnWk=
github.com/uptrace/bun/dialect/pgdialect v1.2.9/go.mod h1:m7L9JtOp/Lt8HccET70ULxplMweE/u0S9lNUSxz2duo=
github.com/uptrace/bun/driver/pgdriver v1.2.6 h1:dD7ckqIhVDayfYTwMKZ0dJM9AZfNJNBu5Cg/8g0EMOk=
github.com/uptrace/bun/driver/pgdriver v1.2.6/go.mod h1:ChrVrMZlRzPQHTP4QCm/p1FfQqgnYXWlES0GS9qjWEY=
github.com/uptrace/bun/driver/pgdriver v1.2.9 h1:wPXQwD78mYeR7o5tQTM/tgBaVd5QWMN/Nq02h+zHlsI=
github.com/uptrace/bun/driver/pgdriver v1.2.9/go.mod h1:YnlfL8hiQ++jSCPySK3k8BotpwbLL9SRDzssvts1Bm4=
github.com/uptrace/bun/extra/bundebug v1.2.6 h1:5l61LXIR2YWk/gqGSq5esha+x/qPPyhtQKORAuTfV34=
github.com/uptrace/bun/extra/bundebug v1.2.6/go.mod h1:11C5ajtPrFcmIRo31TfQrmK5D2LgNIxxTQEZMz6lD2k=
github.com/uptrace/bun/extra/bundebug v1.2.9 h1:3SU66p+q76XhfeUUzl9XooVu7hVNueZ/2Q3J8S1uzCU=
github.com/uptrace/bun/extra/bundebug v1.2.9/go.mod h1:/rp83jYAtwZUQIz+L3KwvREXaSd5GQGPJUusqq+Qtis=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/wk8/go-ordered-map/v2 v2.1.9-0.20240816141633-0a40785b4f41 h1:rnB8ZLMeAr3VcqjfRkAm27qb8y6zFKNfuHvy1Gfe7KI=
github.com/wk8/go-ordered-map/v2 v2.1.9-0.20240816141633-0a40785b4f41/go.mod h1:DbzwytT4g/odXquuOCqroKvtxxldI4nb3nuesHF/Exo=
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI=
go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k=
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/arch v0.12.0 h1:UsYJhbzPYGsT0HbEdmYcqtCv8UNGvnaL561NnIUvaKg=
golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/arch v0.13.0 h1:KCkqVVV1kGg0X87TFysjCJ8MxtZEIU4Ja/yXGeoECdA=
golang.org/x/arch v0.13.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c h1:KL/ZBHXgKGVmuZBZ01Lt57yE5ws8ZPSkkihmEyq7FXc=
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM=
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mellium.im/sasl v0.3.2 h1:PT6Xp7ccn9XaXAnJ03FcEjmAn7kK1x7aoXV6F+Vmrl0=
mellium.im/sasl v0.3.2/go.mod h1:NKXDi1zkr+BlMHLQjY3ofYuU4KSPFxknb8mfEu6SveY=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

14
handlers/handlers.go Normal file
View File

@ -0,0 +1,14 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/uptrace/bun"
)
var DB *bun.DB
func HomePage(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"message": "Welcome to the Receipt Tracker API"})
}

27
handlers/items.go Normal file
View File

@ -0,0 +1,27 @@
package handlers
// form:
import (
"net/http"
"git.savin.nyc/alex/go-receipt-tracker-api/models"
"github.com/gin-gonic/gin"
)
func GetItems(ctx *gin.Context) {
receiptID := ctx.Param("id")
// Create a slice to store the retrieved tasks
var items models.Items
// Execute the database query to retrieve tasks using Go bun
err := DB.NewSelect().Model(&items).Where("receipt_id = ?", receiptID).Scan(ctx.Request.Context())
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Return the retrieved tasks in the response
ctx.JSON(http.StatusOK, gin.H{"items": items})
}

162
handlers/receipts.go Normal file
View File

@ -0,0 +1,162 @@
package handlers
// form:
import (
"net/http"
"git.savin.nyc/alex/go-receipt-tracker-api/models"
"github.com/gin-gonic/gin"
)
func GetReceipts(ctx *gin.Context) {
// Create a slice to store the retrieved tasks
var receipts models.Receipts
// Execute the database query to retrieve tasks using Go bun
err := DB.NewSelect().Model(&receipts).Scan(ctx.Request.Context())
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
for _, receipt := range receipts {
err = DB.NewSelect().Model(&receipt.Items).Where("receipt_id = ?", receipt.ReceiptID).Scan(ctx)
if err != nil {
panic(err)
}
receipt.CreditCard = new(models.CreditCard)
err = DB.NewSelect().Model(receipt.CreditCard).Where("credit_card_id = ?", receipt.CreditCardID).Scan(ctx)
if err != nil {
panic(err)
}
receipt.Merchant = new(models.Merchant)
err = DB.NewSelect().Model(receipt.Merchant).Where("merchant_id = ?", receipt.MerchantID).Scan(ctx)
if err != nil {
panic(err)
}
}
// Return the retrieved tasks in the response
// ctx.JSON(http.StatusOK, gin.H{"receipts": receipts})
ctx.JSON(http.StatusOK, receipts)
}
func GetReceipt(ctx *gin.Context) {
receiptID := ctx.Param("id")
// Check if the task ID is empty
if receiptID == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "ID must be present"})
return
}
receipt := &models.Receipt{}
// Fetch specific record from the database using Go bun
err := DB.NewSelect().Model(receipt).Where("receipt_id = ?", receipt.ReceiptID).Scan(ctx.Request.Context())
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
err = DB.NewSelect().Model(&receipt.Items).Where("receipt_id = ?", receipt.ReceiptID).Scan(ctx)
if err != nil {
panic(err)
}
receipt.CreditCard = new(models.CreditCard)
err = DB.NewSelect().Model(receipt.CreditCard).Where("credit_card_id = ?", receipt.CreditCardID).Scan(ctx)
if err != nil {
panic(err)
}
receipt.Merchant = new(models.Merchant)
err = DB.NewSelect().Model(receipt.Merchant).Where("merchant_id = ?", receipt.MerchantID).Scan(ctx)
if err != nil {
panic(err)
}
if receipt.ReceiptID < 0 {
ctx.JSON(http.StatusNotFound, gin.H{"message": "Receipt not found"})
return
}
ctx.JSON(http.StatusOK, receipt)
}
func UpdateReceipt(ctx *gin.Context) {
receiptID := ctx.Param("id")
if receiptID == "" {
ctx.JSON(http.StatusNoContent, gin.H{"error": "ID must be present"})
return
}
updatedReceipt := &models.Receipt{}
// Bind JSON body to the updatedTask struct
if err := ctx.ShouldBindJSON(updatedReceipt); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Update the task record in the database using Go bun
_, err := DB.NewUpdate().Model(updatedReceipt).
Set("title = ?", updatedReceipt.Type).
Where("receipt_id = ?", receiptID).
Exec(ctx.Request.Context())
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"message": "Receipt updated"})
}
func RemoveReceipt(ctx *gin.Context) {
receiptID := ctx.Param("id")
receipt := &models.Receipt{}
// Delete specific task record from the database
res, err := DB.NewDelete().Model(receipt).Where("receipt_id = ?", receiptID).Exec(ctx.Request.Context())
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
rowsAffected, err := res.RowsAffected()
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Check if any rows were affected by the delete operation
if rowsAffected > 0 {
ctx.JSON(http.StatusOK, gin.H{"message": "Receipt removed"})
} else {
ctx.JSON(http.StatusNotFound, gin.H{"message": "Receipt not found"})
}
}
func AddReceipt(ctx *gin.Context) {
newReceipt := &models.Receipt{}
if err := ctx.ShouldBindJSON(&newReceipt); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Insert the new task record into the database
_, err := DB.NewInsert().Model(newReceipt).Exec(ctx.Request.Context())
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusCreated, gin.H{"message": "Receipt created"})
}

129
main.go Normal file
View File

@ -0,0 +1,129 @@
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/uptrace/bun/extra/bundebug"
"git.savin.nyc/alex/go-receipt-tracker-api/config"
"git.savin.nyc/alex/go-receipt-tracker-api/database"
"git.savin.nyc/alex/go-receipt-tracker-api/handlers"
)
var cfg = &config.Config{}
const (
LoggingOutputJson = "JSON"
LoggingOutputText = "TEXT"
)
func configureLogging(config *config.Logging) *slog.Logger { //nolint:unparam
if config == nil {
return nil
}
var level slog.Level
if err := level.UnmarshalText([]byte(config.Level)); err != nil {
slog.Warn(err.Error())
slog.Warn(fmt.Sprintf("logging level not recognized, defaulting to level %s", slog.LevelInfo.String()))
level = slog.LevelInfo
}
var handler slog.Handler
switch config.Output {
case LoggingOutputJson:
handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: cfg.Logging.Source, Level: level})
case LoggingOutputText:
handler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{AddSource: cfg.Logging.Source, Level: level})
default:
handler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{AddSource: cfg.Logging.Source, Level: level})
}
return slog.New(handler)
}
func main() {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
done <- true
}()
var err error
cfg, err = config.New()
if err != nil { // Handle errors reading the config file
slog.Error("Fatal error config file", "error", err.Error())
os.Exit(1)
}
// LOGGER
logger := configureLogging(cfg.Logging)
logger.Debug("CONFIG", "config", cfg)
// Create the "tasks" table in the database if it doesn't exist
db, err := database.NewDB(cfg.DataBase, logger)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
// Add a query hook for logging
db.AddQueryHook(bundebug.NewQueryHook(
bundebug.WithVerbose(true),
bundebug.FromEnv("BUNDEBUG"),
))
// Ping the database to test the connection
err = db.Ping()
if err != nil {
logger.Debug("Failed to connect to the database")
return
}
// Connection successful
logger.Debug("Connected to the database")
handlers.DB = db
router := gin.Default()
router.Use(cors.Default())
v1 := router.Group("/api/v1")
{
v1.GET("/", handlers.HomePage)
v1.GET("/receipts", handlers.GetReceipts)
v1.GET("/receipts/:id", handlers.GetReceipt)
v1.DELETE("/receipts/:id", handlers.RemoveReceipt)
v1.POST("/receipts", handlers.AddReceipt)
v1.PUT("/receipts/:id", handlers.UpdateReceipt)
v1.GET("/receipts/:id/items", handlers.GetItems)
}
srv := &http.Server{
Addr: ":8080",
Handler: router.Handler(),
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("cannot start ListenAndServe", "error", err)
}
}()
<-done
logger.Warn("caught signal, stopping...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logger.Error("cannot shutdown server gracefully", "error", err)
}
logger.Info("main.go finished")
}

29
models/blacklist.go Normal file
View File

@ -0,0 +1,29 @@
package models
import (
"context"
"github.com/uptrace/bun"
)
type BlackList struct {
bun.BaseModel `bun:"table:blacklist"`
UserID int64 `bun:",pk,autoincrement"`
Name string `bun:"name,type:text,unique" json:"name" yaml:"name"`
TelegramID int64 `bun:"telegram_id,type:int,unique" json:"telegram_id" yaml:"telegram_id"`
TelegramUsername string `bun:"telegram_username,type:text,unique" json:"telegram_username" yaml:"telegram_username"`
}
var _ bun.AfterCreateTableHook = (*BlackList)(nil)
func (*BlackList) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.DB().NewCreateIndex().Model((*BlackList)(nil)).Index("idx_bl_telegram_id").Column("telegram_id").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*BlackList)(nil)).Index("idx_bl_telegram_username").Column("telegram_username").Exec(ctx)
if err != nil {
return err
}
return nil
}

92
models/commands.go Normal file
View File

@ -0,0 +1,92 @@
package models
import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
)
var BtnCmd = map[string]int64{
"user_add": 1,
"user_edit": 2,
"user_detete": 3,
"users_list": 4,
"group_add": 11,
"group_edit": 12,
"group_delete": 13,
"groups_list": 14,
"receipt_add": 21,
"receipt_edit": 22,
"receipt_delete": 23,
"receipts_list": 24,
"item_add": 31,
"item_edit": 32,
"item_delete": 33,
"items_list": 34,
"merchant": 96,
"category": 97,
"tax": 98,
"total": 99,
}
// var btnPage = map[string]int64{
// "prev": 1,
// "next": 2,
// }
// TelegramBotCommand .
type TelegramBotCommand struct {
Cmd int64 // Internal Command ( user_add | user_edit | user_delete )
ID int64 // Internal UserID
RefCmd int64 // Internal Reference Command
RefID int64 // Internal Reference ID
TelegramID int64 // Telegram UserID
Pagination int64 // Pagination
Extra string // Internal String filed to pass Telegram FileID
}
// Unmarshal .
func (t *TelegramBotCommand) Unmarshal(c string) error {
r := regexp.MustCompile(`^(?P<Cmd>.*)\|(?P<ID>.*)\|(?P<RefCmd>.*)\|(?P<RefID>.*)\|(?P<TelegramID>.*)\|(?P<Pagination>.*)\|(?P<Extra>.*)$`)
m := r.FindStringSubmatch(c)
res := make(map[string]string)
for i, name := range r.SubexpNames() {
if i != 0 && name != "" {
res[name] = m[i]
}
}
for k, val := range res {
v := reflect.ValueOf(&t).Elem()
f := reflect.Indirect(v).FieldByName(k)
if !f.IsValid() {
continue
}
if f.Type().Kind() == reflect.Int64 {
age, err := strconv.Atoi(val)
if err != nil {
continue
}
f.SetInt(int64(age))
} else {
f.SetString(val)
}
}
log.Printf("CALLBACK (UNMARSHAL) %d|%d|%d|%d|%d|%d|%s", t.Cmd, t.ID, t.RefCmd, t.RefID, t.TelegramID, t.Pagination, t.Extra)
return nil
}
// Marshal .
func (t *TelegramBotCommand) Marshal() string {
return fmt.Sprintf("%d|%d|%d|%d|%d|%d|%s", t.Cmd, t.ID, t.RefCmd, t.RefID, t.TelegramID, t.Pagination, t.Extra)
}
// String .
func String(t *TelegramBotCommand) string {
return t.Marshal()
}

50
models/credit_cards.go Normal file
View File

@ -0,0 +1,50 @@
package models
import (
"context"
"time"
"github.com/uptrace/bun"
)
type CreditCard struct {
bun.BaseModel `bun:"table:credit_cards"`
CreditCardID int64 `bun:",pk,autoincrement" json:"credit_card_id" yaml:"credit_card_id"`
UserID int64 `json:"user_id" yaml:"user_id"`
Title string `bun:"title,type:text" json:"title,omitempty" yaml:"title,omitempty"`
Bank string `bun:"bank,type:text" json:"bank,omitempty" yaml:"bank,omitempty"`
Type string `bun:"type,type:text" json:"type,omitempty" yaml:"type,omitempty"`
Digits string `bun:"digits,type:text,unique" json:"digits,omitempty" yaml:"digits,omitempty"`
Created time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"created" yaml:"created"`
Updated time.Time `json:"updated" yaml:"updated"`
}
type CreditCards []*CreditCard
var _ bun.AfterCreateTableHook = (*CreditCard)(nil)
func (*CreditCard) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.DB().NewCreateIndex().Model((*CreditCard)(nil)).Index("idx_credit_card_title").Column("title").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*CreditCard)(nil)).Index("idx_credit_card_type").Column("type").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*CreditCard)(nil)).Index("idx_credit_card_digits").Column("digits").Exec(ctx)
if err != nil {
return err
}
return nil
}
// Add .
func (cc *CreditCard) Add(db *bun.DB, ctx context.Context) error {
_, err := db.NewInsert().Model(cc).On("CONFLICT (digits) DO UPDATE").Set("updated = EXCLUDED.updated").Exec(ctx)
if err != nil {
return err
}
return nil
}

92
models/groups.go Normal file
View File

@ -0,0 +1,92 @@
package models
import (
"context"
"github.com/uptrace/bun"
)
type Group struct {
bun.BaseModel `bun:"table:groups"`
UserID int64
GroupID int64 `bun:",pk,autoincrement"`
Name string `bun:"name,type:text" json:"name" yaml:"name"`
TelegramChatID int64 `bun:"telegram_chat_id,type:bigint" json:"telegram_chat_id" yaml:"telegram_chat_id"`
Users Users `bun:"m2m:group_to_users,join:Group=User"`
}
type GroupToUser struct {
GroupID int64 `bun:",pk"`
Group *Group `bun:"rel:belongs-to,join:group_id=user_id"`
UserID int64 `bun:",pk"`
User *User `bun:"rel:belongs-to,join:user_id=group_id"`
}
func NewGroup(j []byte) (*Group, error) {
g := &Group{}
return g, nil
}
// Add .
func (g *Group) Add(db *bun.DB, ctx context.Context) error {
_, err := db.NewInsert().Model(g).Exec(ctx)
if err != nil {
return err
}
return nil
}
// Update .
func (g *Group) Update(db *bun.DB, ctx context.Context) error {
_, err := db.NewUpdate().Model(g).WherePK().Exec(ctx)
if err != nil {
return err
}
return nil
}
// Remove .
func (g *Group) Remove(db *bun.DB, ctx context.Context) error {
_, err := db.NewDelete().Model(g).WherePK().Exec(ctx)
if err != nil {
return err
}
return nil
}
// UserAdd .
func (g *Group) UserAdd(db *bun.DB, ctx context.Context, u int64) error {
_, err := db.NewInsert().Model(g).Exec(ctx)
if err != nil {
return err
}
return nil
}
// UserList .
func (g *Group) UserList(db *bun.DB, ctx context.Context) (Users, error) {
var users Users
_, err := db.NewSelect().Model(&users).ScanAndCount(ctx)
if err != nil {
return nil, err
}
return users, nil
}
// UserRemove .
func (g *Group) UserRemove(db *bun.DB, ctx context.Context, u int) error {
_, err := db.NewInsert().Model(g).Exec(ctx)
if err != nil {
return err
}
return nil
}
func GroupExists(db *bun.DB, ctx context.Context, tid int64) bool {
exists, err := db.NewSelect().Model((*Group)(nil)).Where("telegram_chat_id = ?", tid).Exists(ctx)
if err != nil {
panic(err)
}
return exists
}

79
models/items.go Normal file
View File

@ -0,0 +1,79 @@
package models
import (
"context"
"github.com/uptrace/bun"
)
type Item struct {
bun.BaseModel `bun:"table:items"`
ItemID int64 `bun:",pk,autoincrement" json:"item_id" yaml:"item_id"`
ReceiptID int64 `json:"receipt_id" yaml:"receipt_id"`
Title string `bun:"item,type:text" json:"item" yaml:"item"`
Quantity float64 `bun:"quantity" json:"quantity" yaml:"quantity"`
Price float64 `bun:"price" json:"price" yaml:"price"`
Category string `bun:"category" json:"category" yaml:"category"`
}
type Items []*Item
var _ bun.AfterCreateTableHook = (*Item)(nil)
func (*Item) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.DB().NewCreateIndex().Model((*Item)(nil)).Index("idx_items_item").Column("item").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*Item)(nil)).Index("idx_items_category").Column("category").Exec(ctx)
if err != nil {
return err
}
return nil
}
// Add .
func (i *Item) Add(db *bun.DB, ctx context.Context) error {
_, err := db.NewInsert().Model(i).Exec(ctx)
if err != nil {
return err
}
return nil
}
// Update .
func (i *Item) Update(db *bun.DB, ctx context.Context) error {
_, err := db.NewUpdate().Model(i).WherePK().Exec(ctx)
if err != nil {
return err
}
return nil
}
// Add .
func (i *Items) Add(db *bun.DB, ctx context.Context) error {
_, err := db.NewInsert().Model(i).Exec(ctx)
if err != nil {
return err
}
return nil
}
// List .
func (i *Items) List(db *bun.DB, ctx context.Context, r int) (Items, error) {
var items Items
_, err := db.NewSelect().Model(&items).Where("receipt_id = ?", r).ScanAndCount(ctx)
if err != nil {
return nil, err
}
return items, nil
}
// Remove .
func (i *Items) Remove(db *bun.DB, ctx context.Context) error {
_, err := db.NewDelete().Model(i).WherePK().Exec(ctx)
if err != nil {
return err
}
return nil
}

47
models/merchants.go Normal file
View File

@ -0,0 +1,47 @@
package models
import (
"context"
"time"
"github.com/uptrace/bun"
)
type Merchant struct {
bun.BaseModel `bun:"table:merchants"`
MerchantID int64 `bun:",pk,autoincrement" json:"merchant_id" yaml:"merchant_id"`
Title string `bun:"title" json:"title" yaml:"title"`
Address string `bun:"address,type:text,unique" json:"address,omitempty" yaml:"address,omitempty"`
Phone string `bun:"phone,type:text,unique" json:"phone,omitempty" yaml:"phone,omitempty"`
Created time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"created" yaml:"created"`
Updated time.Time `json:"updated" yaml:"updated"`
}
var _ bun.AfterCreateTableHook = (*Merchant)(nil)
func (*Merchant) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.DB().NewCreateIndex().Model((*Merchant)(nil)).Index("idx_merchant_title").Column("title").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*Merchant)(nil)).Index("idx_merchant_address").Column("address").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*Merchant)(nil)).Index("idx_merchant_phone").Column("phone").Exec(ctx)
if err != nil {
return err
}
return nil
}
// Add .
func (m *Merchant) Add(db *bun.DB, ctx context.Context) error {
_, err := db.NewInsert().Model(m).On("CONFLICT (phone) DO UPDATE").Set("updated = EXCLUDED.updated").Exec(ctx)
// _, err := db.NewInsert().Model(m).Exec(ctx)
if err != nil {
return err
}
return nil
}

119
models/receipts.go Normal file
View File

@ -0,0 +1,119 @@
package models
import (
"context"
"encoding/json"
"strings"
"time"
"github.com/uptrace/bun"
)
type Receipt struct {
bun.BaseModel `bun:"table:receipts"`
ReceiptID int64 `bun:",pk,autoincrement" json:"receipt_id" yaml:"receipt_id"`
UserID int64 `json:"user_id" yaml:"user_id"`
GroupID int64 `json:"group_id" yaml:"group_id"`
MerchantID int64 `json:"merchant_id" yaml:"merchant_id"`
CreditCardID int64 `json:"credit_card_id" yaml:"credit_card_id"`
CreditCard *CreditCard `bun:"credit_card,rel:belongs-to,join:credit_card_id=credit_card_id" json:"credit_card,omitempty" yaml:"credit_card,omitempty"`
Merchant *Merchant `bun:"merchant,rel:belongs-to,join:merchant_id=merchant_id" json:"merchant" yaml:"merchant"`
URL string `bun:"url" json:"url" yaml:"url"`
Type string `bun:"type" json:"type" yaml:"type"`
Category string `bun:"category" json:"category" yaml:"category"`
Items Items `bun:"rel:has-many,join:receipt_id=item_id" json:"items" yaml:"items"`
Tax float64 `bun:"tax" json:"tax,omitempty" yaml:"tax,omitempty"`
CCFee float64 `bun:"cc_fee" json:"cc_fee,omitempty" yaml:"cc_fee,omitempty"`
Tips float64 `bun:"tips" json:"tips,omitempty" yaml:"tips,omitempty"`
Total float64 `bun:"total" json:"total" yaml:"total"`
PaidWith string `bun:"paid_with" json:"paid_with" yaml:"paid_with"`
PaidAt string `bun:"paid_at" json:"paid_at" yaml:"paid_at"`
Created time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"created" yaml:"created"`
Updated time.Time `json:"updated" yaml:"updated"`
}
type Receipts []*Receipt
var _ bun.AfterCreateTableHook = (*Receipt)(nil)
// AfterCreateTable .
func (*Receipt) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.DB().NewCreateIndex().Model((*Receipt)(nil)).Index("idx_receipts_category").Column("category").Exec(ctx)
if err != nil {
return err
}
return nil
}
// NewReceipt .
func NewReceipt(j []byte) (*Receipt, error) {
var r *Receipt
if err := json.Unmarshal([]byte(j), &r); err != nil {
panic(err)
}
return r, nil
}
// Add .
func (r *Receipt) Add(db *bun.DB, ctx context.Context) error {
err := r.Merchant.Add(db, ctx)
if err != nil {
return err
}
err = r.CreditCard.Add(db, ctx)
if err != nil {
return err
}
_, err = db.NewInsert().Model(r).Exec(ctx)
if err != nil {
return err
}
if len(r.Items) > 0 {
for _, item := range r.Items {
item.ReceiptID = r.ReceiptID
item.Title = strings.Title(strings.ToLower(item.Title))
}
err = r.Items.Add(db, ctx)
if err != nil {
return err
}
}
return nil
}
// Update .
func (r *Receipt) Update(db *bun.DB, ctx context.Context) error {
_, err := db.NewUpdate().Model(r).WherePK().Exec(ctx)
if err != nil {
return err
}
return nil
}
// Remove .
func (r *Receipt) Remove(db *bun.DB, ctx context.Context) error {
err := r.Items.Remove(db, ctx)
if err != nil {
return err
}
_, err = db.NewDelete().Where("receipt_id = ?", r.ReceiptID).Exec(ctx)
if err != nil {
return err
}
return nil
}
// Select .
func (r *Receipt) Select(db *bun.DB, ctx context.Context, id int64) error {
err := db.NewSelect().Model(r).Where("receipt_id = ?", id).Scan(ctx)
if err != nil {
return err
}
return nil
}

5
models/settings.go Normal file
View File

@ -0,0 +1,5 @@
package models
type Settings struct {
OpenRegistration bool `bun:"open_registration,type:boolean" json:"open_registration" yaml:"open_registration"`
}

83
models/users.go Normal file
View File

@ -0,0 +1,83 @@
package models
import (
"context"
"time"
"github.com/uptrace/bun"
)
type User struct {
bun.BaseModel `bun:"table:users"`
UserID int64 `bun:",pk,autoincrement" json:"user_id" yaml:"user_id"`
GroupID int64 `json:"-" yaml:"-"`
Name string `bun:"name,type:text,unique" json:"name" yaml:"name"`
Email string `bun:"email,type:text" json:"email,omitempty" yaml:"email,omitempty"`
TelegramID int64 `bun:"telegram_id,type:int,unique" json:"telegram_id" yaml:"telegram_id"`
TelegramUsername string `bun:"telegram_username,type:text,unique" json:"telegram_username" yaml:"telegram_username"`
Receipts Receipts `bun:"rel:has-many,join:user_id=receipt_id"`
CreditCards CreditCards `bun:"rel:has-many,join:user_id=credit_card_id"`
Created time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"created" yaml:"created"`
Updated time.Time `json:"updated" yaml:"updated"`
}
type Users []*User
var _ bun.AfterCreateTableHook = (*User)(nil)
func (*User) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.DB().NewCreateIndex().Model((*User)(nil)).Index("idx_users_name").Column("name").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*User)(nil)).Index("idx_users_telegram_id").Column("telegram_id").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*User)(nil)).Index("idx_users_telegram_username").Column("telegram_username").Exec(ctx)
if err != nil {
return err
}
return nil
}
func NewUser(j []byte) (*User, error) {
u := &User{}
// u := &User{Name: fmt.Sprintf("%s %s", c.Sender().FirstName, c.Sender().LastName), TelegramID: c.Sender().ID, TelegramUsername: c.Sender().Username}
return u, nil
}
func (u *User) Add(db *bun.DB, ctx context.Context) error {
_, err := db.NewInsert().Model(u).Exec(ctx)
if err != nil {
return err
}
return nil
}
func (u *User) Update(db *bun.DB, ctx context.Context) error {
_, err := db.NewUpdate().Model(u).WherePK().Exec(ctx)
if err != nil {
return err
}
return nil
}
func (u *User) Remove(db *bun.DB, ctx context.Context) error {
_, err := db.NewDelete().Model(u).WherePK().Exec(ctx)
if err != nil {
return err
}
return nil
}
func UserExists(db *bun.DB, ctx context.Context, tid int64) bool {
exists, err := db.NewSelect().Model((*User)(nil)).Where("telegram_id = ?", tid).Exists(ctx)
if err != nil {
panic(err)
}
return exists
}

29
models/whitelist.go Normal file
View File

@ -0,0 +1,29 @@
package models
import (
"context"
"github.com/uptrace/bun"
)
type WhiteList struct {
bun.BaseModel `bun:"table:whitelist"`
UserID int64 `bun:",pk,autoincrement"`
Name string `bun:"name,type:text,unique" json:"name" yaml:"name"`
TelegramID int64 `bun:"telegram_id,type:int,unique" json:"telegram_id" yaml:"telegram_id"`
TelegramUsername string `bun:"telegram_username,type:text,unique" json:"telegram_username" yaml:"telegram_username"`
}
var _ bun.AfterCreateTableHook = (*WhiteList)(nil)
func (*WhiteList) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.DB().NewCreateIndex().Model((*WhiteList)(nil)).Index("idx_wl_telegram_id").Column("telegram_id").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*WhiteList)(nil)).Index("idx_wl_telegram_username").Column("telegram_username").Exec(ctx)
if err != nil {
return err
}
return nil
}