Skip to content

Commit

Permalink
MF-739 - Add ID to the User entity (#1152)
Browse files Browse the repository at this point in the history
* MF-739 - Add ID to the User entity

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Resolve remarks

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Move idp to project root

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use RetrieveByEmail func and UUIDProvider

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rm idp.go

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix comment

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rename UserInfo into ViewUser

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix ViewUser naming

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
  • Loading branch information
manuio authored Jun 4, 2020
1 parent b3f5f0a commit 8db82e1
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 97 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/influxdata/influxdb v1.8.0
github.com/jmoiron/sqlx v1.2.1-0.20190319043955-cdf62fdf55f6
github.com/lib/pq v1.6.0
github.com/mainflux/mproxy v0.2.1-0.20200603122422-b08e1fa2cf5c
github.com/mainflux/mproxy v0.2.1
github.com/mainflux/senml v1.5.0
github.com/nats-io/nats.go v1.10.0
github.com/opentracing/opentracing-go v1.1.0
Expand All @@ -36,8 +36,6 @@ require (
go.mongodb.org/mongo-driver v1.3.3
golang.org/x/crypto v0.0.0-20200602180216-279210d13fed
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
gonum.org/v1/gonum v0.7.0
google.golang.org/genproto v0.0.0-20200603110839-e855014d5736 // indirect
google.golang.org/grpc v1.29.1
)
23 changes: 7 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ github.com/go-logfmt/logfmt v0.5.0 h1:TrB8swr/68K7m9CcGut2g3UOihhbcbiMAYiuTXdEih
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-redis/redis v6.15.0+incompatible h1:/Wib9cA7CF3SQxBZRMHyQvqzlwzc8PJGDMkRfqQebSE=
github.com/go-redis/redis v6.15.0+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redis v6.15.7+incompatible h1:3skhDh95XQMpnqeqNftPkQD9jL9e5e36z/1SUm6dy1U=
github.com/go-redis/redis v6.15.7+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redis v6.15.8+incompatible h1:BKZuG6mCnRj5AOaWJXoCgf6rqTYnYJLe4en2hxT7r9o=
github.com/go-redis/redis v6.15.8+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
Expand Down Expand Up @@ -228,8 +226,6 @@ github.com/gobuffalo/packr/v2 v2.7.1/go.mod h1:qYEvAazPaVxy7Y7KR0W8qYEE+RymX74kE
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/gocql/gocql v0.0.0-20181106112037-68ae1e384be4 h1:n5NlV76GU6337XT+jarynqONI5LlqaYkTPaFZ25og6g=
github.com/gocql/gocql v0.0.0-20181106112037-68ae1e384be4/go.mod h1:4Fw1eo5iaEhDUs8XyuhSVCVy52Jq3L+/3GJgYkwc+/0=
github.com/gocql/gocql v0.0.0-20200511135441-57b003a04490 h1:DE7b84SsUn+1Y3LG4YpFn7KpUra0iuYwS6PkSayorcg=
github.com/gocql/gocql v0.0.0-20200511135441-57b003a04490/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY=
github.com/gocql/gocql v0.0.0-20200526081602-cd04bd7f22a7 h1:TvUE5vjfoa7fFHMlmGOk0CsauNj1w4yJjR9+/GnWVCw=
github.com/gocql/gocql v0.0.0-20200526081602-cd04bd7f22a7/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY=
github.com/godror/godror v0.13.3/go.mod h1:2ouUT4kdhUBk7TAkHWD4SN0CdI0pgEQbo8FVHhbSKWg=
Expand Down Expand Up @@ -298,14 +294,14 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopcua/opcua v0.1.6 h1:B9SVRKQGzcWcwP2QPYN93Uku32+3wL+v5cgzBxE6V5I=
github.com/gopcua/opcua v0.1.6/go.mod h1:INwnDoRxmNWAt7+tzqxuGqQkSF2c1C69VAL0c2q6AcY=
github.com/gopcua/opcua v0.1.11 h1:i7qDkVYRQP5LTiAEnPu09RcPSA1KGPkHsNd/TeDcZc8=
github.com/gopcua/opcua v0.1.11/go.mod h1:O2l+/u0jM6f3WKRbz5L5ep7yNpQX2l5DOagXqXNcDV8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ=
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
Expand Down Expand Up @@ -427,8 +423,6 @@ github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2/go.mod h1:5WUZQaWbwv1U+lTRe
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.5.2 h1:yTSXVswvWUOQ3k1sd7vJfDrbSl8lKuscqFJRqjC0ifw=
github.com/lib/pq v1.5.2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.6.0 h1:I5DPxhYJChW9KYc66se+oKFFQX6VuQrKiprsX6ivRZc=
github.com/lib/pq v1.6.0/go.mod h1:4vXEAYvW1fRQ2/FhZ78H73A60MHw1geSm145z2mdY1g=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
Expand All @@ -441,6 +435,7 @@ github.com/mainflux/mainflux v0.0.0-20191223163044-f42f2095bab4/go.mod h1:K3ghSI
github.com/mainflux/mainflux v0.0.0-20200314190902-c91fe0d45353/go.mod h1:yijZGLNkcDOPJfPhRMwuu5ZFcNHqDHzWurN4q1rOT/Q=
github.com/mainflux/mainflux v0.0.0-20200324100741-6ffa916ed229/go.mod h1:mde8cQhTPjLulu2pn/x8OgQ2S++lDufS+ODE93zuHjY=
github.com/mainflux/mainflux v0.0.0-20200512161904-df6f5adff8e4/go.mod h1:2caJ68GaQPVNe85z5pNJMJk0CflgcS3XWghYsJSBesU=
github.com/mainflux/mainflux v0.11.1-0.20200603183352-7f3e2c1b21ed/go.mod h1:8jwcwH3MKYgoQks9BBHq19Br25ElzW25vteZX7tWZ+w=
github.com/mainflux/mproxy v0.1.3 h1:/JNnxgo/03wSpbwQH2+WE1AzgMWaSKogTVXblh18x5s=
github.com/mainflux/mproxy v0.1.3/go.mod h1:/BdaBfgye1GNCD+eat4ipFamy9IEVRH5nhZS0yEShVg=
github.com/mainflux/mproxy v0.1.5 h1:a0zKiUyuTDld2TwCuhrOFtUvotcvSfwJMuh/JcrqC6I=
Expand All @@ -449,6 +444,8 @@ github.com/mainflux/mproxy v0.1.8 h1:vFpSbSTu327runxYnXoJOg2MrJ2etcMPUF1rPTrZbTk
github.com/mainflux/mproxy v0.1.8/go.mod h1:NnhrUDytvV4pCI5LDuet86/WrymrUaX0/x1tlUHTKhU=
github.com/mainflux/mproxy v0.2.1-0.20200603122422-b08e1fa2cf5c h1:vLMYmnA4W2GzUwpSAnRAB0eBJ9USQpNUo0+WTaVB9uI=
github.com/mainflux/mproxy v0.2.1-0.20200603122422-b08e1fa2cf5c/go.mod h1:lFD56bDgNTslCLoTlZfo2DyQbkQOnoxEXmbE4VumRm4=
github.com/mainflux/mproxy v0.2.1 h1:Qu+r9GwXO3zhWEXPHYbW/TopDbMDQlfJx2VrmVEElJc=
github.com/mainflux/mproxy v0.2.1/go.mod h1:+T8h6ZupYPl6Lx9A0hqpcUQtcLyOBdzm/lfkjvPfGXo=
github.com/mainflux/senml v1.0.0 h1:oLS5aBhvdHjgQ8kfq3jX7yD+DaquhvpyvIWNsPil3X0=
github.com/mainflux/senml v1.0.0/go.mod h1:g9i8pj4WMs29KkUpXivbe/PP0qJd1kt3b1CF77S8A3s=
github.com/mainflux/senml v1.0.1 h1:qWKIGeUe7YEygM3xZcJ9Lbq+DHuT8V23dz1hgAYkYEY=
Expand Down Expand Up @@ -772,8 +769,6 @@ golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200602180216-279210d13fed h1:g4KENRiCMEx58Q7/ecwfT0N2o8z35Fnbsjig/Alf2T4=
golang.org/x/crypto v0.0.0-20200602180216-279210d13fed/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -832,8 +827,6 @@ golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120 h1:EZ3cVSzKOlJxAd8e8YAJ7no8nNypTxexh/YE/xW3ZEY=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -889,8 +882,6 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepx
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200513112337-417ce2331b5c h1:kISX68E8gSkNYAFRFiDU8rl5RIn1sJYKYb/r2vMLDrU=
golang.org/x/sys v0.0.0-20200513112337-417ce2331b5c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down Expand Up @@ -994,11 +985,11 @@ google.golang.org/genproto v0.0.0-20191230161307-f3c370f40bfb/go.mod h1:n3cpQtvx
google.golang.org/genproto v0.0.0-20200108215221-bd8f9a0ef82f/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc=
google.golang.org/genproto v0.0.0-20200225123651-fc8f55426688 h1:1+0Z5cgv1eDXJD9z2tdQF9PSSQnJXwism490hJydMRI=
google.golang.org/genproto v0.0.0-20200225123651-fc8f55426688/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884 h1:fiNLklpBwWK1mth30Hlwk+fcdBmIALlgF5iy77O37Ig=
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20200603110839-e855014d5736 h1:+IE3xTD+6Eb7QWG5JFp+dQr/XjKpjmrNkh4pdjTdHEs=
google.golang.org/genproto v0.0.0-20200603110839-e855014d5736/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/genproto v0.0.0-20200604104852-0b0486081ffb h1:ek2py5bOqzR7MR/6obzk0rXUgYCLmjyLnaO9ssT+l6w=
google.golang.org/genproto v0.0.0-20200604104852-0b0486081ffb/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.0/go.mod h1:chYK+tFQF0nDUGJgXMSgLCQk3phJEuONr2DCgLDdAQM=
Expand Down
1 change: 1 addition & 0 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var _ SDK = (*mfSDK)(nil)

// User represents mainflux user its credentials.
type User struct {
ID string `json:"id,omitempty"`
Email string `json:"email,omitempty"`
Password string `json:"password,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Expand Down
6 changes: 2 additions & 4 deletions things/postgres/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ func migrateDB(db *sqlx.DB) error {
Id: "things_2",
Up: []string{
`ALTER TABLE IF EXISTS things ALTER COLUMN
metadata TYPE JSONB using metadata::text::jsonb
`,
metadata TYPE JSONB using metadata::text::jsonb`,
},
},
{
Id: "things_3",
Up: []string{
`ALTER TABLE IF EXISTS channels ALTER COLUMN
metadata TYPE JSONB using metadata::text::jsonb
`,
metadata TYPE JSONB using metadata::text::jsonb`,
},
},
},
Expand Down
4 changes: 3 additions & 1 deletion things/postgres/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ func TestThingUpdate(t *testing.T) {
Key: thkey,
}

sths, _ := thingRepo.Save(context.Background(), thing)
sths, err := thingRepo.Save(context.Background(), thing)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

thing.ID = sths[0].ID

nonexistentThingID, err := uuidProvider.New().ID()
Expand Down
12 changes: 8 additions & 4 deletions users/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,24 @@ func passwordResetEndpoint(svc users.Service) endpoint.Endpoint {
}
}

func userInfoEndpoint(svc users.Service) endpoint.Endpoint {
func viewUserEndpoint(svc users.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(viewUserInfoReq)
req := request.(viewUserReq)

if err := req.validate(); err != nil {
return nil, err
}

u, err := svc.UserInfo(ctx, req.token)
u, err := svc.ViewUser(ctx, req.token)
if err != nil {
return nil, err
}

return identityRes{u.Email, u.Metadata}, nil
return viewUserRes{
ID: u.ID,
Email: u.Email,
Metadata: u.Metadata,
}, nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion users/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestLogin(t *testing.T) {
}
}

func TestUserInfo(t *testing.T) {
func TestViewUser(t *testing.T) {
svc := newService()
ts := newServer(svc)
defer ts.Close()
Expand Down
6 changes: 3 additions & 3 deletions users/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ func (lm *loggingMiddleware) Login(ctx context.Context, user users.User) (token
return lm.svc.Login(ctx, user)
}

func (lm *loggingMiddleware) UserInfo(ctx context.Context, token string) (u users.User, err error) {
func (lm *loggingMiddleware) ViewUser(ctx context.Context, token string) (u users.User, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method user_info for user %s took %s to complete", u.Email, time.Since(begin))
message := fmt.Sprintf("Method view_user for user %s took %s to complete", u.Email, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.UserInfo(ctx, token)
return lm.svc.ViewUser(ctx, token)
}

func (lm *loggingMiddleware) UpdateUser(ctx context.Context, token string, u users.User) (err error) {
Expand Down
8 changes: 4 additions & 4 deletions users/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func (ms *metricsMiddleware) Login(ctx context.Context, user users.User) (string
return ms.svc.Login(ctx, user)
}

func (ms *metricsMiddleware) UserInfo(ctx context.Context, token string) (users.User, error) {
func (ms *metricsMiddleware) ViewUser(ctx context.Context, token string) (users.User, error) {
defer func(begin time.Time) {
ms.counter.With("method", "user_info").Add(1)
ms.latency.With("method", "user_info").Observe(time.Since(begin).Seconds())
ms.counter.With("method", "view_user").Add(1)
ms.latency.With("method", "view_user").Observe(time.Since(begin).Seconds())
}(time.Now())

return ms.svc.UserInfo(ctx, token)
return ms.svc.ViewUser(ctx, token)
}

func (ms *metricsMiddleware) UpdateUser(ctx context.Context, token string, u users.User) (err error) {
Expand Down
4 changes: 2 additions & 2 deletions users/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func (req userReq) validate() error {
return req.user.Validate()
}

type viewUserInfoReq struct {
type viewUserReq struct {
token string
}

func (req viewUserInfoReq) validate() error {
func (req viewUserReq) validate() error {
if req.token == "" {
return users.ErrUnauthorizedAccess
}
Expand Down
11 changes: 6 additions & 5 deletions users/api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

var (
_ mainflux.Response = (*tokenRes)(nil)
_ mainflux.Response = (*identityRes)(nil)
_ mainflux.Response = (*viewUserRes)(nil)
_ mainflux.Response = (*passwChangeRes)(nil)
)

Expand Down Expand Up @@ -48,20 +48,21 @@ func (res updateUserRes) Empty() bool {
return true
}

type identityRes struct {
type viewUserRes struct {
ID string `json:"id"`
Email string `json:"email"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

func (res identityRes) Code() int {
func (res viewUserRes) Code() int {
return http.StatusOK
}

func (res identityRes) Headers() map[string]string {
func (res viewUserRes) Headers() map[string]string {
return map[string]string{}
}

func (res identityRes) Empty() bool {
func (res viewUserRes) Empty() bool {
return false
}

Expand Down
8 changes: 4 additions & 4 deletions users/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func MakeHandler(svc users.Service, tracer opentracing.Tracer, l log.Logger) htt
))

mux.Get("/users", kithttp.NewServer(
kitot.TraceServer(tracer, "user_info")(userInfoEndpoint(svc)),
decodeViewInfo,
kitot.TraceServer(tracer, "view_user")(viewUserEndpoint(svc)),
decodeViewUser,
encodeResponse,
opts...,
))
Expand Down Expand Up @@ -100,8 +100,8 @@ func MakeHandler(svc users.Service, tracer opentracing.Tracer, l log.Logger) htt
return mux
}

func decodeViewInfo(_ context.Context, r *http.Request) (interface{}, error) {
req := viewUserInfoReq{
func decodeViewUser(_ context.Context, r *http.Request) (interface{}, error) {
req := viewUserReq{
token: r.Header.Get("Authorization"),
}
return req, nil
Expand Down
2 changes: 1 addition & 1 deletion users/mocks/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (urm *userRepositoryMock) UpdateUser(ctx context.Context, user users.User)
return nil
}

func (urm *userRepositoryMock) RetrieveByID(ctx context.Context, email string) (users.User, error) {
func (urm *userRepositoryMock) RetrieveByEmail(ctx context.Context, email string) (users.User, error) {
urm.mu.Lock()
defer urm.mu.Unlock()

Expand Down
13 changes: 10 additions & 3 deletions users/postgres/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ func migrateDB(db *sqlx.DB) error {
Id: "users_1",
Up: []string{
`CREATE TABLE IF NOT EXISTS users (
email VARCHAR(254) PRIMARY KEY,
password CHAR(60) NOT NULL
)`,
email VARCHAR(254) PRIMARY KEY,
password CHAR(60) NOT NULL)`,
},
Down: []string{"DROP TABLE users"},
},
Expand All @@ -61,6 +60,14 @@ func migrateDB(db *sqlx.DB) error {
`ALTER TABLE IF EXISTS users ADD COLUMN IF NOT EXISTS metadata JSONB`,
},
},
{
Id: "users_3",
Up: []string{
`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
ALTER TABLE IF EXISTS users ADD COLUMN IF NOT EXISTS
id UUID DEFAULT uuid_generate_v4()`,
},
},
},
}

Expand Down
10 changes: 6 additions & 4 deletions users/postgres/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func New(db Database) users.UserRepository {
}

func (ur userRepository) Save(ctx context.Context, user users.User) error {
q := `INSERT INTO users (email, password, metadata) VALUES (:email, :password, :metadata)`
q := `INSERT INTO users (id, email, password, metadata) VALUES (:id, :email, :password, :metadata)`

dbu := toDBUser(user)
if _, err := ur.db.NamedExecContext(ctx, q, dbu); err != nil {
Expand Down Expand Up @@ -70,8 +70,8 @@ func (ur userRepository) UpdateUser(ctx context.Context, user users.User) error
return nil
}

func (ur userRepository) RetrieveByID(ctx context.Context, email string) (users.User, error) {
q := `SELECT password, metadata FROM users WHERE email = $1`
func (ur userRepository) RetrieveByEmail(ctx context.Context, email string) (users.User, error) {
q := `SELECT id, password, metadata FROM users WHERE email = $1`

dbu := dbUser{
Email: email,
Expand Down Expand Up @@ -121,7 +121,6 @@ func (m *dbMetadata) Scan(value interface{}) error {
}

if err := json.Unmarshal(b, m); err != nil {
m = &dbMetadata{}
return err
}

Expand All @@ -142,13 +141,15 @@ func (m dbMetadata) Value() (driver.Value, error) {
}

type dbUser struct {
ID string `db:"id"`
Email string `db:"email"`
Password string `db:"password"`
Metadata dbMetadata `db:"metadata"`
}

func toDBUser(u users.User) dbUser {
return dbUser{
ID: u.ID,
Email: u.Email,
Password: u.Password,
Metadata: u.Metadata,
Expand All @@ -157,6 +158,7 @@ func toDBUser(u users.User) dbUser {

func toUser(dbu dbUser) users.User {
return users.User{
ID: dbu.ID,
Email: dbu.Email,
Password: dbu.Password,
Metadata: dbu.Metadata,
Expand Down
Loading

0 comments on commit 8db82e1

Please sign in to comment.