-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_database.go
190 lines (158 loc) · 3.79 KB
/
mock_database.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
package database
import (
"fmt"
"sync"
"time"
"github.com/MSSkowron/BookRESTAPI/internal/models"
)
// MockDatabase is a mock implementation of Database interface.
type MockDatabase struct {
userMu sync.RWMutex
bookMu sync.RWMutex
users []*models.User
books []*models.Book
}
// NewMockDatabase creates a new MockDatabase.
func NewMockDatabase() Database {
return &MockDatabase{
users: []*models.User{
{
ID: 1,
CreatedAt: time.Now(),
Email: "johndoe@net.eu",
Password: "johnpassword",
FirstName: "John",
LastName: "Doe",
Age: 30,
},
{
ID: 2,
CreatedAt: time.Now(),
Email: "janedoe@net.eu",
Password: "janepassword",
FirstName: "Jane",
LastName: "Doe",
Age: 25,
},
{
ID: 3,
CreatedAt: time.Now(),
Email: "jankowalski@net.pl",
Password: "janpassword",
FirstName: "Jan",
LastName: "Kowalski",
Age: 30,
},
},
books: []*models.Book{
{
ID: 1,
CreatedBy: 1,
CreatedAt: time.Now(),
Author: "J.R.R. Tolkien",
Title: "The Lord of the Rings",
},
{
ID: 2,
CreatedBy: 2,
CreatedAt: time.Now(),
Author: "J.K. Rowling",
Title: "Harry Potter",
},
{
ID: 3,
CreatedBy: 3,
CreatedAt: time.Now(),
Author: "Stephen King",
Title: "The Shining",
},
},
}
}
// Close closes the database connection.
func (db *MockDatabase) Close() {}
// InsertUser inserts a new user into the database.
func (db *MockDatabase) InsertUser(user *models.User) (int, error) {
db.userMu.Lock()
defer db.userMu.Unlock()
user.ID = len(db.users) + 1
for _, u := range db.users {
if u.Email == user.Email {
return -1, fmt.Errorf("user with email %s already exists", user.Email)
}
}
db.users = append(db.users, user)
return len(db.users), nil
}
// SelectUserByID selects a user with given ID from the database.
func (db *MockDatabase) SelectUserByID(id int) (*models.User, error) {
db.userMu.RLock()
defer db.userMu.RUnlock()
for _, user := range db.users {
if user.ID == id {
return user, nil
}
}
return nil, nil
}
// SelectUserByEmail selects a user with given email from the database.
func (db *MockDatabase) SelectUserByEmail(email string) (*models.User, error) {
db.userMu.RLock()
defer db.userMu.RUnlock()
for _, user := range db.users {
if user.Email == email {
return user, nil
}
}
return nil, nil
}
// InsertBook inserts a new book into the database.
func (db *MockDatabase) InsertBook(book *models.Book) (int, error) {
db.bookMu.Lock()
defer db.bookMu.Unlock()
book.ID = len(db.books) + 1
db.books = append(db.books, book)
return len(db.books), nil
}
// SelectBookByID selects a book with given ID from the database.
func (db *MockDatabase) SelectBookByID(id int) (*models.Book, error) {
db.bookMu.RLock()
defer db.bookMu.RUnlock()
for _, book := range db.books {
if book.ID == id {
return book, nil
}
}
return nil, nil
}
// SelectAllBooks selects all books from the database.
func (db *MockDatabase) SelectAllBooks() ([]*models.Book, error) {
db.bookMu.RLock()
defer db.bookMu.RUnlock()
return db.books, nil
}
// DeleteBook deletes a book with given ID from the database.
func (db *MockDatabase) DeleteBook(id int) error {
db.bookMu.Lock()
defer db.bookMu.Unlock()
for i, book := range db.books {
if book.ID == id {
db.books = append(db.books[:i], db.books[i+1:]...)
return nil
}
}
return nil
}
// UpdateBook updates a book with given ID in the database.
func (db *MockDatabase) UpdateBook(id int, book *models.Book) error {
db.bookMu.Lock()
defer db.bookMu.Unlock()
for i, b := range db.books {
if b.ID == id {
db.books[i].Author = book.Author
db.books[i].Title = book.Title
return nil
}
}
return nil
}