/
profile.go
174 lines (147 loc) · 5.28 KB
/
profile.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package service
import (
"net/http"
"../../utils"
"../repository"
"gorm.io/gorm"
)
type ProfileService struct {
accountRepo *repository.AccountRepository
testRepo *repository.TestRepository
versionRepo *repository.VersionRepository
descriptionRepo *repository.DescriptionRepository
}
func NewProfileService(accountRepo *repository.AccountRepository,
testRepo *repository.TestRepository,
versionRepo *repository.VersionRepository,
descriptionRepo *repository.DescriptionRepository) *ProfileService {
return &ProfileService{accountRepo, testRepo, versionRepo, descriptionRepo}
}
func (profile *ProfileService) Find(login string) map[string]interface{} {
account, err := profile.accountRepo.FindByLogin(login)
if err != nil {
return utils.Message(http.StatusInternalServerError, "Failure occured while finding account!")
}
account.Password = ""
response := utils.Message(http.StatusOK, "Account found!")
response["account"] = account
return response
}
func (profile *ProfileService) UpdateIcon(login, icon string) map[string]interface{} {
err := profile.accountRepo.UpdateIcon(login, icon)
if err != nil {
return utils.Message(http.StatusInternalServerError, "Failure occured while updating icon!")
}
return utils.Message(http.StatusOK, "Icon updated!")
}
func (profile *ProfileService) UpdateRole(login, role string) map[string]interface{} {
author, err := profile.accountRepo.FindByLogin(login)
if err != nil {
return utils.Message(http.StatusInternalServerError, "Failure occured while updating role!")
}
if author.Role != "admin" {
return utils.Message(http.StatusForbidden, "Role update is forbidden!")
}
err = profile.accountRepo.UpdateRole(login, role)
if err != nil {
return utils.Message(http.StatusInternalServerError, "Failure occured while updating role!")
}
return utils.Message(http.StatusOK, "Role updated!")
}
func (profile *ProfileService) ListVersions(login string) map[string]interface{} {
author, err := profile.accountRepo.FindByLogin(login)
switch err {
case nil:
break
case gorm.ErrRecordNotFound:
return utils.Message(http.StatusNotFound, "Author not found")
default:
return utils.Message(http.StatusInternalServerError, "Error occured while listing versions!")
}
versions, err := profile.versionRepo.ListByAuthor(author.ID)
switch err {
case nil:
break
case gorm.ErrEmptySlice:
return utils.Message(http.StatusNotFound, "No versions found!")
default:
return utils.Message(http.StatusInternalServerError, "Error occured while listing versions!")
}
response := utils.Message(http.StatusOK, "Versions listed!")
response["versions"] = versions
return response
}
func (profile *ProfileService) ListTests(login string) map[string]interface{} {
author, err := profile.accountRepo.FindByLogin(login)
switch err {
case nil:
break
case gorm.ErrRecordNotFound:
return utils.Message(http.StatusNotFound, "Author not found")
default:
return utils.Message(http.StatusInternalServerError, "Error occured while listing tests!")
}
versions, err := profile.testRepo.ListByAuthor(author.ID)
switch err {
case nil:
break
case gorm.ErrEmptySlice:
return utils.Message(http.StatusNotFound, "No versions found!")
default:
return utils.Message(http.StatusInternalServerError, "Error occured while listing tests!")
}
response := utils.Message(http.StatusOK, "Tests listed!")
response["tests"] = versions
return response
}
func (profile *ProfileService) ListDescriptions(login string) map[string]interface{} {
author, err := profile.accountRepo.FindByLogin(login)
switch err {
case nil:
break
case gorm.ErrRecordNotFound:
return utils.Message(http.StatusNotFound, "Author not found")
default:
return utils.Message(http.StatusInternalServerError, "Error occured while listing descriptions!")
}
descriptions, err := profile.descriptionRepo.ListByAuthor(author.ID)
switch err {
case nil:
break
case gorm.ErrEmptySlice:
return utils.Message(http.StatusNotFound, "No descriptions found!")
default:
return utils.Message(http.StatusInternalServerError, "Error occured while listing descriptions!")
}
response := utils.Message(http.StatusOK, "Descriptions listed!")
response["descriptions"] = descriptions
return response
}
func (profile *ProfileService) CountLevel(login string) map[string]interface{} {
author, err := profile.accountRepo.FindByLogin(login)
switch err {
case nil:
break
case gorm.ErrRecordNotFound:
return utils.Message(http.StatusNotFound, "Author not found")
default:
return utils.Message(http.StatusInternalServerError, "Error occured while listing descriptions!")
}
versions, err := profile.versionRepo.ListByAuthor(author.ID)
if err != nil {
return utils.Message(http.StatusInternalServerError, "Error occured while counting level!")
}
tests, err := profile.testRepo.ListByAuthor(author.ID)
if err != nil {
return utils.Message(http.StatusInternalServerError, "Error occured while counting level!")
}
descriptions, err := profile.descriptionRepo.ListByAuthor(author.ID)
if err != nil {
return utils.Message(http.StatusInternalServerError, "Error occured while counting level!")
}
totalPoins := 5*len(versions) + 2*len(tests) + len(descriptions)
response := utils.Message(http.StatusOK, "Level counted!")
response["level"] = totalPoins / 10
response["tenth"] = totalPoins % 10
return response
}