/
room.go
99 lines (81 loc) · 2.22 KB
/
room.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 repository
import (
"context"
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-redis/cache/v8"
"github.com/sakuraapp/shared/pkg/constant"
"github.com/sakuraapp/shared/pkg/model"
)
type RoomRepository struct {
db *pg.DB
cache *cache.Cache
}
func (r *RoomRepository) Get(ctx context.Context, id model.RoomId) (*model.Room, error) {
room := new(model.Room)
if err := r.cache.Once(&cache.Item{
Ctx: ctx,
Key: fmt.Sprintf(constant.RoomCacheFmt, id),
Value: room,
TTL: constant.RoomCacheTTL,
Do: func(item *cache.Item) (interface{}, error) {
return r.fetch(room, id)
},
}); err != nil {
return nil, err
}
return room, nil
}
func (r *RoomRepository) fetch(room *model.Room, id model.RoomId) (*model.Room, error) {
err := r.db.Model(room).
Column("room.*").
Relation("Owner").
ColumnExpr("discriminator.value AS owner__discriminator").
Join("LEFT JOIN discriminators AS discriminator ON discriminator.owner_id = ?", pg.Ident("owner.id")).
Where("room.id = ?", id).
First()
if err == pg.ErrNoRows {
err = nil
room = nil
}
return room, err
}
func (r *RoomRepository) GetLatest() ([]model.Room, error) {
var rooms []model.Room
err := r.db.Model(&rooms).
Column("room.*").
Relation("Owner").
ColumnExpr("discriminator.value AS owner__discriminator").
Join("LEFT JOIN discriminators AS discriminator ON discriminator.owner_id = ?", pg.Ident("owner.id")).
Where("room.private = FALSE").
Order("room.id DESC").
Limit(5).
Select()
if err == pg.ErrNoRows {
err = nil
rooms = []model.Room{}
}
return rooms, err
}
func (r *RoomRepository) GetByOwnerId(id model.UserId) (*model.Room, error) {
room := new(model.Room)
err := r.db.Model(room).
Where("owner_id = ?", id).
First()
if err == pg.ErrNoRows {
err = nil
room = nil
}
return room, err
}
func (r *RoomRepository) Create(room *model.Room) error {
_, err := r.db.QueryOne(room, `INSERT INTO "rooms" ("id", "name", "owner_id", "private") VALUES (DEFAULT, ?, ?, ?) RETURNING "id"`, room.Name, room.OwnerId, room.Private)
return err
}
func (r *RoomRepository) UpdateInfo(room *model.Room) error {
_, err := r.db.Model(room).
Set("name = ?name, private = ?private").
Where("id = ?", room.Id).
Update()
return err
}