diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 1407fe0..32117ec 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -143,8 +143,8 @@ FROM WHERE qz.id = $1; --- name: CreateAccount :exec -INSERT INTO account (id, user_id, email, created_at, deleted) VALUES ($1, $2, $3, $4, $5); +-- name: CreateAccount :one +INSERT INTO account (id, user_id, email, created_at, deleted) VALUES ($1, $2, $3, $4, $5) RETURNING *;; -- name: DeletePrivilegeByAccID :exec DELETE FROM privileges WHERE account_id = $1; @@ -299,7 +299,7 @@ WHERE a.session = $1 AND a.start = false AND a.deleted = false ORDER BY a.question_id ASC, a.created_at DESC; --- name: InsertAnswers :exec +-- name: InsertAnswers :one INSERT INTO answer( content, quiz_id, @@ -315,7 +315,8 @@ INSERT INTO answer( ip, start, utm -) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14); +) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) +RETURNING *; -- name: GetResultAnswers :many SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip FROM answer WHERE session = ( diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 4f49350..bbe4a8b 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -745,8 +745,8 @@ func (q *Queries) CopyQuizQuestions(ctx context.Context, arg CopyQuizQuestionsPa return err } -const createAccount = `-- name: CreateAccount :exec -INSERT INTO account (id, user_id, email, created_at, deleted) VALUES ($1, $2, $3, $4, $5) +const createAccount = `-- name: CreateAccount :one +INSERT INTO account (id, user_id, email, created_at, deleted) VALUES ($1, $2, $3, $4, $5) RETURNING id, user_id, email, created_at, deleted ` type CreateAccountParams struct { @@ -757,15 +757,23 @@ type CreateAccountParams struct { Deleted sql.NullBool `db:"deleted" json:"deleted"` } -func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) error { - _, err := q.db.ExecContext(ctx, createAccount, +func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error) { + row := q.db.QueryRowContext(ctx, createAccount, arg.ID, arg.UserID, arg.Email, arg.CreatedAt, arg.Deleted, ) - return err + var i Account + err := row.Scan( + &i.ID, + &i.UserID, + &i.Email, + &i.CreatedAt, + &i.Deleted, + ) + return i, err } const createAmoAccount = `-- name: CreateAmoAccount :exec @@ -2871,7 +2879,7 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU return items, nil } -const insertAnswers = `-- name: InsertAnswers :exec +const insertAnswers = `-- name: InsertAnswers :one INSERT INTO answer( content, quiz_id, @@ -2888,6 +2896,7 @@ INSERT INTO answer( start, utm ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) +RETURNING id, content, quiz_id, question_id, fingerprint, session, created_at, result, new, deleted, email, device_type, device, os, browser, ip, start, utm ` type InsertAnswersParams struct { @@ -2907,8 +2916,8 @@ type InsertAnswersParams struct { Utm json.RawMessage `db:"utm" json:"utm"` } -func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) error { - _, err := q.db.ExecContext(ctx, insertAnswers, +func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (Answer, error) { + row := q.db.QueryRowContext(ctx, insertAnswers, arg.Content, arg.QuizID, arg.QuestionID, @@ -2924,7 +2933,28 @@ func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) er arg.Start, arg.Utm, ) - return err + var i Answer + err := row.Scan( + &i.ID, + &i.Content, + &i.QuizID, + &i.QuestionID, + &i.Fingerprint, + &i.Session, + &i.CreatedAt, + &i.Result, + &i.New, + &i.Deleted, + &i.Email, + &i.DeviceType, + &i.Device, + &i.Os, + &i.Browser, + &i.Ip, + &i.Start, + &i.Utm, + ) + return i, err } const insertPrivilege = `-- name: InsertPrivilege :exec diff --git a/go.mod b/go.mod index 1faf46d..b30afc3 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/rs/xid v1.5.0 github.com/stretchr/testify v1.8.4 github.com/tealeg/xlsx v1.0.5 + github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf google.golang.org/protobuf v1.33.0 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d ) @@ -31,15 +32,20 @@ require ( github.com/minio/sha256-simd v1.0.1 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.8.0 // indirect + go.uber.org/zap v1.21.0 // indirect golang.org/x/crypto v0.20.0 // indirect golang.org/x/net v0.21.0 // indirect golang.org/x/sys v0.17.0 // indirect golang.org/x/text v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 7b3e639..e82a594 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= +github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -50,6 +52,9 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w 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/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= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= @@ -58,37 +63,82 @@ github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 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.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= +github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf h1:TJJm6KcBssmbWzplF5lzixXl1RBAi/ViPs1GaSOkhwo= +github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf/go.mod h1:1FsorU3vnXO9xS9SrhUp8fRb/6H/Zfll0rPt1i4GWaA= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= +go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8= +go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= +go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= +go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg= golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/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= golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +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/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.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/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-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/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.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/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= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d h1:gbaDt35HMDqOK84WYmDIlXMI7rstUcRqNttaT6Kx1do= diff --git a/middleware/middleware.go b/middleware/middleware.go index 0c596d7..90dde8a 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -2,6 +2,7 @@ package middleware import ( "github.com/gofiber/fiber/v2" + "github.com/themakers/hlog" "os" "strings" "time" @@ -16,6 +17,7 @@ const ( SessionKey = "X-SessionKey" SessionCookie = "session" AccountId = "id" + HlogCtxKey = "logger" ) func AnswererChain() fiber.Handler { @@ -99,7 +101,19 @@ func JWTAuth() fiber.Handler { } } +func ContextLogger(logger hlog.Logger) fiber.Handler { + return func(c *fiber.Ctx) error { + c.Locals(HlogCtxKey, logger) + return c.Next() + } +} + func GetAccountId(c *fiber.Ctx) (string, bool) { id, ok := c.Context().UserValue(AccountId).(string) return id, ok } + +func GetLogger(c *fiber.Ctx) hlog.Logger { + logger := c.Context().UserValue(HlogCtxKey).(hlog.Logger) + return logger +} diff --git a/repository/account/account.go b/repository/account/account.go index 473251e..1cb97b6 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -94,19 +94,23 @@ func (r *AccountRepository) GetPrivilegesByAccountID(ctx context.Context, userID } // todo test -func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Account) error { +func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Account) (model.Account, error) { data.ID = uuid.NewString() - - err := r.queries.CreateAccount(ctx, sqlcgen.CreateAccountParams{ + row, err := r.queries.CreateAccount(ctx, sqlcgen.CreateAccountParams{ ID: uuid.MustParse(data.ID), UserID: sql.NullString{String: data.UserID, Valid: data.UserID != ""}, Email: sql.NullString{String: data.Email, Valid: data.Email != ""}, CreatedAt: sql.NullTime{Time: data.CreatedAt, Valid: !data.CreatedAt.IsZero()}, Deleted: sql.NullBool{Bool: data.Deleted, Valid: true}, }) - if err != nil { - return fmt.Errorf("failed to create account: %w", err) + return model.Account{}, fmt.Errorf("failed to create account: %w", err) + } + + createdAccount := model.Account{ + ID: row.ID.String(), + UserID: row.UserID.String, + Email: row.Email.String, } for _, privilege := range data.Privileges { @@ -119,11 +123,11 @@ func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Accou }) if err != nil { - return fmt.Errorf("failed to insert privilege: %w", err) + return model.Account{}, fmt.Errorf("failed to insert privilege: %w", err) } } - return nil + return createdAccount, nil } func (r *AccountRepository) DeleteAccount(ctx context.Context, accountID string) error { diff --git a/repository/answer/answer.go b/repository/answer/answer.go index 99cc010..c473ce0 100644 --- a/repository/answer/answer.go +++ b/repository/answer/answer.go @@ -29,10 +29,10 @@ func NewAnswerRepository(deps Deps) *AnswerRepository { } // test + -func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.Answer, session, fp string, quizID uint64) ([]uint64, []error) { +func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.Answer, session, fp string, quizID uint64) ([]model.Answer, []error) { var ( - answered []uint64 - errs []error + createdAnswers []model.Answer + errs []error ) tx, err := r.pool.BeginTx(ctx, nil) @@ -66,11 +66,29 @@ func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.An Utm: utmJSON, } - err = r.queries.InsertAnswers(ctx, params) + row, err := r.queries.InsertAnswers(ctx, params) + createdAnswer := model.Answer{ + Id: uint64(row.ID), + Content: row.Content.String, + QuizId: uint64(row.QuizID), + QuestionId: uint64(row.QuestionID), + Fingerprint: row.Fingerprint.String, + Session: row.Session.String, + Result: row.Result.Bool, + New: row.New.Bool, + Email: row.Email, + DeviceType: row.DeviceType, + Device: row.Device, + OS: row.Os, + Browser: row.Browser, + IP: row.Ip, + Start: row.Start, + } + if err != nil { errs = append(errs, err) } else { - answered = append(answered, ans.QuestionId) + createdAnswers = append(createdAnswers, createdAnswer) } } @@ -80,7 +98,7 @@ func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.An return nil, errs } - return answered, errs + return createdAnswers, errs } // test + diff --git a/repository/question/question.go b/repository/question/question.go index 88e33e0..44f56a5 100644 --- a/repository/question/question.go +++ b/repository/question/question.go @@ -32,7 +32,7 @@ func NewQuestionRepository(deps Deps) *QuestionRepository { } // test + -func (r *QuestionRepository) CreateQuestion(ctx context.Context, record *model.Question) error { +func (r *QuestionRepository) CreateQuestion(ctx context.Context, record *model.Question) (uint64, error) { params := sqlcgen.InsertQuestionParams{ QuizID: int64(record.QuizId), Title: record.Title, @@ -47,14 +47,14 @@ func (r *QuestionRepository) CreateQuestion(ctx context.Context, record *model.Q data, err := r.queries.InsertQuestion(ctx, params) if err != nil { - return err + return 0, err } record.Id = uint64(data.ID) record.CreatedAt = data.CreatedAt.Time record.UpdatedAt = data.UpdatedAt.Time - return nil + return record.Id, nil } // test + diff --git a/repository/quiz/quiz.go b/repository/quiz/quiz.go index 4257cb3..f4ce259 100644 --- a/repository/quiz/quiz.go +++ b/repository/quiz/quiz.go @@ -33,7 +33,7 @@ func NewQuizRepository(deps Deps) *QuizRepository { } // test + -func (r *QuizRepository) CreateQuiz(ctx context.Context, record *model.Quiz) error { +func (r *QuizRepository) CreateQuiz(ctx context.Context, record *model.Quiz) (uint64, error) { if record.Qid == "" { record.Qid = uuid.NewString() } @@ -62,7 +62,7 @@ func (r *QuizRepository) CreateQuiz(ctx context.Context, record *model.Quiz) err data, err := r.queries.InsertQuiz(ctx, params) if err != nil { - return err + return 0, err } record.Id = uint64(data.ID) @@ -70,7 +70,7 @@ func (r *QuizRepository) CreateQuiz(ctx context.Context, record *model.Quiz) err record.UpdatedAt = data.UpdatedAt.Time record.Qid = data.Qid.UUID.String() - return nil + return record.Id, nil } type GetQuizListDeps struct {