-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover.html
673 lines (534 loc) · 21.2 KB
/
cover.html
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>database: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">app/database/todo.repository.go (50.0%)</option>
<option value="file1">app/database/user.repository.go (86.7%)</option>
<option value="file2">app/service/cypto_service.go (0.0%)</option>
<option value="file3">app/service/mail_service.go (0.0%)</option>
<option value="file4">app/service/session_service.go (0.0%)</option>
<option value="file5">app/usecase/auth.usecase.go (45.9%)</option>
<option value="file6">app/usecase/todo.usecase.go (0.0%)</option>
<option value="file7">app/usecase/user.usecase.go (0.0%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package database
import (
"app/config"
"app/entity"
)
type TodoRepository interface {
FindAll(userId uint64) (todos []*entity.Todo, err error)
Create(t *entity.Todo) (todo *entity.Todo, err error)
Find(todoId int) (todo *entity.Todo, err error)
Update(todoId int, t *entity.Todo) (todo *entity.Todo, err error)
Delete(t *entity.Todo) error
}
type todoRepository struct {
DB *config.DB
}
func NewTodoRepository(DB *config.DB) TodoRepository <span class="cov8" title="1">{
return &todoRepository{DB: DB}
}</span>
func (todoRep *todoRepository) FindAll(userId uint64) (todos []*entity.Todo, err error) <span class="cov8" title="1">{
err = todoRep.DB.Model(&entity.Todo{}).
Where("user_id = ?", userId).
Find(&todos).
Error
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">return</span>
}
func (todoRep *todoRepository) Create(t *entity.Todo) (todo *entity.Todo, err error) <span class="cov8" title="1">{
err = todoRep.DB.Create(&t).Error
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">err = todoRep.DB.Find(&todo, &t.ID).Error
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">return</span>
}
func (todoRep *todoRepository) Find(todoId int) (todo *entity.Todo, err error) <span class="cov8" title="1">{
err = todoRep.DB.First(&todo, todoId).Error
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">return</span>
}
func (todoRep *todoRepository) Update(todoId int, t *entity.Todo) (todo *entity.Todo, err error) <span class="cov0" title="0">{
err = todoRep.DB.Updates(&t).Error
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">err = todoRep.DB.Find(&todo, &t.ID).Error
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">return</span>
}
func (todoRep *todoRepository) Delete(t *entity.Todo) error <span class="cov0" title="0">{
if err := todoRep.DB.
Delete(&t).
Error; err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">return nil</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package database
import (
"app/config"
"app/entity"
)
type UserRepository interface {
FindAll() (uesrs []*entity.User, err error)
Find(userId uint64) (user *entity.User, err error)
FindByEmail(email string) (user *entity.User, err error)
Create(u *entity.User) (user *entity.User, err error)
Update(u *entity.User) (user *entity.User, err error)
Delete(userId uint64) error
}
type userRepository struct {
DB *config.DB
}
func NewUserRepository(DB *config.DB) UserRepository <span class="cov8" title="1">{
return &userRepository{DB: DB}
}</span>
func (userRep *userRepository) FindAll() (users []*entity.User, err error) <span class="cov8" title="1">{
err = userRep.DB.Model(&entity.User{}).
Find(&users).
Error
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">return</span>
}
func (userRep *userRepository) Find(userId uint64) (user *entity.User, err error) <span class="cov8" title="1">{
err = userRep.DB.First(&user, userId).Error
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">return</span>
}
func (userRep *userRepository) FindByEmail(email string) (user *entity.User, err error) <span class="cov8" title="1">{
err = userRep.DB.First(&user, "email = ?", email).Error
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">return</span>
}
func (userRep *userRepository) Create(u *entity.User) (user *entity.User, err error) <span class="cov8" title="1">{
err = userRep.DB.Create(&u).Error
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">err = userRep.DB.Find(&user, &u.ID).Error
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">return user, nil</span>
}
func (userRep *userRepository) Update(u *entity.User) (user *entity.User, err error) <span class="cov8" title="1">{
err = userRep.DB.Updates(&u).Error
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">err = userRep.DB.First(&user, &u.ID).Error
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">return</span>
}
func (userRep *userRepository) Delete(userId uint64) error <span class="cov8" title="1">{
if err := userRep.DB.Delete(&entity.User{}, userId).Error; err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov8" title="1">return nil</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package service
import "golang.org/x/crypto/bcrypt"
type CyptoService interface {
HashAndSalt(pwd []byte) (string, error)
ComparePasswords(hashedPwd string, plainPwd []byte) bool
}
type cyptoService struct{}
func NewCyptoService() CyptoService <span class="cov0" title="0">{
return &cyptoService{}
}</span>
func (c *cyptoService) HashAndSalt(pwd []byte) (string, error) <span class="cov0" title="0">{
hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.DefaultCost)
if err != nil </span><span class="cov0" title="0">{
return "", err
}</span>
<span class="cov0" title="0">return string(hash), nil</span>
}
func (c *cyptoService) ComparePasswords(hashedPwd string, plainPwd []byte) bool <span class="cov0" title="0">{
byteHash := []byte(hashedPwd)
err := bcrypt.CompareHashAndPassword(byteHash, plainPwd)
if err != nil </span><span class="cov0" title="0">{
return false
}</span>
<span class="cov0" title="0">return true</span>
}
</pre>
<pre class="file" id="file3" style="display: none">package service
import (
"app/config"
)
type MailService interface {
SendMail(to, subject, body string) error
}
type mailService struct {
Mail *config.Mail
}
func NewMailService(Mail *config.Mail) MailService <span class="cov0" title="0">{
return &mailService{
Mail: Mail,
}
}</span>
func (m *mailService) SendMail(to, subject, body string) error <span class="cov0" title="0">{
if err := m.Mail.Send(to, subject, body); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">return nil</span>
}
</pre>
<pre class="file" id="file4" style="display: none">package service
import (
"app/config"
"context"
"net/http"
"github.com/gorilla/sessions"
"github.com/wader/gormstore/v2"
)
type SessionService interface {
GetSessionValue(ctx context.Context, key string) (interface{}, error)
SaveSession(ctx context.Context, key string, value interface{}) error
DeleteSession(ctx context.Context) error
}
type sessionService struct {
store *gormstore.Store
}
func NewSessionService(s *gormstore.Store) SessionService <span class="cov0" title="0">{
return &sessionService{
store: s,
}
}</span>
// HTTPKey is the key used to extract the Http struct.
type HTTPKey string
// HTTP is the struct used to inject the response writer and request http structs.
type HTTP struct {
W *http.ResponseWriter
R *http.Request
}
// GetSession returns a cached session of the given name
func (service *sessionService) GetSession(ctx context.Context, name string) (*sessions.Session, error) <span class="cov0" title="0">{
httpContext := ctx.Value(HTTPKey("http")).(HTTP)
// Ignore err because a session is always returned even if one doesn't exist
session, err := service.store.Get(httpContext.R, name)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">return session, nil</span>
}
func (service *sessionService) GetSessionValue(ctx context.Context, key string) (interface{}, error) <span class="cov0" title="0">{
session, err := service.GetSession(ctx, config.SESSION_KEY_NAME)
if err != nil </span><span class="cov0" title="0">{
return "", err
}</span>
<span class="cov0" title="0">return session.Values[key], nil</span>
}
func (service *sessionService) SaveSession(ctx context.Context, key string, value interface{}) error <span class="cov0" title="0">{
session, err := service.GetSession(ctx, config.SESSION_KEY_NAME)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">session.Values[key] = value
httpContext := ctx.Value(HTTPKey("http")).(HTTP)
err = service.store.Save(httpContext.R, *httpContext.W, session)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">return nil</span>
}
func (service *sessionService) DeleteSession(ctx context.Context) error <span class="cov0" title="0">{
session, err := service.GetSession(ctx, config.SESSION_KEY_NAME)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">session.Options.MaxAge = -1
httpContext := ctx.Value(HTTPKey("http")).(HTTP)
err = service.store.Save(httpContext.R, *httpContext.W, session)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">return nil</span>
}
</pre>
<pre class="file" id="file5" style="display: none">package usecase
import (
"app/controller/dto"
"app/database"
"app/entity"
"app/service"
"context"
"errors"
)
type AuthUsecase interface {
SignIn(ctx context.Context, signInParams *dto.AuthSignInRequestDto) error
SignUp(ctx context.Context, signInParams *dto.AuthSignUpRequestDto) error
Show(ctx context.Context) (user *entity.User, err error)
Edit(ctx context.Context, userUpdateParams *dto.AuthUserUpdateRequestDto) (user *entity.User, err error)
Delete(ctx context.Context) error
}
type authUsecase struct {
userRepo database.UserRepository
sessionS service.SessionService
cyptoS service.CyptoService
mailS service.MailService
}
func NewAuthUsecase(userRepo database.UserRepository, sessionS service.SessionService, cyptoS service.CyptoService, mailS service.MailService) AuthUsecase <span class="cov8" title="1">{
return &authUsecase{
userRepo: userRepo,
sessionS: sessionS,
cyptoS: cyptoS,
mailS: mailS,
}
}</span>
func (uu *authUsecase) SignIn(ctx context.Context, signInParams *dto.AuthSignInRequestDto) error <span class="cov8" title="1">{
loginUser, err := uu.userRepo.FindByEmail(signInParams.Email)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov8" title="1">if !uu.cyptoS.ComparePasswords(loginUser.HashPassword, []byte(signInParams.Password)) </span><span class="cov8" title="1">{
return errors.New("Authentication Failure")
}</span>
<span class="cov8" title="1">err = uu.sessionS.SaveSession(ctx, "userId", loginUser.ID)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov8" title="1">return nil</span>
}
func (uu *authUsecase) SignUp(ctx context.Context, signInParams *dto.AuthSignUpRequestDto) error <span class="cov8" title="1">{
hashPwd, err := uu.cyptoS.HashAndSalt([]byte(signInParams.Password))
if err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">u := entity.User{Name: signInParams.Name, Email: signInParams.Email, HashPassword: hashPwd}
loginUser, err := uu.userRepo.Create(&u)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov8" title="1">err = uu.sessionS.SaveSession(ctx, "userId", loginUser.ID)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov8" title="1">return nil</span>
}
func (uu *authUsecase) Show(ctx context.Context) (user *entity.User, err error) <span class="cov0" title="0">{
userId, _ := uu.sessionS.GetSessionValue(ctx, "userId")
user, err = uu.userRepo.Find(userId.(uint64))
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">return</span>
}
func (uu *authUsecase) Edit(ctx context.Context, userUpdateParams *dto.AuthUserUpdateRequestDto) (user *entity.User, err error) <span class="cov0" title="0">{
userId, _ := uu.sessionS.GetSessionValue(ctx, "userId")
u := &entity.User{
ID: userId.(uint64),
Name: userUpdateParams.Name,
Email: userUpdateParams.Email,
}
user, err = uu.userRepo.Update(u)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">return</span>
}
func (uu *authUsecase) Delete(ctx context.Context) error <span class="cov0" title="0">{
userId, _ := uu.sessionS.GetSessionValue(ctx, "userId")
if err := uu.userRepo.Delete(userId.(uint64)); err != nil </span><span class="cov0" title="0">{
return nil
}</span>
<span class="cov0" title="0">uu.sessionS.DeleteSession(ctx)
return nil</span>
}
</pre>
<pre class="file" id="file6" style="display: none">package usecase
import (
"app/controller/dto"
"app/database"
"app/entity"
"app/service"
"context"
"errors"
)
type TodoUsecase interface {
FindAll(ctx context.Context) (todos []*entity.Todo, err error)
Create(ctx context.Context, createParams *dto.TodoCreateRequestDto) (todos *entity.Todo, err error)
Show(ctx context.Context, todoId int) (todo *entity.Todo, err error)
Edit(todoId int, updateParams *dto.TodoUpdateRequestDto) (todo *entity.Todo, err error)
Delete(ctx context.Context, todoId int) error
}
type todoUsecase struct {
todoRepo database.TodoRepository
sessionS service.SessionService
}
func NewTodoUsecase(todoRepo database.TodoRepository, sessionS service.SessionService) TodoUsecase <span class="cov0" title="0">{
return &todoUsecase{
todoRepo: todoRepo,
sessionS: sessionS,
}
}</span>
func (tu *todoUsecase) FindAll(ctx context.Context) (todos []*entity.Todo, err error) <span class="cov0" title="0">{
userId, _ := tu.sessionS.GetSessionValue(ctx, "userId")
todos, err = tu.todoRepo.FindAll(userId.(uint64))
return
}</span>
func (tu *todoUsecase) Create(ctx context.Context, createParams *dto.TodoCreateRequestDto) (todo *entity.Todo, err error) <span class="cov0" title="0">{
userId, _ := tu.sessionS.GetSessionValue(ctx, "userId")
t := entity.Todo{Title: createParams.Title, Description: createParams.Description, UserId: userId.(uint64)}
todo, err = tu.todoRepo.Create(&t)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">return</span>
}
func (tu *todoUsecase) Show(ctx context.Context, todoId int) (todo *entity.Todo, err error) <span class="cov0" title="0">{
userId, _ := tu.sessionS.GetSessionValue(ctx, "userId")
todo, err = tu.todoRepo.Find(todoId)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">if userId.(uint64) != todo.UserId </span><span class="cov0" title="0">{
return nil, errors.New("no your todo")
}</span>
<span class="cov0" title="0">return</span>
}
func (tu *todoUsecase) Edit(todoId int, updateParams *dto.TodoUpdateRequestDto) (todo *entity.Todo, err error) <span class="cov0" title="0">{
t := entity.Todo{ID: uint64(todoId), Title: updateParams.Title, Description: updateParams.Description}
todo, err = tu.todoRepo.Update(todoId, &t)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">return</span>
}
func (tu *todoUsecase) Delete(ctx context.Context, todoId int) error <span class="cov0" title="0">{
if err := tu.todoRepo.Delete(&entity.Todo{ID: uint64(todoId)}); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">return nil</span>
}
</pre>
<pre class="file" id="file7" style="display: none">package usecase
import (
"app/database"
"app/entity"
)
type UserUsecase interface {
FindAll() (users []*entity.User, err error)
}
type userUsecase struct {
userRepo database.UserRepository
}
func NewUserUsecase(userRepo database.UserRepository) UserUsecase <span class="cov0" title="0">{
return &userUsecase{
userRepo: userRepo,
}
}</span>
func (uu *userUsecase) FindAll() (users []*entity.User, err error) <span class="cov0" title="0">{
return uu.userRepo.FindAll()
}</span>
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>