2018-11-08 14:19:42 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2018-11-08 14:19:42 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/satellite"
|
|
|
|
"storj.io/storj/pkg/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
2018-11-09 12:05:24 +00:00
|
|
|
// implementation of Users interface repository using spacemonkeygo/dbx orm
|
2018-11-08 14:19:42 +00:00
|
|
|
type users struct {
|
2018-12-10 12:29:01 +00:00
|
|
|
db dbx.Methods
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get is a method for querying user from the database by id
|
|
|
|
func (users *users) Get(ctx context.Context, id uuid.UUID) (*satellite.User, error) {
|
2018-11-12 09:14:16 +00:00
|
|
|
user, err := users.db.Get_User_By_Id(ctx, dbx.User_Id(id[:]))
|
2018-11-08 14:19:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return userFromDBX(user)
|
|
|
|
}
|
|
|
|
|
2018-12-10 13:47:48 +00:00
|
|
|
// GetByEmail is a method for querying user by email from the database.
|
|
|
|
func (users *users) GetByEmail(ctx context.Context, email string) (*satellite.User, error) {
|
|
|
|
user, err := users.db.Get_User_By_Email(ctx, dbx.User_Email(email))
|
2018-11-08 14:19:42 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return userFromDBX(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert is a method for inserting user into the database
|
2018-11-09 12:05:24 +00:00
|
|
|
func (users *users) Insert(ctx context.Context, user *satellite.User) (*satellite.User, error) {
|
2018-11-13 08:27:42 +00:00
|
|
|
userID, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-09 12:05:24 +00:00
|
|
|
createdUser, err := users.db.Create_User(ctx,
|
2018-11-13 08:27:42 +00:00
|
|
|
dbx.User_Id(userID[:]),
|
2018-11-08 14:19:42 +00:00
|
|
|
dbx.User_FirstName(user.FirstName),
|
|
|
|
dbx.User_LastName(user.LastName),
|
|
|
|
dbx.User_Email(user.Email),
|
|
|
|
dbx.User_PasswordHash(user.PasswordHash))
|
|
|
|
|
2018-11-09 12:05:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return userFromDBX(createdUser)
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is a method for deleting user by Id from the database.
|
|
|
|
func (users *users) Delete(ctx context.Context, id uuid.UUID) error {
|
2018-11-12 09:14:16 +00:00
|
|
|
_, err := users.db.Delete_User_By_Id(ctx, dbx.User_Id(id[:]))
|
2018-11-08 14:19:42 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is a method for updating user entity
|
|
|
|
func (users *users) Update(ctx context.Context, user *satellite.User) error {
|
2018-11-28 10:31:15 +00:00
|
|
|
_, err := users.db.Update_User_By_Id(
|
|
|
|
ctx,
|
2018-11-12 09:14:16 +00:00
|
|
|
dbx.User_Id(user.ID[:]),
|
2018-11-28 10:31:15 +00:00
|
|
|
toUpdateUser(user),
|
|
|
|
)
|
2018-11-08 14:19:42 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:31:15 +00:00
|
|
|
// toUpdateUser creates dbx.User_Update_Fields with only non-empty fields as updatable
|
|
|
|
func toUpdateUser(user *satellite.User) dbx.User_Update_Fields {
|
|
|
|
update := dbx.User_Update_Fields{
|
|
|
|
Email: dbx.User_Email(user.Email),
|
|
|
|
FirstName: dbx.User_FirstName(user.FirstName),
|
|
|
|
LastName: dbx.User_LastName(user.LastName),
|
|
|
|
}
|
|
|
|
|
|
|
|
// extra password check to update only calculated hash from service
|
2018-12-10 15:57:06 +00:00
|
|
|
if len(user.PasswordHash) != 0 {
|
2018-11-28 10:31:15 +00:00
|
|
|
update.PasswordHash = dbx.User_PasswordHash(user.PasswordHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:19:42 +00:00
|
|
|
// userFromDBX is used for creating User entity from autogenerated dbx.User struct
|
|
|
|
func userFromDBX(user *dbx.User) (*satellite.User, error) {
|
|
|
|
if user == nil {
|
|
|
|
return nil, errs.New("user parameter is nil")
|
|
|
|
}
|
|
|
|
|
2018-11-12 09:14:16 +00:00
|
|
|
id, err := bytesToUUID(user.Id)
|
2018-11-08 14:19:42 +00:00
|
|
|
if err != nil {
|
2018-11-12 09:14:16 +00:00
|
|
|
return nil, err
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
return &satellite.User{
|
2018-11-12 09:14:16 +00:00
|
|
|
ID: id,
|
2018-11-09 12:05:24 +00:00
|
|
|
FirstName: user.FirstName,
|
|
|
|
LastName: user.LastName,
|
|
|
|
Email: user.Email,
|
|
|
|
PasswordHash: user.PasswordHash,
|
|
|
|
CreatedAt: user.CreatedAt,
|
2018-11-20 14:50:47 +00:00
|
|
|
}, nil
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|