2021-07-12 18:22:16 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2021-12-17 15:55:07 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-07-12 18:22:16 +00:00
|
|
|
"log"
|
2021-09-21 02:53:40 +00:00
|
|
|
"time"
|
2021-07-12 18:22:16 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
"github.com/arangodb/go-driver"
|
|
|
|
arangoDriver "github.com/arangodb/go-driver"
|
2021-10-03 16:03:55 +00:00
|
|
|
"github.com/google/uuid"
|
2021-07-12 18:22:16 +00:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
2021-12-17 15:55:07 +00:00
|
|
|
Key string `json:"_key,omitempty"` // for arangodb
|
|
|
|
ObjectID string `json:"_id,omitempty"` // for arangodb & mongodb
|
|
|
|
ID string `gorm:"primaryKey;type:char(36)" json:"id"`
|
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
|
|
|
Email string `gorm:"unique" json:"email"`
|
|
|
|
Password string `gorm:"type:text" json:"password"`
|
|
|
|
SignupMethod string `json:"signup_method"`
|
|
|
|
EmailVerifiedAt int64 `json:"email_verified_at"`
|
|
|
|
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at"`
|
|
|
|
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at"`
|
|
|
|
Image string `gorm:"type:text" json:"image"`
|
|
|
|
Roles string `json:"roles"`
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
// AddUser function to add user even with email conflict
|
|
|
|
func (mgr *manager) AddUser(user User) (User, error) {
|
|
|
|
if user.ID == "" {
|
|
|
|
user.ID = uuid.New().String()
|
|
|
|
}
|
2021-10-03 16:03:55 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsSQL {
|
|
|
|
// copy id as value for fields required for mongodb & arangodb
|
|
|
|
user.Key = user.ID
|
|
|
|
user.ObjectID = user.ID
|
|
|
|
result := mgr.sqlDB.Clauses(
|
|
|
|
clause.OnConflict{
|
|
|
|
UpdateAll: true,
|
|
|
|
Columns: []clause.Column{{Name: "email"}},
|
|
|
|
}).Create(&user)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println("error adding user:", result.Error)
|
|
|
|
return user, result.Error
|
|
|
|
}
|
|
|
|
}
|
2021-10-03 16:03:55 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsArangoDB {
|
|
|
|
user.CreatedAt = time.Now().Unix()
|
|
|
|
user.UpdatedAt = time.Now().Unix()
|
|
|
|
ctx := context.Background()
|
|
|
|
userCollection, _ := mgr.arangodb.Collection(nil, Collections.User)
|
|
|
|
meta, err := userCollection.CreateDocument(arangoDriver.WithOverwrite(ctx), user)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error adding user:", err)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
user.Key = meta.Key
|
|
|
|
user.ObjectID = meta.ID.String()
|
2021-07-18 03:55:20 +00:00
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser function to update user with ID conflict
|
|
|
|
func (mgr *manager) UpdateUser(user User) (User, error) {
|
2021-09-21 02:53:40 +00:00
|
|
|
user.UpdatedAt = time.Now().Unix()
|
2021-12-17 15:55:07 +00:00
|
|
|
|
|
|
|
if IsSQL {
|
|
|
|
result := mgr.sqlDB.Save(&user)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println("error updating user:", result.Error)
|
|
|
|
return user, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
collection, _ := mgr.arangodb.Collection(nil, Collections.User)
|
|
|
|
meta, err := collection.UpdateDocument(nil, user.Key, user)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error updating user:", err)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Key = meta.Key
|
|
|
|
user.ObjectID = meta.ID.String()
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsers function to get all users
|
|
|
|
func (mgr *manager) GetUsers() ([]User, error) {
|
|
|
|
var users []User
|
2021-12-17 15:55:07 +00:00
|
|
|
|
|
|
|
if IsSQL {
|
|
|
|
result := mgr.sqlDB.Find(&users)
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println("error getting users:", result.Error)
|
|
|
|
return users, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
query := fmt.Sprintf("FOR d in %s RETURN d", Collections.User)
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, nil)
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var user User
|
|
|
|
meta, err := cursor.ReadDocument(nil, &user)
|
|
|
|
|
|
|
|
if driver.IsNoMoreDocuments(err) {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.Key != "" {
|
|
|
|
user.Key = meta.Key
|
|
|
|
user.ObjectID = meta.ID.String()
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *manager) GetUserByEmail(email string) (User, error) {
|
|
|
|
var user User
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsSQL {
|
|
|
|
result := mgr.sqlDB.Where("email = ?", email).First(&user)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return user, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
query := fmt.Sprintf("FOR d in %s FILTER d.email == @email LIMIT 1 RETURN d", Collections.User)
|
|
|
|
bindVars := map[string]interface{}{
|
|
|
|
"email": email,
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, bindVars)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err := cursor.ReadDocument(nil, &user)
|
|
|
|
if driver.IsNoMoreDocuments(err) {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
|
2021-09-21 02:53:40 +00:00
|
|
|
func (mgr *manager) GetUserByID(id string) (User, error) {
|
|
|
|
var user User
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsSQL {
|
|
|
|
result := mgr.sqlDB.Where("id = ?", id).First(&user)
|
2021-09-21 02:53:40 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
return user, result.Error
|
|
|
|
}
|
2021-07-15 12:02:55 +00:00
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsArangoDB {
|
|
|
|
query := fmt.Sprintf("FOR d in %s FILTER d.id == @id LIMIT 1 RETURN d", Collections.User)
|
|
|
|
bindVars := map[string]interface{}{
|
|
|
|
"id": id,
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, bindVars)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
count := cursor.Count()
|
|
|
|
if count == 0 {
|
|
|
|
return user, errors.New("user not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err := cursor.ReadDocument(nil, &user)
|
|
|
|
if driver.IsNoMoreDocuments(err) {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
return user, nil
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
2021-08-06 13:47:52 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
func (mgr *manager) DeleteUser(user User) error {
|
|
|
|
if IsSQL {
|
|
|
|
result := mgr.sqlDB.Delete(&user)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error deleting user:`, result.Error)
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 13:47:52 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsArangoDB {
|
|
|
|
collection, _ := mgr.arangodb.Collection(nil, Collections.User)
|
|
|
|
_, err := collection.RemoveDocument(nil, user.Key)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`error deleting user:`, err)
|
|
|
|
return err
|
|
|
|
}
|
2021-08-06 13:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|