🗃️ Implement the GroupHelperRepository with the different database connections

This commit is contained in:
Alphyron 2020-04-11 22:27:56 +02:00
parent ce0f5bafa0
commit f4e902d5f1
8 changed files with 255 additions and 10 deletions

20
config/database.go Normal file
View File

@ -0,0 +1,20 @@
package config
import "github.com/caarlos0/env"
type DatabaseConfig struct {
Debug bool `env:"DB_DEBUG" envDefault:"false"`
DatabaseType string `env:"DB_TYPE,required"`
Host string `env:"DB_HOST,required"`
Port int `env:"DB_PORT"`
Username string `env:"DB_USER"`
Password string `env:"DB_PASS"`
Database string `env:"DB_DATABASE"`
}
func (dc *DatabaseConfig) LoadConfig() error {
if err := env.Parse(dc); err != nil {
return err
}
return nil
}

6
go.mod
View File

@ -1,3 +1,9 @@
module git.dragon-labs.de/alphyron/group_helper module git.dragon-labs.de/alphyron/group_helper
go 1.13 go 1.13
require (
github.com/caarlos0/env v3.5.0+incompatible
github.com/jinzhu/gorm v1.9.12
github.com/stretchr/testify v1.5.1 // indirect
)

6
logic/repository.go Normal file
View File

@ -0,0 +1,6 @@
package logic
type GroupHelperRepository interface {
GroupHelperService
InitRepository() error
}

View File

@ -7,10 +7,10 @@ type GroupHelperService interface {
UpdateGroup(*models.Group) (*models.Group, error) UpdateGroup(*models.Group) (*models.Group, error)
GetGroupByID(int64) (*models.Group, error) GetGroupByID(int64) (*models.Group, error)
GetGroupDatabaseSize(int64) (int, error) GetGroupDatabaseSize(*models.Group) (int, error)
CreateUser(*models.User) (*models.User, error) CreateUser(*models.User) (*models.User, error)
GetUserByID(int64) *models.User GetUserByID(int64) (*models.User, error)
UserJoinGroup(*models.User, *models.Group) (bool, error) UserJoinGroup(*models.User, *models.Group) (bool, error)
UserLeaveGroup(*models.User, *models.Group) (bool, error) UserLeaveGroup(*models.User, *models.Group) (bool, error)

12
main.go
View File

@ -1,7 +1,15 @@
package main package main
import "log" import (
"git.dragon-labs.de/alphyron/group_helper/config"
"log"
)
func main() { func main() {
log.Println("Hello World") err := config.DatabaseConfig{}.LoadConfig()
if err != nil {
log.Println("Problem while loading database environment variables")
log.Fatal(err)
}
} }

View File

@ -10,6 +10,7 @@ type Group struct {
UserKickMessage string `gorm:"column:msg_user_kick;type:text"` UserKickMessage string `gorm:"column:msg_user_kick;type:text"`
ForbidWriting bool `gorm:"column:forbid_writing"` ForbidWriting bool `gorm:"column:forbid_writing"`
OnlineCheck int `gorm:"column:kick_cooldown"` OnlineCheck int `gorm:"column:kick_cooldown"`
Users []*User `gorm:"many2many:GroupUser;"`
} }
func (group *Group) FillDefaultValues() { func (group *Group) FillDefaultValues() {

View File

@ -0,0 +1,18 @@
package repository
import (
"errors"
"git.dragon-labs.de/alphyron/group_helper/config"
"git.dragon-labs.de/alphyron/group_helper/logic"
)
func NewUserHelperRepository() (*logic.GroupHelperRepository, error) {
databaseConfig := config.DatabaseConfig{}
_ = databaseConfig.LoadConfig()
switch databaseConfig.DatabaseType {
}
return nil, errors.New("cannot support given database type (" + databaseConfig.DatabaseType + ")")
}

View File

@ -0,0 +1,186 @@
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
err := g.Conn.Where("group_id = ?", id).First(&group).Error
if err != nil {
return nil, errors.New("problem while getting the group by his group id - " + err.Error())
}
return &group, nil
}
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
err := g.Conn.Where("user_id = ?", userID).First(&user).Error
if err != nil {
return nil, errors.New("problem while getting the user by his user id - " + err.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").Append(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) 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
}