group_helper/logic/grouphelperservice.go
2020-05-09 22:44:28 +02:00

105 lines
2.9 KiB
Go

package logic
import (
"errors"
"git.dragon-labs.de/alphyron/group_helper/models"
)
type groupHelperService struct {
groupHelperRepo GroupHelperRepository
}
func NewGroupHelperService(groupHelperRepo GroupHelperRepository) GroupHelperService {
return &groupHelperService{
groupHelperRepo: groupHelperRepo,
}
}
func (g groupHelperService) CreateGroup(group *models.Group) (*models.Group, error) {
if group.GroupID == 0 {
return nil, errors.New("you need set the group id to create a new group")
}
group.FillDefaultValues()
return g.CreateGroup(group)
}
func (g groupHelperService) UpdateGroup(group *models.Group) (*models.Group, error) {
if group.GroupID == 0 {
return nil, errors.New("you need set the group id to update an existing group")
}
return g.groupHelperRepo.UpdateGroup(group)
}
func (g groupHelperService) GetGroupByID(groupID int64) (*models.Group, error) {
return g.groupHelperRepo.GetGroupByID(groupID)
}
func (g groupHelperService) GetGroupDatabaseSize(group *models.Group) (int, error) {
if group.ID == 0 {
return -1, errors.New("you need to set the id to get the size of connected users")
}
return g.groupHelperRepo.GetGroupDatabaseSize(group)
}
func (g groupHelperService) CreateUser(user *models.User) (*models.User, error) {
if user.UserID == 0 {
return nil, errors.New("you need to set the userID to create a new user")
}
return g.groupHelperRepo.CreateUser(user)
}
func (g groupHelperService) GetUserByID(userID int64) (*models.User, error) {
user, err := g.groupHelperRepo.GetUserByID(userID)
return user, err
}
func (g groupHelperService) UserJoinGroup(user *models.User, group *models.Group) (bool, error) {
if user.ID == 0 {
return false, errors.New("you need to give a valid id from the user to join a group")
}
if group.ID == 0 {
return false, errors.New("you need to give a valid id from the group to let a user join")
}
return g.groupHelperRepo.UserJoinGroup(user, group)
}
func (g groupHelperService) UserLeaveGroup(user *models.User, group *models.Group) (bool, error) {
if user.ID == 0 {
return false, errors.New("you need to give a valid id from the user to leave from group")
}
if group.ID == 0 {
return false, errors.New("you need to give a valid id from the group to let a user leave")
}
return g.groupHelperRepo.UserLeaveGroup(user, group)
}
func (g groupHelperService) IsUserInGroup(user *models.User, group *models.Group) (bool, error) {
if user.ID == 0 {
return false, errors.New("you need to give a valid id from the user to check if he is in a group")
}
if group.ID == 0 {
return false, errors.New("you need to give a valid id from the group to check if a user is in the group")
}
return g.groupHelperRepo.UserJoinGroup(user, group)
}
func (g groupHelperService) ListGroups() ([]*models.Group, error) {
return g.groupHelperRepo.ListGroups()
}
func (g groupHelperService) ListUsers() ([]*models.User, error) {
return g.groupHelperRepo.ListUsers()
}