diff --git a/.gitignore b/.gitignore index c329722..434c14b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,12 @@ # development environment .envrc .direnv - +cmd/server/server +cmd/server/app.log # postgres postgres.db postgres.log .s.PGSQL.5432* +cmd/server/logfile + + diff --git a/README.md b/README.md index 85b3fb2..86efcab 100644 --- a/README.md +++ b/README.md @@ -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,34 @@ 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? \ No newline at end of file +- moderation tooling? + +## Tip +Paste this to log the SQL queries in stdout. +ctx = ksql.InjectLogger(ctx, ksql.Logger) + + +## 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 ) + + +## 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 "" 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 ` + diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..e932f38 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "vegan-barcode/internal/application" +) + +func main() { + application.Start() +} diff --git a/go.mod b/go.mod index 8ab901a..ee2277c 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 623c258..87c814b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/application/application.go b/internal/application/application.go new file mode 100644 index 0000000..1df7485 --- /dev/null +++ b/internal/application/application.go @@ -0,0 +1,20 @@ +package application + +import ( + "vegan-barcode/internal/database" + "vegan-barcode/internal/utils" +) + +type Application struct { + db database.Database + // TODO: possibly include logger? +} + +func Start() { + application := Application{ + db: database.InitializeDatabase(), + } + utils.InitializeLogger() + application.bindRoutes() + +} diff --git a/internal/application/handlers.go b/internal/application/handlers.go new file mode 100644 index 0000000..aaa1a54 --- /dev/null +++ b/internal/application/handlers.go @@ -0,0 +1,47 @@ +package application + +import ( + "net/http" + "vegan-barcode/internal/models" + + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" +) + +func (a *Application) TestHandler(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{"message": "Hello World!"}) +} + +func (a *Application) GetClaimsHandler(c *gin.Context) { + system := c.Param("system") + barcode := c.Param("barcode") + + productClaims, err := a.GetClaims(system, barcode) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, productClaims) +} + +// PostClaimHandler takes the parameters and body form +func (a *Application) PostClaimHandler(c *gin.Context) { + system := c.Param("system") + barcode := c.Param("barcode") + + var requestBody models.UserClaimForm + err := c.BindJSON(&requestBody) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + log.Debugf("requestbody: %v", requestBody) + + claim, err := a.CreateUserClaim(system, barcode, requestBody) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, claim) +} diff --git a/internal/application/routes.go b/internal/application/routes.go new file mode 100644 index 0000000..8a8f21c --- /dev/null +++ b/internal/application/routes.go @@ -0,0 +1,20 @@ +package application + +import ( + "github.com/gin-gonic/gin" +) + +func (application *Application) bindRoutes() { + + router := gin.Default() + + router.GET("/", application.TestHandler) + + claims := router.Group("/claims/:system/:barcode") + { + claims.GET("", application.GetClaimsHandler) + claims.POST("", application.PostClaimHandler) + } + + router.Run("localhost:8080") +} diff --git a/internal/application/services.go b/internal/application/services.go new file mode 100644 index 0000000..3dae047 --- /dev/null +++ b/internal/application/services.go @@ -0,0 +1,37 @@ +package application + +import ( + "errors" + "vegan-barcode/internal/database" + "vegan-barcode/internal/models" +) + +// GetClaims doesn't automatically create a new product because maybe the user mistyped. +// Only create a new product when they want to add claims. +func (a *Application) GetClaims(system string, barcode string) (*models.ProductClaims, error) { + product, err := a.db.FindProductByBarcode(system, barcode) + if err != nil { + return nil, err + } + if product == nil { + return nil, errors.New("product not found") + } + + claims, err := a.db.FindClaimsByProductID(product.Id) + if err != nil { + return nil, err + } + + return &models.ProductClaims{Id: product.Id, Claims: claims}, nil +} + +// CreateUserClaim gets the product ID or creates a new entry if not found, +// then calls InsertUserClaim to create the claim object and put it in the database. +func (a *Application) CreateUserClaim(system string, barcode string, form models.UserClaimForm) (*database.UserClaim, error) { + product, err := a.db.FindOrCreateProduct(system, barcode) + if err != nil { + return nil, err + } + + return a.db.InsertUserClaim(product.Id, form) +} diff --git a/internal/database/claims.go b/internal/database/claims.go new file mode 100644 index 0000000..73532b8 --- /dev/null +++ b/internal/database/claims.go @@ -0,0 +1,94 @@ +package database + +import ( + "context" + "fmt" + "vegan-barcode/internal/models" + + "github.com/vingarcia/ksql" +) + +func (d *Database) FindClaimsByProductID(product_id int) (claims []models.Claim, err error) { + ctx := context.Background() + + err = d.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(claims) 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(claims) 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(counterclaims) 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(counterclaims) as category, false as polarity, created_at, null as created_by FROM automated_claims + ) + ) AS combined_claims + WHERE product_id = $1 + ) AS ranked_claims + WHERE rn = 1 + ORDER BY created_at; + `, product_id) + + if err != nil { + return nil, fmt.Errorf("query failed: %w", err) + } + return +} + +// Testing function +func (database *Database) FindUserClaimById(claim_id int) (*UserClaim, error) { + ctx := context.Background() + + var claim UserClaim + + err := database.db.QueryOne(ctx, claim, "FROM user_claims WHERE id = $1", claim_id) + + if err == ksql.ErrRecordNotFound { + return nil, err + } + return &claim, err +} + +// InsertUserClaim puts creates a new user claim database object and inserts in DB. +func (d *Database) InsertUserClaim(product_id int, form models.UserClaimForm) (*UserClaim, error) { + ctx := context.Background() + var uc = UserClaim{ + Product_id: product_id, + Evidence_type: form.Evidence_type, + Evidence: form.Evidence, + Claims: form.Claims, + Counterclaims: form.Counterclaims, + Created_by: form.Created_by, + } + + err := d.db.Insert(ctx, UserClaimsTable, &uc) + if err != nil { + return nil, err + } + + return &uc, nil +} diff --git a/internal/database/claims_test.go b/internal/database/claims_test.go new file mode 100644 index 0000000..81769a6 --- /dev/null +++ b/internal/database/claims_test.go @@ -0,0 +1,41 @@ +package database + +import ( + "testing" + "vegan-barcode/internal/models" +) + +func TestCreateClaimByProductId(t *testing.T) { + database := InitializeDatabase() + + product, _ := database.CreateProduct("upc", "1234567890") + + form := models.UserClaimForm{ + Evidence_type: models.IngredientsList, + Evidence: models.IngredientsListEvidence{ + Ingredients: "flour,egg,milk,water,salt,butter", + }, + Claims: []models.ClaimType{ + models.ContainsEgg, + models.ContainsMilk, + }, + Counterclaims: []models.ClaimType{ + models.ContainsMeat, + models.ContainsFish, + models.ContainsHoney, + }, + Created_by: "1", + } + + claim, err := database.CreateUserClaim(product.Id, form) + + if err != nil { + t.Errorf("Got error while creating user claim %v", err) + } + + foundClaim, _ := database.FindUserClaimById(claim.Id) + + if foundClaim == nil { + t.Errorf("Could not find created claim %d", claim.Id) + } +} diff --git a/internal/database/database.go b/internal/database/database.go new file mode 100644 index 0000000..42604f3 --- /dev/null +++ b/internal/database/database.go @@ -0,0 +1,98 @@ +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, + UNIQUE (system, barcode) + ); + `) + 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 TEXT, + evidence JSONB, + claims TEXT[], + counterclaims TEXT[], + 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 TEXT, + evidence JSONB, + claims TEXT[], + counterclaims TEXT[], + 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) + } +} diff --git a/internal/database/models.go b/internal/database/models.go new file mode 100644 index 0000000..d93dc5c --- /dev/null +++ b/internal/database/models.go @@ -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 interface{} `ksql:"evidence,json"` + Claims []models.ClaimType `ksql:"claims"` + Counterclaims []models.ClaimType `ksql:"counterclaims"` + 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 interface{} `ksql:"evidence,json"` + Claims []models.ClaimType `ksql:"claims"` + Counterclaims []models.ClaimType `ksql:"counterclaims"` + Created_at time.Time `ksql:"created_at,timeNowUTC"` + Created_by string `ksql:"created_by"` +} diff --git a/internal/database/products.go b/internal/database/products.go new file mode 100644 index 0000000..77f5ad0 --- /dev/null +++ b/internal/database/products.go @@ -0,0 +1,62 @@ +package database + +import ( + "context" + "fmt" + "time" + + log "github.com/sirupsen/logrus" + "github.com/vingarcia/ksql" +) + +func (d *Database) FindOrCreateProduct(system string, barcode string) (*Product, error) { + product, err := d.FindProductByBarcode(system, barcode) + if err != nil { + return nil, err + } + + // If the product doesn't exist yet, create it + if product == nil { + product, err = d.CreateProduct(system, barcode) + if err != nil { + return nil, err + } + } + return product, nil +} + +// FindProductByBarcode will return nil with no error if the product doesn't exist. +func (d *Database) FindProductByBarcode(system string, barcode string) (*Product, error) { + + ctx := context.Background() + + var product Product + err := d.db.QueryOne(ctx, &product, "FROM products WHERE system = $1 AND barcode = $2", system, barcode) + + // No error because product may just not exist. + if err == ksql.ErrRecordNotFound { + return nil, nil + } + if err != nil { + return nil, err + } + return &product, nil +} + +// CreateProduct simply makes an entry in the products table. Prefer FindOrCreateProduct +func (d *Database) CreateProduct(system string, barcode string) (*Product, error) { + ctx := context.Background() + var product = Product{ + System: system, + Barcode: barcode, + Created_at: time.Now(), + } + + log.Debugf("successfully created new product: %v", product) + + if err := d.db.Insert(ctx, ProductsTable, &product); err != nil { + return nil, fmt.Errorf("failed to insert new product: %w", err) + } + + return &product, nil +} diff --git a/internal/models/models.go b/internal/models/models.go new file mode 100644 index 0000000..a299443 --- /dev/null +++ b/internal/models/models.go @@ -0,0 +1,74 @@ +package models + +import ( + "time" +) + +type ClaimType string + +const ( + ContainsMeat ClaimType = "meat" + ContainsFish ClaimType = "fish" + ContainsEgg ClaimType = "egg" + ContainsMilk ClaimType = "milk" + ContainsHoney ClaimType = "honey" + ContainsWax ClaimType = "wax" + ContainsFur ClaimType = "fur" + ContainsLeather ClaimType = "leather" + ContainsAnimalFibers ClaimType = "animal_fibers" + ContainsWool ClaimType = "wool" + ContainsFeathers ClaimType = "feathers" + AnimalTesting ClaimType = "animal_testing" + MonkeySlavery ClaimType = "monkey_slavery" +) + +type WorkerType string + +const ( + Barnivore WorkerType = "barnivore" +) + +// Using a string here to make database modification easier. +type EvidenceType string + +const ( + ManufacturerWebsite EvidenceType = "manufacturer" + IngredientsList EvidenceType = "ingredients" +) + +type IngredientsListEvidence struct { + Ingredients string +} + +type ClusterType string + +const ( + User ClusterType = "user" + Automated ClusterType = "auto" +) + +// Generic claim type for combining both automated and user claims. +type Claim struct { + Id int `ksql:"id"` + Worker_type *WorkerType `ksql:"worker_type"` + Evidence_type EvidenceType `ksql:"evidence_type"` + Evidence struct{} `ksql:"evidence"` + Category ClaimType `ksql:"category"` + Polarity bool `ksql:"polarity"` + Created_at time.Time `ksql:"created_at"` + Created_by string `ksql:"created_by"` + Cluster ClusterType `ksql:"cluster"` +} + +type ProductClaims struct { + Id int + Claims []Claim +} + +type UserClaimForm struct { + Evidence_type EvidenceType + Evidence interface{} + Claims []ClaimType + Counterclaims []ClaimType + Created_by string +} diff --git a/internal/swagger.yml b/internal/swagger.yml new file mode 100644 index 0000000..05a8408 --- /dev/null +++ b/internal/swagger.yml @@ -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 diff --git a/internal/utils/logger.go b/internal/utils/logger.go new file mode 100644 index 0000000..16e3d09 --- /dev/null +++ b/internal/utils/logger.go @@ -0,0 +1,22 @@ +package utils + +import ( + "io" + "os" + + log "github.com/sirupsen/logrus" +) + +func InitializeLogger() { + file, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + if err != nil { + log.Fatal(err) + } + log.SetOutput(io.MultiWriter(os.Stdout, file)) + + log.SetFormatter(&log.TextFormatter{ + FullTimestamp: true, + }) + + log.SetLevel(log.DebugLevel) +} diff --git a/query_draft.sql b/query_draft.sql index 19a6283..082d649 100644 --- a/query_draft.sql +++ b/query_draft.sql @@ -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 = ??? diff --git a/src/database/database.go b/src/database/database.go deleted file mode 100644 index c9175c7..0000000 --- a/src/database/database.go +++ /dev/null @@ -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()) - } -}