valera/db/db.go

233 lines
5.3 KiB
Go

package db
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"
"valera/config"
)
type DB struct {
cfg *config.Config
}
func NewDB(
mongoURL string,
dbName string,
) *DB {
return &DB{
cfg: config.NewConfig(mongoURL, dbName),
}
}
func (db *DB) AddChat(chatID int64) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
result := collection.FindOne(ctx, bson.M{"chat_id": chatID})
if result.Err() == mongo.ErrNoDocuments {
if _, err := collection.InsertOne(ctx, &Chat{ChatID: chatID}); err != nil {
return err
}
return nil
}
return result.Err()
}
func (db *DB) AddWorkout(chatID int64, workout *Workout) error {
if workout.Count <= 0 {
return nil
}
if err := db.AddChat(chatID); err != nil {
return err
}
workout.ChatID = chatID
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(db.cfg.DBName).Collection(db.cfg.WorkoutsCollectionName)
_, err = collection.InsertOne(
ctx,
workout,
)
return err
}
func (db *DB) AddCalories(chatID int64, calories *Calories) error {
if calories.Count <= 0 {
return nil
}
if err := db.AddChat(chatID); err != nil {
return err
}
calories.ChatID = chatID
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(db.cfg.DBName).Collection(db.cfg.CaloriesCollectionName)
_, err = collection.InsertOne(
ctx,
calories,
)
return err
}
func (db *DB) GetChatInfo(chatID int64) (*ChatInfo, error) {
if err := db.AddChat(chatID); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return nil, err
}
collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
chatInfoDTO := &ChatInfo{}
if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil {
return nil, err
}
return chatInfoDTO, err
}
func (db *DB) SetStatusToChat(chatID int64, status UserState) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
_, err = collection.UpdateOne(
ctx,
bson.M{"chat_id": chatID},
bson.M{"$set": bson.M{"status": status}},
)
return err
}
func (db *DB) GetStat(chatID int64, cron bool) (map[string]int, error) {
if err := db.AddChat(chatID); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return nil, err
}
loc, _ := time.LoadLocation("Asia/Novosibirsk")
t := time.Now().In(loc)
if cron {
t = t.Add(-24 * time.Hour)
}
if !cron {
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
}
res := map[string]int{}
collection := client.Database(db.cfg.DBName).Collection(db.cfg.WorkoutsCollectionName)
cursor, err := collection.Find(
ctx,
bson.M{
"chat_id": chatID,
"created_at": bson.M{"$gt": t},
},
)
if err != nil {
return nil, err
}
for cursor.Next(context.Background()) {
var result Workout
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
res[result.Name] += result.Count
}
if err := cursor.Err(); err != nil {
log.Fatal(err)
}
caloriesCollection := client.Database(db.cfg.DBName).Collection(db.cfg.CaloriesCollectionName)
caloriesCursor, err := caloriesCollection.Find(
ctx,
bson.M{
"chat_id": chatID,
"created_at": bson.M{"$gt": t},
},
)
if err != nil {
return nil, err
}
for caloriesCursor.Next(context.Background()) {
var result Calories
if err := caloriesCursor.Decode(&result); err != nil {
log.Fatal(err)
}
res["Калории"] += result.Count
}
if err := caloriesCursor.Err(); err != nil {
log.Fatal(err)
}
return res, err
}
func (db *DB) GetAllChats() ([]int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return nil, err
}
collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
cursor, err := collection.Find(ctx, bson.M{})
if err != nil {
return nil, err
}
var res []int64
for cursor.Next(context.Background()) {
var result ChatInfo
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
res = append(res, result.ChatID)
}
if err := cursor.Err(); err != nil {
log.Fatal(err)
}
return res, err
}