209 lines
6.1 KiB
Go
209 lines
6.1 KiB
Go
package sql
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"git.dragon-labs.de/alphyron/group_helper/config"
|
|
"git.dragon-labs.de/alphyron/group_helper/logic"
|
|
"git.dragon-labs.de/alphyron/group_helper/models"
|
|
"github.com/jinzhu/gorm"
|
|
_ "github.com/jinzhu/gorm/dialects/mssql"
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
|
)
|
|
|
|
type groupHelperRepository struct {
|
|
Conn *gorm.DB
|
|
}
|
|
|
|
func newMySQLConnection(host string, database string, user string, password string) (*gorm.DB, error) {
|
|
return gorm.Open("mysql", fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, database))
|
|
}
|
|
|
|
func newPostgresConnection(host string, port int, database string, user string, password string) (*gorm.DB, error) {
|
|
return gorm.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s", host, port, user, database, password))
|
|
}
|
|
|
|
func newSQLiteConnection(path string) (*gorm.DB, error) {
|
|
return gorm.Open("sqlite3", path)
|
|
}
|
|
|
|
func newMsSqlConnection(host string, database string, user string, password string) (*gorm.DB, error) {
|
|
return gorm.Open("mssql", fmt.Sprintf("sqlserver://%s:%s@%s?database=%s", user, password, host, database))
|
|
}
|
|
|
|
// NewUserRepository tamdp, test
|
|
func NewGroupHelperRepository(config *config.DatabaseConfig) (logic.GroupHelperRepository, error) {
|
|
var (
|
|
db *gorm.DB
|
|
err error
|
|
)
|
|
|
|
switch config.DatabaseType {
|
|
case "mysql":
|
|
db, err = newMySQLConnection(fmt.Sprintf("%s:%d", config.Host, config.Port), config.Database, config.Username, config.Password)
|
|
break
|
|
case "postgres":
|
|
db, err = newPostgresConnection(config.Host, config.Port, config.Database, config.Username, config.Password)
|
|
break
|
|
case "sqlite3":
|
|
db, err = newSQLiteConnection(config.Host)
|
|
break
|
|
case "mssql":
|
|
db, err = newMsSqlConnection(fmt.Sprintf("%s:%d", config.Host, config.Port), config.Database, config.Username, config.Password)
|
|
break
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if db == nil {
|
|
return nil, errors.New("no valid type given (mysql,postgres,sqlite3,mssql)")
|
|
}
|
|
|
|
db.LogMode(config.Debug)
|
|
return &groupHelperRepository{Conn: db}, nil
|
|
}
|
|
|
|
// NewUserRepositoryDBConnection creates a user repository with an existing gorm database connection
|
|
func NewUserRepositoryDBConnection(db *gorm.DB) logic.GroupHelperRepository {
|
|
return &groupHelperRepository{Conn: db}
|
|
}
|
|
|
|
func (g groupHelperRepository) CreateGroup(group *models.Group) (*models.Group, error) {
|
|
err := g.Conn.Create(group).Error
|
|
|
|
if err != nil {
|
|
return nil, errors.New("problem while creating a group - " + err.Error())
|
|
}
|
|
|
|
return group, nil
|
|
}
|
|
|
|
func (g groupHelperRepository) UpdateGroup(group *models.Group) (*models.Group, error) {
|
|
err := g.Conn.Where("id = ?", group.ID).Save(group).Error
|
|
return group, err
|
|
}
|
|
|
|
func (g groupHelperRepository) GetGroupByID(id int64) (*models.Group, error) {
|
|
var group models.Group
|
|
|
|
defaultGroup := &models.Group{}
|
|
defaultGroup.FillDefaultValues()
|
|
defaultGroup.GroupID = id
|
|
|
|
err := g.Conn.Where("group_id = ?", id).First(&group)
|
|
|
|
if err.RecordNotFound() {
|
|
return g.CreateGroup(defaultGroup)
|
|
}
|
|
return &group, err.Error
|
|
}
|
|
|
|
func (g groupHelperRepository) GetGroupDatabaseSize(group *models.Group) (int, error) {
|
|
association := g.Conn.Model(group).Association("Users")
|
|
|
|
if err := association.Error; err != nil {
|
|
return -1, errors.New("problem while getting the size of users connected with the group - " + err.Error())
|
|
}
|
|
|
|
return association.Count(), nil
|
|
}
|
|
|
|
func (g groupHelperRepository) CreateUser(user *models.User) (*models.User, error) {
|
|
err := g.Conn.Create(user).Error
|
|
|
|
if err != nil {
|
|
return nil, errors.New("problem while creating a user - " + err.Error())
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func (g groupHelperRepository) GetUserByID(userID int64) (*models.User, error) {
|
|
var user models.User
|
|
db := g.Conn.Where("user_id = ?", userID).First(&user)
|
|
|
|
if db.RecordNotFound() {
|
|
return g.CreateUser(&models.User{
|
|
UserID: userID,
|
|
})
|
|
}
|
|
|
|
if db.Error != nil {
|
|
return nil, errors.New("problem while getting the user by his user id - " + db.Error.Error())
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (g groupHelperRepository) UserJoinGroup(user *models.User, group *models.Group) (bool, error) {
|
|
|
|
err := g.Conn.Model(user).Association("Groups").Append(group).Error
|
|
|
|
if err != nil {
|
|
return false, errors.New("problem while connect the user with the group - " + err.Error())
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (g groupHelperRepository) UserLeaveGroup(user *models.User, group *models.Group) (bool, error) {
|
|
err := g.Conn.Model(user).Association("Groups").Delete(group).Error
|
|
|
|
if err != nil {
|
|
return false, errors.New("problem while disconnecting the user from the group - " + err.Error())
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (g groupHelperRepository) IsUserInGroup(user *models.User, group *models.Group) (bool, error) {
|
|
association := g.Conn.Model(user).Association("Groups")
|
|
|
|
if err := association.Error; err != nil {
|
|
return false, errors.New("problem while checking if the user is connected with the group - " + err.Error())
|
|
}
|
|
|
|
association = association.Find(group)
|
|
|
|
if err := association.Error; err != nil {
|
|
return false, errors.New("problem while find the user is connected with the group - " + err.Error())
|
|
}
|
|
|
|
return association.Count() > 0, nil
|
|
}
|
|
|
|
func (g groupHelperRepository) ListGroups() ([]*models.Group, error) {
|
|
var groups []*models.Group
|
|
err := g.Conn.Find(&groups).Error
|
|
|
|
if err != nil {
|
|
return nil, errors.New("problem while getting all group - " + err.Error())
|
|
}
|
|
return groups, nil
|
|
}
|
|
|
|
func (g groupHelperRepository) ListGroupUsers(group *models.Group) ([]*models.User, error) {
|
|
var users []*models.User
|
|
err := g.Conn.Model(group).Related(&users, "GroupUser").Error
|
|
|
|
if err != nil {
|
|
return nil, errors.New("problem while getting all users from group - " + err.Error())
|
|
}
|
|
return users, nil
|
|
}
|
|
|
|
func (g groupHelperRepository) ListUsers() ([]*models.User, error) {
|
|
var users []*models.User
|
|
err := g.Conn.Find(&users).Error
|
|
|
|
if err != nil {
|
|
return nil, errors.New("problem while getting all users - " + err.Error())
|
|
}
|
|
return users, nil
|
|
}
|
|
|
|
func (g groupHelperRepository) InitRepository() error {
|
|
return g.Conn.AutoMigrate(models.Group{}, models.User{}).Error
|
|
}
|