-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.go
103 lines (86 loc) 路 2.11 KB
/
convert.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package convert
import (
"fmt"
"time"
"github.com/franpog859/cleanaux-backend/content-service/internal/model"
)
// ContentFromItems converts slice of database items
// to user content provided in the handler
func ContentFromItems(items []model.Item) ([]model.ContentResponse, error) {
content := []model.ContentResponse{}
for _, item := range items {
status, err := getStatus(item)
if err != nil {
return content, err
}
c := model.ContentResponse{
ID: item.ID,
Name: item.Name,
Status: status,
}
content = append(content, c)
}
return content, nil
}
func getStatus(item model.Item) (int, error) {
pastDays, err := countDays(item.LastUsageDate)
if err != nil {
return 0, err
}
status, err := calculateStatus(item.IntervalDays, pastDays)
if err != nil {
return 0, err
}
return status, nil
}
func countDays(lastUsageDate string) (int, error) {
lastUsage, err := time.Parse(model.DateLayout, lastUsageDate)
if err != nil {
return 0, fmt.Errorf("failed to parse date: %v", err)
}
pastDays := int(time.Now().Sub(lastUsage).Hours() / 24)
return pastDays, nil
}
func calculateStatus(intervalDays, pastDays int) (int, error) {
if intervalDays < 1 || pastDays < 0 {
return 0, fmt.Errorf(
"error while calculating status from intervalDays: %d and pastDays: %d",
intervalDays, pastDays,
)
}
var border int
if intervalDays < 40 {
border = intervalDays / 20
} else {
border = 2
}
if border >= intervalDays-pastDays {
return 3, nil
}
if intervalDays < 20 {
border = intervalDays / 4
} else {
border = 5
}
if border >= intervalDays-pastDays {
return 2, nil
}
if intervalDays < 20 {
border = intervalDays / 2
} else {
border = 10
}
if border >= intervalDays-pastDays {
return 1, nil
}
return 0, nil
}
// UpdateItemFromContentRequest creates database input item from information
// provided int PUT request
func UpdateItemFromContentRequest(contentRequestBody model.ContentRequest) model.UpdateItem {
lastUsageDate := time.Now().Format(model.DateLayout)
return model.UpdateItem{
ID: contentRequestBody.ID,
LastUsageDate: lastUsageDate,
}
}