From 91843e34e432f0d2305fffa0dd1feea1bd4953d6 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Sat, 11 Apr 2020 22:53:59 +0200 Subject: [PATCH] :sparkles: Implement the GroupHelperService --- logic/grouphelperservice.go | 103 ++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 logic/grouphelperservice.go diff --git a/logic/grouphelperservice.go b/logic/grouphelperservice.go new file mode 100644 index 0000000..46fb9ee --- /dev/null +++ b/logic/grouphelperservice.go @@ -0,0 +1,103 @@ +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.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) { + return g.groupHelperRepo.GetUserByID(userID) +} + +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() +}