Compare commits

...

9 commits
main ... tb/wtf

Author SHA1 Message Date
katefort a216cc14dd redesign structure, add get endpoint 2025-04-21 18:28:55 -05:00
Leyla Becker 3dacf06005 made db module 2025-04-21 17:34:39 -05:00
katefort 2dd8cc3d2b messy 2025-04-21 16:56:58 -05:00
katefort 51a34008f1 verify successful database interactions with test endpoint 2025-04-17 17:29:47 -05:00
katefort c1930d6821 create working test endpoint 2025-04-17 15:54:33 -05:00
katefort 560a3dbc4b fix merge issues 2025-04-08 19:14:08 -05:00
katefort 0254ec2525 add swagger.yml and test route 2025-04-08 19:11:23 -05:00
katefort 735de8d657 add logger, rearrange database initialization 2025-04-08 17:10:49 -05:00
katefort d26e7b8345 Rearrange folders, and separate functionality 2025-03-31 18:15:51 -05:00
18 changed files with 609 additions and 154 deletions

4
.gitignore vendored
View file

@ -2,8 +2,12 @@
# development environment
.envrc
.direnv
cmd/server/server
# postgres
postgres.db
postgres.log
.s.PGSQL.5432*
cmd/server/logfile

View file

@ -3,6 +3,12 @@
The goal of this project is to be a crowd sourced resource to find out if a product is vegan
# Project organization
- handlers: gets query parameters to call service and responds with service output
- services: takes parameters and performs business logic, calls functions to query database
- user_claims.go in database package: queries database
## Task list
- figure out how we want to handle database migrations
- database object models should be separated out from database migration function
@ -13,4 +19,31 @@ The goal of this project is to be a crowd sourced resource to find out if a prod
- barnivore lookup
- alergen ingredient database lookup?
- create mobile and desktop front ends
- moderation tooling?
- moderation tooling?
## How to start database
- Run `brew services start postgresql`
- Create database
- Setting environment variables:
- PGHOST=localhost
- DB_NAME=veganDB (Can be arbitrary)
- Run `createdb veganDB`
(To delete in future just run dropdb <name>)
## Troubleshooting
Error:
`2025/04/17 16:21:21 ERROR: relation "idx_user_claims_product_id" already exists (SQLSTATE 42P07)`
Explanation: The database tables already exist and the program was trying to create them again.
Fix: Either don't create the databases again in the code, or run `dropdb veganDB` to allow it to recreate it.
Error:
`psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: database "<user>" does not exist`
Explanation: Postgres automatically tryes to connect to a database with the same name as the user. Specify user and database:
`psql -U username databaseName `

7
cmd/server/main.go Normal file
View file

@ -0,0 +1,7 @@
package main
import "vegan-barcode/internal/api"
func main() {
api.Start()
}

32
go.mod
View file

@ -1,9 +1,24 @@
module vegan-barcode/backend
module vegan-barcode
go 1.24.1
require (
github.com/gin-gonic/gin v1.10.0
github.com/sirupsen/logrus v1.9.3
)
require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.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.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
@ -13,8 +28,21 @@ require (
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.1 // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // 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.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.31.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

65
go.sum
View file

@ -8,10 +8,18 @@ github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2y
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E=
github.com/cilium/ebpf v0.7.0/go.mod h1:/oI2+1shJiTGAMgl6/RgJr36Eo1jzrRcAWbcXO2usCA=
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/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
@ -37,10 +45,26 @@ github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5Xh
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
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-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
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.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
@ -52,7 +76,9 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
@ -109,8 +135,14 @@ github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0f
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.3.0 h1:eHK/5clGOatcjX3oWGBO/MpxpbHzSwud5EWTSCI+MX0=
github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
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/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@ -122,6 +154,8 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
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/lib/pq v0.0.0-20180327071824-d34b9ff171c2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@ -133,11 +167,18 @@ github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
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.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU=
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 h1:rzf0wL0CHVc8CEsgyygG0Mn9CNCCPZqOPaz8RiiHYQk=
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635/go.mod h1:FBS0z0QWA44HXygs7VXDUOGoN/1TV3RuWkLO04am3wc=
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/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
@ -149,6 +190,8 @@ github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.m
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
github.com/ory/dockertest/v3 v3.10.0 h1:4K3z2VMe8Woe++invjaTB7VRyQXQy5UY+loujO4aNE4=
github.com/ory/dockertest/v3 v3.10.0/go.mod h1:nr57ZbRWMqfsdGdFNLHz5jjNdDb7VVFnzAeW1n5N1Lg=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@ -170,14 +213,16 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
@ -186,9 +231,14 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
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 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
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/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/vingarcia/ksql v1.12.3 h1:1LVRGW39XPaYltPHNQsvHms+bWHp8e99sxQx+aEXDMQ=
github.com/vingarcia/ksql v1.12.3/go.mod h1:DHp/nhVu1nHpBBXH/FRw6JLgIcvcM3+uo2+PfUNdo0g=
@ -217,6 +267,9 @@ go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9E
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@ -253,6 +306,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -285,6 +340,7 @@ golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -335,10 +391,13 @@ golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
@ -354,3 +413,5 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.3.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

View file

@ -0,0 +1,22 @@
package application
import (
"vegan-barcode/internal/database"
"vegan-barcode/internal/utils"
"github.com/sirupsen/logrus"
)
type Application struct {
db database.Database
log *logrus.Logger
}
func Start() {
application := Application{
db: database.InitializeDatabase(),
log: utils.InitializeLogger(),
}
application.bindRoutes()
}

View file

@ -0,0 +1,19 @@
package application
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (a *Application) GetClaimsHandler(c *gin.Context) {
system := c.DefaultQuery("system", "upc")
barcode := c.Query("barcode")
productClaims, err := a.GetClaims(system, barcode)
if err != nil {
c.JSON(http.StatusInternalServerError, nil)
return
}
c.JSON(http.StatusOK, productClaims)
}

View file

@ -0,0 +1,17 @@
package application
import (
"github.com/gin-gonic/gin"
)
func (application *Application) bindRoutes() {
router := gin.Default()
router.Group("/claims")
{
router.GET("/:barcode", application.GetClaimsHandler)
}
router.Run("localhost:8080")
}

View file

@ -0,0 +1,19 @@
package application
import (
"vegan-barcode/internal/models"
)
func (a *Application) GetClaims(system string, barcode string) (*models.ProductClaims, error) {
id, err := a.db.FindProductIDByBarcode(system, barcode)
if err != nil {
return nil, err
}
claims, err := a.db.FindClaimsByProductID(id)
if err != nil {
return nil, err
}
return &models.ProductClaims{Id: id, Claims: claims}, nil
}

View file

@ -0,0 +1,54 @@
package database
import (
"context"
"vegan-barcode/internal/models"
)
func (database *Database) FindClaimsByProductID(product_id int) (claims []models.Claim, err error) {
ctx := context.Background()
err = database.db.Query(ctx, claims, `SELECT
cluster,
id,
worker_type,
evidence_type,
evidence,
category,
polarity,
created_at,
created_by
FROM (
SELECT
cluster,
id,
worker_type,
evidence_type,
evidence,
category,
polarity,
created_at,
created_by,
ROW_NUMBER() OVER (PARTITION BY category ORDER BY created_at DESC) AS rn
FROM (
(
SELECT "user" as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, created_by FROM user_claims
UNION ALL
SELECT "automated" as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, null as created_by FROM automated_claims
)
UNION ALL
(
SELECT "user" as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, created_by FROM user_claims
UNION ALL
SELECT "automated" as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, null as created_by FROM automated_claims
)
)
WHERE product_id = ?
)
WHERE rn = 1
ORDER BY created_at;
`, product_id)
if err != nil {
return claims, err
}
return
}

View file

@ -0,0 +1,97 @@
package database
import (
"context"
"fmt"
"log"
"os"
"github.com/vingarcia/ksql"
"github.com/vingarcia/ksql/adapters/kpgx"
)
var ProductsTable = ksql.NewTable("products", "id")
var UserClaimsTable = ksql.NewTable("user_claims", "id")
var AutomatedClaimsTable = ksql.NewTable("automated_claims", "id")
// Struct used in dependency injection can be replaced with a mock for testing.
type Database struct {
db *ksql.DB
}
// initializeDatabase creates the database and calls createTables.
func InitializeDatabase() Database {
ctx := context.Background()
// urlExample := "postgres://username:password@localhost:5432/database_name"
host := os.Getenv("PGHOST")
dbName := os.Getenv("DB_NAME")
connectString := fmt.Sprintf("postgres:///?host=%s&database=%s", host, dbName)
db, err := kpgx.New(ctx, connectString, ksql.Config{})
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
database := Database{
db: &db,
}
database.createTables(ctx)
return database
}
// createTables adds the product, automated_claims, and user_claims tables to the initialized database.
func (database *Database) createTables(ctx context.Context) {
_, err := database.db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS products (
id BIGSERIAL PRIMARY KEY,
system TEXT,
barcode TEXT,
created_at TIMESTAMPTZ
);
`)
if err != nil {
log.Fatal(err)
}
_, err = database.db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS user_claims (
id BIGSERIAL PRIMARY KEY,
product_id INTEGER,
evidence_type INTEGER,
evidence JSONB,
claim INTEGER,
counter_claim INTEGER,
created_at TIMESTAMPTZ,
created_by TEXT,
CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES products(id)
);
-- CREATE INDEX idx_user_claims_product_id ON user_claims USING HASH (product_id);
`)
if err != nil {
log.Fatal(err)
}
_, err = database.db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS automated_claims (
id BIGSERIAL PRIMARY KEY,
product_id INTEGER,
worker_type INTEGER,
evidence JSONB,
claim INTEGER,
counter_claim INTEGER,
created_at TIMESTAMPTZ,
CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES products(id)
);
-- CREATE INDEX idx_automated_claims_product_id ON automated_claims USING HASH (product_id);
`)
if err != nil {
log.Fatal(err)
}
}

View file

@ -0,0 +1,34 @@
package database
import (
"time"
"vegan-barcode/internal/models"
)
type Product struct {
Id int `ksql:"id"`
System string `ksql:"system"`
Barcode string `ksql:"barcode"`
Created_at time.Time `ksql:"created_at,timeNowUTC"`
}
type AutomatedClaim struct {
id int `ksql:"id"`
product_id int `ksql:"product_id"`
worker_type models.WorkerType `ksql:"worker_type"`
evidence struct{} `ksql:"evidence,json"`
claim models.ClaimType `ksql:"claim"`
counter_claim models.ClaimType `ksql:"counter_claim"`
created_at time.Time `ksql:"created_at,timeNowUTC"`
}
type UserClaim struct {
id int `ksql:"id"`
product_id int `ksql:"product_id"`
evidence_type models.EvidenceType `ksql:"evidence_type"`
evidence struct{} `ksql:"evidence,json"`
claim models.ClaimType `ksql:"claim"`
counter_claim models.ClaimType `ksql:"counter_claim"`
created_at time.Time `ksql:"created_at,timeNowUTC"`
created_by string `ksql:"created_by"`
}

View file

@ -0,0 +1,14 @@
package database
import "context"
func (d *Database) FindProductIDByBarcode(system string, barcode string) (id int, err error) {
ctx := context.Background()
err = d.db.QueryOne(ctx, &id, "SELECT id FROM products WHERE system = ? AND barcode = ?", system, barcode)
if err != nil {
return -1, err
}
return
}

61
internal/models/models.go Normal file
View file

@ -0,0 +1,61 @@
package models
import (
"time"
)
type ClaimType int
const (
ContainsMeat ClaimType = iota
ContainsFish
ContainsEggs
ContainsMilk
ContainsHoney
ContainsWax
ContainsFur
ContainsLeather
ContainsAnimalFibers
ContainsWool
ContainsFeathers
AnimalTesting
MonkeySlavery
)
type WorkerType int
const (
Barnivore WorkerType = iota
)
type EvidenceType int
const (
ManufactureWebsite EvidenceType = iota
IngredientsList
)
type ClusterType int
const (
User ClusterType = iota
Automated
)
// Generic claim type for combining both automated and user claims.
type Claim struct {
Id int
Worker_type WorkerType
Evidence_type EvidenceType
Evidence struct{}
Category ClaimType
Polarity bool
Created_at time.Time
Created_by string
Cluster ClusterType
}
type ProductClaims struct {
Id int
Claims []Claim
}

115
internal/swagger.yml Normal file
View file

@ -0,0 +1,115 @@
openapi: 3.0.0
info:
title: Automated Claims API
version: 1.0.0
paths:
/claims/{barcode}:
get:
summary: Get claims for a barcode and system
parameters:
- name: barcode
in: path
description: Barcode value
required: true
schema:
type: string
- name: system
in: query
description: Name of barcode standard
required: false
schema:
type: string
default: UPC
responses:
"200":
description: Claims found
content:
application/json:
type: array
items:
$ref: "#/components/schemas/Claim"
"204":
description: No claims found for product
"404":
description: Product not found
delete:
summary: Delete claims by user ID
parameters:
- name: id
in: path
description: ID of the claim to delete
required: true
schema:
type: long
responses:
"200":
description: Successful deletion
content:
application/json:
schema:
$ref: "#/components/schemas/Claim"
"404":
description: Claim not found
post:
summary: Make a new user claim
parameters:
- name: barcode
in: path
description: ID of the automated claim
required: true
schema:
type: integer
- name: claim
in: body
description: Claim information
required: true
schema:
$ref: "#/components/schemas/AutomatedClaim"
responses:
"200":
description: Successfully added
content:
schema:
$ref: "#/components/schemas/AutomatedClaim"
"400":
description: Missing information
components:
schemas:
Claim:
type: object
properties:
id:
type: long
product_id:
type: long
evidence_type: # only in user claims
type: string
enum: [image, link, text]
worker: # only in auto claims
type: string
enum: [barnivore] # TODO add other workers
evidence:
type: object
category: # single category claims
type: string
enum:
- ContainsMeat
- ContainsFish
- ContainsEggs
- ContainsMilk
- ContainsHoney
- ContainsWax
- ContainsFur
- ContainsLeather
- ContainsAnimalFibers
- ContainsWool
- ContainsFeathers
- AnimalTesting
- MonkeySlavery
polarity: # true means it does do the thing
type: boolean
created_at:
type: string
format: date-time
created_by:
type: string

13
internal/utils/logger.go Normal file
View file

@ -0,0 +1,13 @@
package utils
import (
"github.com/sirupsen/logrus"
)
func InitializeLogger() *logrus.Logger {
log := logrus.New()
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true, // Include the full timestamp (with date and time)
})
return log
}

View file

@ -1,7 +1,7 @@
SELECT
cluster,
id,
worker,
worker_type,
evidence_type,
evidence,
category,
@ -12,7 +12,7 @@ FROM (
SELECT
cluster,
id,
worker,
worker_type,
evidence_type,
evidence,
category,
@ -22,15 +22,15 @@ FROM (
ROW_NUMBER() OVER (PARTITION BY category ORDER BY created_at DESC) AS rn
FROM (
(
SELECT "user" as cluster, id, product_id, null as worker, evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, created_by FROM user_claims
SELECT "user" as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, created_by FROM user_claims
UNION ALL
SELECT "automated" as cluster, id, product_id, worker, null as evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, null as created_by FROM automated_claims
SELECT "automated" as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, null as created_by FROM automated_claims
)
UNION ALL
(
SELECT "user" as cluster, id, product_id, null as worker, evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, created_by FROM user_claims
SELECT "user" as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, created_by FROM user_claims
UNION ALL
SELECT "automated" as cluster, id, product_id, worker, null as evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, null as created_by FROM automated_claims
SELECT "automated" as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, null as created_by FROM automated_claims
)
)
WHERE product_id = ???

View file

@ -1,143 +0,0 @@
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/vingarcia/ksql"
"github.com/vingarcia/ksql/adapters/kpgx"
)
var ProductsTable = ksql.NewTable("products")
type Product struct {
id int `ksql:"id"`
system string `ksql:"system"`
barcode string `ksql:"barcode"`
created_at time.Time `ksql:"created_at,timeNowUTC"`
}
var UserClaimsTable = ksql.NewTable("user_claims")
type UserClaim struct {
id int `ksql:"id"`
product_id int `ksql:"product_id"`
evidence_type EvidenceType `ksql:"evidence_type"`
evidence struct{} `ksql:"evidence,json"`
claim Claim `ksql:"claim"`
counter_claim Claim `ksql:"counter_claim"`
created_at time.Time `ksql:"created_at,timeNowUTC"`
created_by string `ksql:"created_by"`
}
type EvidenceType int
const (
ManufactureWebsite EvidenceType = iota
IngredientsList
)
var AutomatedClaimsTable = ksql.NewTable("automated_claims")
type AutomatedClaim struct {
id int `ksql:"id"`
product_id int `ksql:"product_id"`
worker_type WorkerType `ksql:"worker_type"`
evidence struct{} `ksql:"evidence,json"`
claim Claim `ksql:"claim"`
counter_claim Claim `ksql:"counter_claim"`
created_at time.Time `ksql:"created_at,timeNowUTC"`
}
type WorkerType int
const (
Barnivore WorkerType = iota
)
type Claim int
const (
ContainsMeat Claim = iota
ContainsFish
ContainsEggs
ContainsMilk
ContainsHoney
ContainsWax
ContainsFur
ContainsLeather
ContainsAnimalFibers
ContainsWool
ContainsFeathers
AnimalTesting
MonkeySlavery
)
func main() {
ctx := context.Background()
// urlExample := "postgres://username:password@localhost:5432/database_name"
host := os.Getenv("PGHOST")
database := os.Getenv("DB_NAME")
connectString :=
fmt.Sprintf("postgres:///?host=%s&database=%s", host, database)
db, err := kpgx.New(ctx, connectString, ksql.Config{})
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer db.Close()
_, err = db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY,
system TEXT,
barcode TEXT,
created_at TIMESTAMPTZ
);
`)
if err != nil {
panic(err.Error())
}
_, err = db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS user_claims (
id INTEGER PRIMARY KEY,
product_id INTEGER,
evidence_type INTEGER,
evidence JSONB,
claim INTEGER,
counter_claim INTEGER,
created_at TIMESTAMPTZ,
created_by TEXT,
CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES products(id)
);
CREATE INDEX idx_user_claims_product_id ON user_claims USING HASH (product_id);
`)
if err != nil {
panic(err.Error())
}
_, err = db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS automated_claims (
id INTEGER PRIMARY KEY,
product_id INTEGER,
worker_type INTEGER,
evidence JSONB,
claim INTEGER,
counter_claim INTEGER,
created_at TIMESTAMPTZ,
CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES products(id)
);
CREATE INDEX idx_automated_claims_product_id ON automated_claims USING HASH (product_id);
`)
if err != nil {
panic(err.Error())
}
}