-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.go
62 lines (53 loc) · 1.2 KB
/
blog.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
package repository
import (
"context"
"github.com/granitebps/fiber-api/pkg/core"
"github.com/granitebps/fiber-api/src/model"
)
type BlogRepository struct {
Core *core.Core
}
func NewBlogRepository(c *core.Core) *BlogRepository {
return &BlogRepository{
Core: c,
}
}
func (r *BlogRepository) GetAll(ctx context.Context) (res []model.Blog, err error) {
err = r.Core.Database.Db.
WithContext(ctx).
Find(&res).
Error
return
}
func (r *BlogRepository) GetByID(ctx context.Context, id uint64) (res model.Blog, err error) {
err = r.Core.Database.Db.
WithContext(ctx).
Where("id = ?", id).
First(&res).
Error
return
}
func (r *BlogRepository) Insert(ctx context.Context, data *model.Blog) (err error) {
err = r.Core.Database.Db.
WithContext(ctx).
Create(&data).
Error
return
}
func (r *BlogRepository) Update(ctx context.Context, id uint64, data *model.Blog) (err error) {
err = r.Core.Database.Db.
WithContext(ctx).
Model(&model.Blog{}).
Where("id = ?", id).
Updates(&data).
Error
return
}
func (r *BlogRepository) Delete(ctx context.Context, id uint64) (err error) {
err = r.Core.Database.Db.
WithContext(ctx).
Where("id = ?", id).
Delete(&model.Blog{}).
Error
return
}