Implement the database routine

This commit is contained in:
Alphyron 2020-05-09 22:44:28 +02:00
parent ca63d7be2f
commit 498ddbc31e
8 changed files with 95 additions and 10 deletions

View File

@ -2,6 +2,9 @@
FROM golang:1.13-stretch AS build-env
COPY . /src
WORKDIR /src
ENV GOPROXY=http://192.168.0.143:3000
ENV GO113MODULE=on
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go test ./... -cover -coverprofile=c.out #gosetup
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o goapp

View File

@ -55,7 +55,8 @@ func (g groupHelperService) CreateUser(user *models.User) (*models.User, error)
}
func (g groupHelperService) GetUserByID(userID int64) (*models.User, error) {
return g.groupHelperRepo.GetUserByID(userID)
user, err := g.groupHelperRepo.GetUserByID(userID)
return user, err
}
func (g groupHelperService) UserJoinGroup(user *models.User, group *models.Group) (bool, error) {

View File

@ -56,17 +56,19 @@ func main() {
service := logic.NewGroupHelperService(groupHelperRepo)
cm := InitialCommandManager(bot, service)
rm := InitialRoutineManager(bot, cm)
rm := InitialRoutineManager(bot, cm, service)
rm.StartUpdates()
}
func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager) *telegram.RoutineManager {
func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager, service logic.GroupHelperService) *telegram.RoutineManager {
rm := &telegram.RoutineManager{
Bot: bot,
Bot: bot,
GroupHelperService: service,
}
rm.RegisterRoutine(routines.NewCommandRoutine(commandManager))
rm.RegisterRoutine(routines.NewDatabaseRoutine(service))
return rm
}

View File

@ -124,15 +124,22 @@ func (g groupHelperRepository) CreateUser(user *models.User) (*models.User, erro
func (g groupHelperRepository) GetUserByID(userID int64) (*models.User, error) {
var user models.User
err := g.Conn.Where("user_id = ?", userID).First(&user).Error
db := g.Conn.Where("user_id = ?", userID).First(&user)
if err != nil {
return nil, errors.New("problem while getting the user by his user id - " + err.Error())
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 {

View File

@ -27,7 +27,7 @@ func (rm RoutineManager) StartUpdates() {
}
for _, routine := range rm.Routine {
err := routine.Update(&update, group)
err := routine.Update(rm.Bot, &update, group)
if err != nil {
log.Printf("ERROR - Routine error")

View File

@ -6,5 +6,5 @@ import (
)
type Routine interface {
Update(update *tgbotapi.Update, group *models.Group) error
Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error
}

View File

@ -15,7 +15,7 @@ func NewCommandRoutine(commandManager *cli.CommandManager) telegram.Routine {
return &commandRoutine{CommandManager: commandManager}
}
func (cr commandRoutine) Update(update *tgbotapi.Update, group *models.Group) error {
func (cr commandRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error {
_, err := cr.CommandManager.ExecuteUpdate(update, group)
return err

View File

@ -0,0 +1,72 @@
package routines
import (
"git.dragon-labs.de/alphyron/group_helper/logic"
"git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/telegram"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
type databaseRoutine struct {
groupHelperService logic.GroupHelperService
}
func NewDatabaseRoutine(groupHelperService logic.GroupHelperService) telegram.Routine {
return &databaseRoutine{groupHelperService: groupHelperService}
}
func (d databaseRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error {
if update.Message == nil { // ignore any non-Message Updates
return nil
}
if update.Message.Chat.IsPrivate() || update.Message.Chat.IsChannel() {
return nil
}
if update.Message.NewChatMembers != nil {
for _, newUser := range *update.Message.NewChatMembers {
user, err := d.groupHelperService.GetUserByID(int64(newUser.ID))
if err != nil {
return err
}
go d.groupHelperService.UserJoinGroup(user, group)
}
config := tgbotapi.ChatConfig{
ChatID: update.Message.Chat.ID,
}
size, _ := botAPI.GetChatMembersCount(config)
group.Size = size - 1
_, err := d.groupHelperService.UpdateGroup(group)
return err
}
if update.Message.LeftChatMember != nil {
user, err := d.groupHelperService.GetUserByID(int64(update.Message.LeftChatMember.ID))
if err != nil {
return err
}
go d.groupHelperService.UserLeaveGroup(user, group)
config := tgbotapi.ChatConfig{
ChatID: update.Message.Chat.ID,
}
size, _ := botAPI.GetChatMembersCount(config)
group.Size = size - 1
_, err = d.groupHelperService.UpdateGroup(group)
return err
}
user, err := d.groupHelperService.GetUserByID(int64(update.Message.From.ID))
if err != nil {
return err
}
_, err = d.groupHelperService.UserJoinGroup(user, group)
return err
}