forked from bootdotdev/learn-cicd-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
74 lines (64 loc) · 1.56 KB
/
models.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
package main
import (
"time"
"github.com/bootdotdev/learn-cicd-starter/internal/database"
)
type User struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
ApiKey string `json:"api_key"`
}
func databaseUserToUser(user database.User) (User, error) {
createdAt, err := time.Parse(time.RFC3339, user.CreatedAt)
if err != nil {
return User{}, err
}
updatedAt, err := time.Parse(time.RFC3339, user.UpdatedAt)
if err != nil {
return User{}, err
}
return User{
ID: user.ID,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
Name: user.Name,
ApiKey: user.ApiKey,
}, nil
}
type Note struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Note string `json:"note"`
UserID string `json:"user_id"`
}
func databaseNoteToNote(post database.Note) (Note, error) {
createdAt, err := time.Parse(time.RFC3339, post.CreatedAt)
if err != nil {
return Note{}, err
}
updatedAt, err := time.Parse(time.RFC3339, post.UpdatedAt)
if err != nil {
return Note{}, err
}
return Note{
ID: post.ID,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
Note: post.Note,
UserID: post.UserID,
}, nil
}
func databasePostsToPosts(notes []database.Note) ([]Note, error) {
result := make([]Note, len(notes))
for i, note := range notes {
var err error
result[i], err = databaseNoteToNote(note)
if err != nil {
return nil, err
}
}
return result, nil
}