2021-04-10 18:46:51 +00:00
|
|
|
package dal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bitbucket.org/BlackBroker/heruvym/model"
|
|
|
|
"context"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
"github.com/themakers/hlog"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-04-11 09:48:15 +00:00
|
|
|
const (
|
|
|
|
collMessages = "messages"
|
|
|
|
collTickets = "tickets"
|
|
|
|
)
|
2021-04-10 18:46:51 +00:00
|
|
|
|
|
|
|
type DAL struct {
|
2021-04-11 09:48:15 +00:00
|
|
|
logger hlog.Logger
|
|
|
|
colMsg, colTck *mongo.Collection
|
|
|
|
client *mongo.Client
|
2021-04-10 18:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorConnectToDB struct {
|
|
|
|
Err error
|
|
|
|
MongoURI string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorPingDB struct {
|
|
|
|
Err error
|
|
|
|
MongoURI string
|
|
|
|
}
|
|
|
|
|
|
|
|
type InfoPing struct {
|
|
|
|
MongoURI string
|
2021-04-11 09:48:15 +00:00
|
|
|
Nanoseconds int64
|
2021-04-10 18:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(
|
|
|
|
ctx context.Context,
|
|
|
|
mongoURI, database string,
|
|
|
|
log hlog.Logger,
|
|
|
|
) (*DAL, error) {
|
|
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI))
|
|
|
|
if err != nil {
|
|
|
|
log.Emit(ErrorConnectToDB{
|
|
|
|
Err: err,
|
|
|
|
MongoURI: mongoURI,
|
|
|
|
})
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-11 09:48:15 +00:00
|
|
|
before := time.Now().Unix()
|
2021-04-10 18:46:51 +00:00
|
|
|
|
|
|
|
if err := client.Ping(ctx, readpref.PrimaryPreferred()); err != nil {
|
|
|
|
log.Emit(ErrorPingDB{
|
|
|
|
Err: err,
|
|
|
|
MongoURI: mongoURI,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Emit(InfoPing{
|
|
|
|
MongoURI: mongoURI,
|
2021-04-11 09:48:15 +00:00
|
|
|
Nanoseconds: time.Now().Unix() - before,
|
2021-04-10 18:46:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return &DAL{
|
|
|
|
client: client,
|
2021-04-11 09:48:15 +00:00
|
|
|
colMsg: client.Database(database).Collection(collMessages),
|
|
|
|
colTck: client.Database(database).Collection(collTickets),
|
2021-04-10 18:46:51 +00:00
|
|
|
logger: log.Module("DAL"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorInsert struct {
|
2021-04-11 09:48:15 +00:00
|
|
|
Err error
|
2021-04-10 18:46:51 +00:00
|
|
|
UserID, SessionID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DAL) PutMessage(
|
|
|
|
ctx context.Context,
|
2021-04-11 09:48:15 +00:00
|
|
|
message, userID, sessionID, ticketID string,
|
2021-04-10 18:46:51 +00:00
|
|
|
files []string,
|
2021-04-11 09:48:15 +00:00
|
|
|
) error {
|
2021-04-10 18:46:51 +00:00
|
|
|
|
2021-04-11 09:48:15 +00:00
|
|
|
if _, err := d.colMsg.InsertOne(ctx, &model.Message{
|
2021-04-10 18:46:51 +00:00
|
|
|
ID: xid.New().String(),
|
|
|
|
UserID: userID,
|
|
|
|
SessionID: sessionID,
|
2021-04-11 09:48:15 +00:00
|
|
|
TicketID: ticketID,
|
2021-04-10 18:46:51 +00:00
|
|
|
Message: message,
|
|
|
|
Files: files,
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}); err != nil {
|
|
|
|
d.logger.Emit(ErrorInsert{
|
|
|
|
Err: err,
|
|
|
|
UserID: userID,
|
|
|
|
SessionID: sessionID,
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-11 09:48:15 +00:00
|
|
|
|
|
|
|
func (d *DAL) CreateTicket(
|
|
|
|
ctx context.Context,
|
|
|
|
userID,
|
|
|
|
sessionID,
|
|
|
|
title string,
|
|
|
|
) (string, error) {
|
|
|
|
|
|
|
|
tiketID := xid.New().String()
|
|
|
|
|
|
|
|
if _, err := d.colTck.InsertOne(ctx, &model.Ticket{
|
|
|
|
ID: tiketID,
|
|
|
|
UserID: userID,
|
|
|
|
SessionID: sessionID,
|
|
|
|
Title: title,
|
2021-04-11 09:53:26 +00:00
|
|
|
CreatedAt: time.Now(),
|
2021-04-11 09:48:15 +00:00
|
|
|
}); err != nil {
|
|
|
|
d.logger.Emit(ErrorInsert{
|
|
|
|
Err: err,
|
|
|
|
UserID: userID,
|
|
|
|
SessionID: sessionID,
|
|
|
|
})
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tiketID, nil
|
|
|
|
}
|