1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Dejan Mijic c966a7802d
Integrate manager service
Setup top-level glide dependencies file. Migrated all of the manager
service code into this repository. Fixed docker build procedure.
Extracted executable to the top-level.

Signed-off-by: Dejan Mijic <dejan@mainflux.com>
2017-09-23 01:03:27 +02:00

42 lines
979 B
Go

package cassandra
import (
"github.com/gocql/gocql"
"github.com/mainflux/mainflux/manager"
)
var _ manager.UserRepository = (*userRepository)(nil)
type userRepository struct {
session *gocql.Session
}
// NewUserRepository instantiates Cassandra user repository.
func NewUserRepository(session *gocql.Session) manager.UserRepository {
return &userRepository{session}
}
func (repo *userRepository) Save(user manager.User) error {
cql := `INSERT INTO users (email, password) VALUES (?, ?) IF NOT EXISTS`
applied, err := repo.session.Query(cql, user.Email, user.Password).ScanCAS()
if !applied {
return manager.ErrConflict
}
return err
}
func (repo *userRepository) One(email string) (manager.User, error) {
cql := `SELECT email, password FROM users WHERE email = ? LIMIT 1`
user := manager.User{}
if err := repo.session.Query(cql, email).
Scan(&user.Email, &user.Password); err != nil {
return user, manager.ErrInvalidCredentials
}
return user, nil
}