diff --git a/pkg/models/priority.go b/pkg/models/priority.go index 86af5a4..1980790 100644 --- a/pkg/models/priority.go +++ b/pkg/models/priority.go @@ -5,3 +5,30 @@ const ( PriorityImportant string = "IMPORTANT" PriorityInformational string = "INFORMATIONAL" ) + +var priorityToInt = map[string]int{ + PriorityInformational: 0, + PriorityImportant: 1, + PriorityUrgent: 2, +} + +func ComparePriority(priority1, priority2 string) int { + val1, ok1 := priorityToInt[priority1] + val2, ok2 := priorityToInt[priority2] + + if !ok1 && !ok2 { + return 0 + } else if !ok1 { + return -1 + } else if !ok2 { + return 1 + } + + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } else { + return 0 + } +} diff --git a/pkg/models/priority_test.go b/pkg/models/priority_test.go new file mode 100644 index 0000000..ef48c78 --- /dev/null +++ b/pkg/models/priority_test.go @@ -0,0 +1,32 @@ +package models + +import "testing" + +func TestComparePriority(t *testing.T) { + tests := []struct { + priority1 string + priority2 string + expected int + }{ + {"INVALID", "URGENT", -1}, + {"UNKNOWN", "IMPORTANT", -1}, + {"INFORMATIONAL", "URGENT", -1}, + {"IMPORTANT", "URGENT", -1}, + {"URGENT", "IMPORTANT", 1}, + {"IMPORTANT", "INFORMATIONAL", 1}, + {"URGENT", "UNKNOWN", 1}, + {"IMPORTANT", "INVALID", 1}, + {"UNKNOWN", "UNKNOWN", 0}, + {"INFORMATIONAL", "INFORMATIONAL", 0}, + {"IMPORTANT", "IMPORTANT", 0}, + {"URGENT", "URGENT", 0}, + } + + for _, test := range tests { + result := ComparePriority(test.priority1, test.priority2) + if result != test.expected { + t.Errorf("ComparePriority(%s, %s) = %d; want %d", + test.priority1, test.priority2, result, test.expected) + } + } +}