This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand-unit.go
79 lines (65 loc) · 1.89 KB
/
command-unit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
)
const (
UNIT_WATCHING_CLUB_NOT_EXIST = "You are not watching any club. Use CLUB to watch a club"
UNIT_CLUB_UNIT = "Unit for the club %s: %s"
UNIT_UNKNOWN_UNIT = "Unknown unit"
UNIT_CLUB_UNIT_IS_SET = "UNIT is set to %s"
)
type UnitCommand struct {
}
func (cmd *UnitCommand) Name() string {
return "unit"
}
func (cmd *UnitCommand) Execute(params []string, message *IncomingSlackMessage, executor *CommandExecutor) (string, error) {
if len(params) > 2 {
return COMMAND_TOO_MANY_PARAMETERS, nil
}
jobDetails, err := executor.repo.JobDetails.Get(message.TeamId, message.ChannelId)
if err != nil {
return "", err
}
if jobDetails == nil {
return UNIT_WATCHING_CLUB_NOT_EXIST, nil
}
clubId := jobDetails.ClubId
if len(params) == 0 {
clubs, err := executor.repo.ClubDetails.List()
if err != nil {
return "", err
}
clubUnits := make(map[string]string)
for _, club := range clubs {
clubUnits[club.ClubId] = club.Unit
}
var clubUnit string
var ok bool
if clubUnit, ok = clubUnits[clubId]; !ok {
clubUnit = DefaultUnit.Name
}
return fmt.Sprintf(UNIT_CLUB_UNIT, clubId, clubUnit), nil
}
unit := GetKnownUnit(params[0])
if unit == nil {
return UNIT_UNKNOWN_UNIT, nil
}
clubDetails, err := executor.repo.ClubDetails.Get(clubId)
if err != nil {
return "", err
}
if clubDetails == nil {
clubDetails = &ClubDetails{ClubId: clubId, Unit: unit.Name}
err := executor.repo.ClubDetails.Create(clubDetails)
if err != nil {
return "", fmt.Errorf("Can't create club details in the database: %v\n", err)
}
} else if clubDetails.Unit != unit.Name {
err := executor.repo.ClubDetails.Update(clubDetails, map[string]interface{}{"Unit": unit.Name})
if err != nil {
return "", fmt.Errorf("Can't save club details to the database: %v\n", err)
}
}
return fmt.Sprintf(UNIT_CLUB_UNIT_IS_SET, unit.Name), nil
}