-
Notifications
You must be signed in to change notification settings - Fork 0
/
employee.go
256 lines (231 loc) · 7.32 KB
/
employee.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"log"
"net/http"
)
type Employee struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
DOB string `json:"dob"`
Email string `json:"email"`
VisaStatus string `json:"visaStatus"`
VisaType string `json:"visaType"`
Status string `json:"status"`
}
type EmployeeFullDetail struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
DOB string `json:"dob"`
Email string `json:"email"`
Status string `json:"status"`
VisaStatus string `json:"visaStatus"`
VisaType string `json:"visaType"`
VisaExpiryDate string `json:"visaExpiryDate"`
Client1Name string `json:"client1Name"`
Project1 string `json:"project1"`
StartDate1 string `json:"startDate1"`
HrsPerMonth1 float64 `json:"hrsPerMonth1"`
Location1 string `json:"location1"`
Lca1 float64 `json:"lca1"`
ClientBillingRate1 float64 `json:"clientBillingRate1"`
Vms1 float64 `json:"vms1"`
Dor1 float64 `json:"dor1"`
Load1 float64 `json:"load1"`
EffectiveBillingRateHumetis1 float64 `json:"effectiveBillingRateHumetis1"`
PayConfirmed1 float64 `json:"payConfirmed1"`
Client2Name string `json:"client2Name"`
Project2 string `json:"project2"`
StartDate2 string `json:"startDate2"`
HrsPerMonth2 float64 `json:"hrsPerMonth2"`
Location2 string `json:"location2"`
Lca2 float64 `json:"lca2"`
ClientBillingRate2 float64 `json:"clientBillingRate2"`
Vms2 float64 `json:"vms2"`
Dor2 float64 `json:"dor2"`
Load2 float64 `json:"load2"`
EffectiveBillingRateHumetis2 float64 `json:"effectiveBillingRateHumetis2"`
PayConfirmed2 float64 `json:"payConfirmed2"`
YearlyEstimatedBudget float64 `json:"yearlyEstimatedBudget"`
GroupDiscountedMedicalRequired bool `json:"groupDiscountedMedicalRequired"`
GcPw float64 `json:"gcPw"`
LcaConsidered float64 `json:"lcaConsidered"`
ExpBonusPay float64 `json:"expBonusPay"`
ExpBonusBudget float64 `json:"expBonusBudget"`
ExpBonusLca float64 `json:"expBonusLca"`
ChooseYourOwnOffer float64 `json:"chooseYourOwnOffer"`
Exception bool `json:"exception"`
DateOfJoiningHumetis string `json:"dateOfJoiningHumetis"`
Flexible bool `json:"flexible"`
}
type RequestEmployee struct {
Email string `json:"email"`
}
type EmployeeChangeStatus struct {
Email string `json:"email"`
Status string `json:"status"`
}
//Handler /api/employees
func handleGetEmployees(c *gin.Context) {
var loadedTasks, err = GetAllEmployee()
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"msg": err})
return
}
c.JSON(http.StatusOK, gin.H{"employees": loadedTasks})
}
func handleGetEmployee(c *gin.Context) {
var u RequestEmployee
if err := c.ShouldBindJSON(&u); err != nil {
c.JSON(http.StatusUnprocessableEntity, "Invalid json provided")
return
}
var employee, err = GetEmployee(u.Email)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"msg": err})
return
}
c.JSON(http.StatusOK, employee)
}
func handleUpdateEmployee(c *gin.Context) {
var u EmployeeFullDetail
if err := c.ShouldBindJSON(&u); err != nil {
c.JSON(http.StatusUnprocessableEntity, "Invalid json provided")
return
}
var err = UpdateEmployee(&u)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"msg": err})
return
}
c.JSON(http.StatusOK, gin.H{"msg": "updated"})
}
func handleAddEmployee(c *gin.Context) {
var u EmployeeFullDetail
if err := c.ShouldBindJSON(&u); err != nil {
c.JSON(http.StatusUnprocessableEntity, "Invalid json provided")
return
}
var err = AddEmployee(&u)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"msg": err})
return
}
c.JSON(http.StatusOK, gin.H{"msg": "Added"})
}
func handleChangeEmployeeStatus(c *gin.Context) {
var u EmployeeChangeStatus
if err := c.ShouldBindJSON(&u); err != nil {
c.JSON(http.StatusUnprocessableEntity, "Invalid json provided")
return
}
emp, err := ChangeEmployeeStatus(&u)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"msg": err})
return
}
c.JSON(http.StatusOK, emp)
}
// GetAllTasks Retrieves all Employees from the db
func GetAllEmployee() ([]*Employee, error) {
var employees []*Employee
client, ctx, cancel := getConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database("humetis")
collection := db.Collection("employees")
cursor, err := collection.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
err = cursor.All(ctx, &employees)
if err != nil {
log.Printf("Failed marshalling %v", err)
return nil, err
}
return employees, nil
}
func GetEmployee(email string) (*EmployeeFullDetail, error) {
var employee *EmployeeFullDetail
fmt.Print(email)
client, ctx, cancel := getConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database("humetis")
collection := db.Collection("employees")
result := collection.FindOne(ctx, bson.M{"email": email})
if result == nil {
return nil, errors.New("Could not find a user")
}
err := result.Decode(&employee)
if err != nil {
log.Printf("Failed marshalling %v", err)
return nil, err
}
log.Printf("Users: %v", employee)
return employee, nil
}
func DeleteEmployee(employee *EmployeeFullDetail) error {
client, ctx, cancel := getConnection()
defer cancel()
defer client.Disconnect(ctx)
_, err := client.Database("humetis").Collection("employees").DeleteOne(ctx, bson.M{"email": employee.Email})
if err != nil {
log.Printf("Could not delete employee: %v", err)
return err
}
return nil
}
func InsertEmployee(employee *EmployeeFullDetail) error {
client, ctx, cancel := getConnection()
defer cancel()
defer client.Disconnect(ctx)
_, err := client.Database("humetis").Collection("employees").InsertOne(ctx, employee)
if err != nil {
log.Printf("Could not insert employee: %v", err)
return err
}
return nil
}
func UpdateEmployee(employee *EmployeeFullDetail) error {
err := DeleteEmployee(employee)
if err != nil {
return err
}
err = InsertEmployee(employee)
if err != nil {
return err
}
return nil
}
func AddEmployee(employee *EmployeeFullDetail) error {
_, err := GetEmployee(employee.Email)
if err == nil {
return errors.New("employee: already exist")
}
err = InsertEmployee(employee)
if err != nil {
return err
}
return nil
}
func ChangeEmployeeStatus(employee *EmployeeChangeStatus) (*EmployeeFullDetail, error) {
emp, err := GetEmployee(employee.Email)
if err != nil {
return nil, errors.New("employee: Not exist")
}
err = DeleteEmployee(emp)
if err != nil {
return nil, err
}
emp.Status = employee.Status
err = InsertEmployee(emp)
if err != nil {
return nil, err
}
return emp, nil
}