valera/db/db.go

233 lines
5.3 KiB
Go
Raw Normal View History

2023-03-11 10:17:30 +00:00
package db
import (
"context"
2023-03-13 18:49:54 +00:00
"fmt"
2023-03-11 10:17:30 +00:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
2023-03-11 11:30:12 +00:00
"log"
2023-03-11 10:17:30 +00:00
"time"
"valera/config"
)
2023-03-12 19:38:48 +00:00
type DB struct {
cfg *config.Config
2023-03-11 10:17:30 +00:00
}
2023-03-12 19:38:48 +00:00
func NewDB(
mongoURL string,
dbName string,
) *DB {
return &DB{
cfg: config.NewConfig(mongoURL, dbName),
2023-03-12 06:53:50 +00:00
}
}
2023-03-12 19:38:48 +00:00
func (db *DB) AddChat(chatID int64) error {
2023-03-11 10:17:30 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
2023-03-12 19:38:48 +00:00
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
2023-03-11 10:17:30 +00:00
if err != nil {
return err
}
2023-03-12 19:38:48 +00:00
collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
2023-03-11 10:17:30 +00:00
result := collection.FindOne(ctx, bson.M{"chat_id": chatID})
if result.Err() == mongo.ErrNoDocuments {
2023-03-12 19:38:48 +00:00
if _, err := collection.InsertOne(ctx, &Chat{ChatID: chatID}); err != nil {
2023-03-11 10:17:30 +00:00
return err
}
return nil
}
return result.Err()
}
2023-03-12 19:38:48 +00:00
func (db *DB) AddWorkout(chatID int64, workout *Workout) error {
2023-03-11 12:38:01 +00:00
if workout.Count <= 0 {
return nil
}
2023-03-12 19:38:48 +00:00
if err := db.AddChat(chatID); err != nil {
2023-03-11 10:17:30 +00:00
return err
}
2023-03-11 11:30:12 +00:00
workout.ChatID = chatID
2023-03-11 10:17:30 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
2023-03-12 19:38:48 +00:00
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
2023-03-11 10:17:30 +00:00
if err != nil {
return err
}
2023-03-12 19:38:48 +00:00
collection := client.Database(db.cfg.DBName).Collection(db.cfg.WorkoutsCollectionName)
2023-03-11 11:30:12 +00:00
_, err = collection.InsertOne(
2023-03-11 10:17:30 +00:00
ctx,
2023-03-11 11:30:12 +00:00
workout,
2023-03-11 10:17:30 +00:00
)
return err
}
2023-03-12 19:38:48 +00:00
func (db *DB) AddCalories(chatID int64, calories *Calories) error {
2023-03-12 06:53:50 +00:00
if calories.Count <= 0 {
return nil
}
2023-03-12 19:38:48 +00:00
if err := db.AddChat(chatID); err != nil {
2023-03-12 06:53:50 +00:00
return err
}
calories.ChatID = chatID
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
2023-03-12 19:38:48 +00:00
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
2023-03-12 06:53:50 +00:00
if err != nil {
return err
}
2023-03-12 19:38:48 +00:00
collection := client.Database(db.cfg.DBName).Collection(db.cfg.CaloriesCollectionName)
2023-03-12 06:53:50 +00:00
_, err = collection.InsertOne(
ctx,
calories,
)
return err
}
2023-03-12 19:38:48 +00:00
func (db *DB) GetChatInfo(chatID int64) (*ChatInfo, error) {
if err := db.AddChat(chatID); err != nil {
2023-03-11 11:30:12 +00:00
return nil, err
}
2023-03-11 10:30:38 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
2023-03-12 19:38:48 +00:00
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
2023-03-11 10:30:38 +00:00
if err != nil {
return nil, err
}
2023-03-12 19:38:48 +00:00
collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
chatInfoDTO := &ChatInfo{}
2023-03-11 10:30:38 +00:00
if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil {
return nil, err
}
return chatInfoDTO, err
}
2023-03-12 19:38:48 +00:00
func (db *DB) SetStatusToChat(chatID int64, status UserState) error {
2023-03-11 10:30:38 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
2023-03-12 19:38:48 +00:00
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
2023-03-11 10:30:38 +00:00
if err != nil {
return err
}
2023-03-12 19:38:48 +00:00
collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
2023-03-11 10:30:38 +00:00
_, err = collection.UpdateOne(
ctx,
bson.M{"chat_id": chatID},
bson.M{"$set": bson.M{"status": status}},
)
return err
2023-03-11 10:17:30 +00:00
}
2023-03-11 11:30:12 +00:00
2023-03-13 18:49:54 +00:00
func (db *DB) GetStat(chatID int64, cron bool) (map[string]int, error) {
2023-03-12 19:38:48 +00:00
if err := db.AddChat(chatID); err != nil {
2023-03-11 11:30:12 +00:00
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
2023-03-12 19:38:48 +00:00
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
2023-03-11 11:30:12 +00:00
if err != nil {
return nil, err
}
2023-03-11 12:00:50 +00:00
loc, _ := time.LoadLocation("Asia/Novosibirsk")
2023-03-13 18:49:54 +00:00
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)
}
2023-03-11 12:00:50 +00:00
2023-03-12 06:53:50 +00:00
res := map[string]int{}
2023-03-12 19:38:48 +00:00
collection := client.Database(db.cfg.DBName).Collection(db.cfg.WorkoutsCollectionName)
2023-03-11 12:00:50 +00:00
cursor, err := collection.Find(
ctx,
bson.M{
"chat_id": chatID,
"created_at": bson.M{"$gt": t},
},
)
2023-03-11 11:30:12 +00:00
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)
}
2023-03-12 06:53:50 +00:00
2023-03-12 19:38:48 +00:00
caloriesCollection := client.Database(db.cfg.DBName).Collection(db.cfg.CaloriesCollectionName)
2023-03-12 06:53:50 +00:00
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
2023-03-12 07:06:42 +00:00
if err := caloriesCursor.Decode(&result); err != nil {
2023-03-12 06:53:50 +00:00
log.Fatal(err)
}
res["Калории"] += result.Count
}
2023-03-12 07:06:42 +00:00
if err := caloriesCursor.Err(); err != nil {
2023-03-12 06:53:50 +00:00
log.Fatal(err)
}
2023-03-11 11:30:12 +00:00
return res, err
}
2023-03-11 12:38:01 +00:00
2023-03-12 19:38:48 +00:00
func (db *DB) GetAllChats() ([]int64, error) {
2023-03-11 12:38:01 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
2023-03-12 19:38:48 +00:00
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
2023-03-11 12:38:01 +00:00
if err != nil {
return nil, err
}
2023-03-12 19:38:48 +00:00
collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
2023-03-12 06:53:50 +00:00
cursor, err := collection.Find(ctx, bson.M{})
2023-03-11 12:38:01 +00:00
if err != nil {
return nil, err
}
var res []int64
for cursor.Next(context.Background()) {
2023-03-12 19:38:48 +00:00
var result ChatInfo
2023-03-11 12:38:01 +00:00
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
}