@@ -14,6 +14,7 @@ var ErrDriverUnrecognized = errors.New("driver not recognized, check installed v
|
|||||||
|
|
||||||
type Database interface {
|
type Database interface {
|
||||||
Changes() repositories.ChangeRepository
|
Changes() repositories.ChangeRepository
|
||||||
|
Channels() repositories.ChannelRepository
|
||||||
Characters() repositories.CharacterRepository
|
Characters() repositories.CharacterRepository
|
||||||
Tags() repositories.TagRepository
|
Tags() repositories.TagRepository
|
||||||
Logs() repositories.LogRepository
|
Logs() repositories.LogRepository
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package mongodb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"git.aiterp.net/rpdata/api/models"
|
||||||
|
"github.com/globalsign/mgo"
|
||||||
|
"github.com/globalsign/mgo/bson"
|
||||||
|
)
|
||||||
|
|
||||||
|
type channelRepository struct {
|
||||||
|
channels *mgo.Collection
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *channelRepository) Find(ctx context.Context, name string) (*models.Channel, error) {
|
||||||
|
channel := new(models.Channel)
|
||||||
|
err := r.channels.FindId(name).One(channel)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return channel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *channelRepository) List(ctx context.Context, filter models.ChannelFilter) ([]*models.Channel, error) {
|
||||||
|
query := bson.M{}
|
||||||
|
if filter.Logged != nil {
|
||||||
|
query["logged"] = *filter.Logged
|
||||||
|
}
|
||||||
|
if filter.EventName != nil {
|
||||||
|
query["eventName"] = *filter.EventName
|
||||||
|
}
|
||||||
|
if filter.LocationName != nil {
|
||||||
|
query["locationName"] = *filter.LocationName
|
||||||
|
}
|
||||||
|
|
||||||
|
channels := make([]*models.Channel, 0, 32)
|
||||||
|
err := r.channels.Find(query).Limit(filter.Limit).Sort("_id").All(&channels)
|
||||||
|
if err != nil {
|
||||||
|
if err == mgo.ErrNotFound {
|
||||||
|
return channels, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return channels, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *channelRepository) Insert(ctx context.Context, channel models.Channel) (*models.Channel, error) {
|
||||||
|
err := r.channels.Insert(&channel)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &channel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *channelRepository) Update(ctx context.Context, channel models.Channel, update models.ChannelUpdate) (*models.Channel, error) {
|
||||||
|
updateBson := bson.M{}
|
||||||
|
if update.Logged != nil {
|
||||||
|
updateBson["logged"] = *update.Logged
|
||||||
|
}
|
||||||
|
if update.LocationName != nil {
|
||||||
|
updateBson["locationName"] = *update.LocationName
|
||||||
|
}
|
||||||
|
if update.EventName != nil {
|
||||||
|
updateBson["eventName"] = *update.EventName
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.channels.UpdateId(channel.Name, bson.M{"$set": updateBson})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &channel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *channelRepository) Remove(ctx context.Context, channel models.Channel) error {
|
||||||
|
return r.channels.RemoveId(channel.Name)
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ type MongoDB struct {
|
|||||||
session *mgo.Session
|
session *mgo.Session
|
||||||
|
|
||||||
changes repositories.ChangeRepository
|
changes repositories.ChangeRepository
|
||||||
|
channels *channelRepository
|
||||||
characters repositories.CharacterRepository
|
characters repositories.CharacterRepository
|
||||||
tags repositories.TagRepository
|
tags repositories.TagRepository
|
||||||
logs *logRepository
|
logs *logRepository
|
||||||
@@ -41,6 +42,10 @@ func (m *MongoDB) Posts() repositories.PostRepository {
|
|||||||
return m.posts
|
return m.posts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MongoDB) Channels() repositories.ChannelRepository {
|
||||||
|
return m.channels
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MongoDB) Close(ctx context.Context) error {
|
func (m *MongoDB) Close(ctx context.Context) error {
|
||||||
m.session.Close()
|
m.session.Close()
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+17
-1
@@ -13,4 +13,20 @@ type Channel struct {
|
|||||||
// ChangeObject in GQL.
|
// ChangeObject in GQL.
|
||||||
func (*Channel) IsChangeObject() {
|
func (*Channel) IsChangeObject() {
|
||||||
panic("this method is a dummy, and so is its caller")
|
panic("this method is a dummy, and so is its caller")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ChannelFilter is a filter for channel listing.
|
||||||
|
type ChannelFilter struct {
|
||||||
|
Logged *bool `json:"logged"`
|
||||||
|
EventName *string `json:"eventName"`
|
||||||
|
LocationName *string `json:"locationName"`
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChannelUpdate is a filter for channel listing.
|
||||||
|
type ChannelUpdate struct {
|
||||||
|
Logged *bool `json:"logged"`
|
||||||
|
Hub *bool `json:"hub"`
|
||||||
|
EventName *string `json:"eventName"`
|
||||||
|
LocationName *string `json:"locationName"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"git.aiterp.net/rpdata/api/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChannelRepository is an interface for a database using channels.
|
||||||
|
type ChannelRepository interface {
|
||||||
|
Find(ctx context.Context, name string) (*models.Channel, error)
|
||||||
|
List(ctx context.Context, filter models.ChannelFilter) ([]*models.Channel, error)
|
||||||
|
Insert(ctx context.Context, channel models.Channel) (*models.Channel, error)
|
||||||
|
Update(ctx context.Context, channel models.Channel, update models.ChannelUpdate) (*models.Channel, error)
|
||||||
|
Remove(ctx context.Context, channel models.Channel) error
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"git.aiterp.net/rpdata/api/models"
|
"git.aiterp.net/rpdata/api/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CharacterRepository is an interface for a database using logs.
|
// CharacterRepository is an interface for a database using characters.
|
||||||
type CharacterRepository interface {
|
type CharacterRepository interface {
|
||||||
Find(ctx context.Context, id string) (*models.Character, error)
|
Find(ctx context.Context, id string) (*models.Character, error)
|
||||||
FindNick(ctx context.Context, nick string) (*models.Character, error)
|
FindNick(ctx context.Context, nick string) (*models.Character, error)
|
||||||
|
|||||||
Reference in New Issue
Block a user