-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
99 lines (84 loc) · 2.38 KB
/
repository.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
package data
import (
"errors"
"fmt"
"time"
_ "time/tzdata"
"buf.build/gen/go/khafidprayoga/student-svc/protocolbuffers/go/student/v2"
"github.com/google/uuid"
"github.com/khafidprayoga/student-svc/common/model"
)
var studentData = make(map[string]model.Student)
type StudentDataRepository struct{}
func New() *StudentDataRepository {
return &StudentDataRepository{}
}
func init() {
now := time.Now()
WIB, err := time.LoadLocation("Asia/Jakarta")
if err != nil {
fmt.Printf("failed to init seed data %v", err)
}
dob := time.Date(1996, time.June, 9, 0, 0, 0, 0, time.UTC)
dob = dob.In(WIB)
// Seed initial data
student := model.Student{
Id: "seed",
FullName: "Jane Doe",
Address: "Jalan Jend. Sudirman No. 321",
Hobbies: []string{"listening to the music", "singing"},
BirthDate: &dob,
Gender: studentv2.GenderType_value[studentv2.GenderType_GENDER_TYPE_FEMALE.String()],
Email: "janedoe@example.com",
Nationality: studentv2.StudentNationality_value[studentv2.StudentNationality_STUDENT_NATIONALITY_FOREIGN.String()],
CreatedAt: &now,
UpdatedAt: &now,
}
studentData[student.Id] = student
}
func (data *StudentDataRepository) CreateStudent(s *model.Student) (id string, err error) {
now := time.Now()
preparedData := model.Student{
Id: uuid.NewString(),
FullName: s.FullName,
Address: s.Address,
Hobbies: s.Hobbies,
BirthDate: s.BirthDate,
Gender: s.Gender,
Email: s.Email,
Nationality: s.Nationality,
CreatedAt: &now,
UpdatedAt: &now,
}
studentData[preparedData.Id] = preparedData
if studentData[preparedData.Id].Id == "" {
return "", fmt.Errorf("failed to insert student %v ", s.FullName)
}
return preparedData.Id, nil
}
func (data *StudentDataRepository) GetDetailStudent(
studentId string,
) (*model.Student, error) {
details := studentData[studentId]
if details.Id == "" {
return nil, errors.New("student not found")
}
return &details, nil
}
func (data *StudentDataRepository) DeleteStudent(studentId string) error {
delete(studentData, studentId)
if studentData[studentId].Id != "" {
return fmt.Errorf(
"failed to delete student wit id %v", studentId,
)
}
return nil
}
func (data *StudentDataRepository) GetListStudent() (
listStudent []model.Student,
) {
for _, student := range studentData {
listStudent = append(listStudent, student)
}
return
}