valera/db/db.go

281 lines
6.1 KiB
Go
Raw Normal View History

2023-03-11 10:17:30 +00:00
package db
import (
"context"
"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-11 10:30:38 +00:00
const (
UserStateNone = UserState("")
UserStateGo = UserState("Go")
2023-03-12 06:53:50 +00:00
UserStateEat = UserState("Eat")
2023-03-11 10:30:38 +00:00
)
type UserState string
2023-03-11 10:17:30 +00:00
type chatDTO struct {
ChatID int64 `bson:"chat_id"`
}
2023-03-11 10:30:38 +00:00
type ChatInfoDTO struct {
2023-03-11 11:30:12 +00:00
ChatID int64 `bson:"chat_id"`
Status string `bson:"status"`
2023-03-11 10:17:30 +00:00
}
2023-03-11 10:30:38 +00:00
func (c *ChatInfoDTO) GetStatus() UserState {
2023-03-12 07:02:54 +00:00
return UserState(c.Status)
2023-03-11 10:30:38 +00:00
}
2023-03-11 10:17:30 +00:00
type Workout struct {
2023-03-11 11:30:12 +00:00
ChatID int64 `bson:"chat_id"`
Name string `bson:"name"`
Count int `bson:"count"`
CreatedAt time.Time `bson:"created_at"`
Username string `bson:"username"`
2023-03-11 10:17:30 +00:00
}
func NewWorkout(
name string,
count int,
2023-03-11 11:30:12 +00:00
username string,
2023-03-11 10:17:30 +00:00
) *Workout {
2023-03-12 06:53:50 +00:00
loc, _ := time.LoadLocation("Asia/Novosibirsk")
2023-03-11 10:17:30 +00:00
return &Workout{
2023-03-12 06:53:50 +00:00
Name: name,
Count: count,
Username: username,
CreatedAt: time.Now().In(loc),
}
}
type Calories struct {
ChatID int64 `bson:"chat_id"`
Count int `bson:"count"`
CreatedAt time.Time `bson:"created_at"`
Username string `bson:"username"`
}
func NewCalories(
count int,
username string,
) *Calories {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
return &Calories{
Count: count,
Username: username,
CreatedAt: time.Now().In(loc),
2023-03-11 10:17:30 +00:00
}
}
var cfg *config.Config
func init() {
cfg = config.NewConfig()
}
func AddChat(chatID int64) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
result := collection.FindOne(ctx, bson.M{"chat_id": chatID})
if result.Err() == mongo.ErrNoDocuments {
if _, err := collection.InsertOne(ctx, &chatDTO{ChatID: chatID}); err != nil {
return err
}
return nil
}
return result.Err()
}
2023-03-11 11:30:12 +00:00
func AddWorkout(chatID int64, workout *Workout) error {
2023-03-11 12:38:01 +00:00
if workout.Count <= 0 {
return nil
}
2023-03-11 10:17:30 +00:00
if err := AddChat(chatID); err != nil {
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()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
if err != nil {
return err
}
2023-03-11 11:30:12 +00:00
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName)
_, 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 06:53:50 +00:00
func AddCalories(chatID int64, calories *Calories) error {
if calories.Count <= 0 {
return nil
}
if err := 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(cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(cfg.DBName).Collection(cfg.CaloriesCollectionName)
_, err = collection.InsertOne(
ctx,
calories,
)
return err
}
2023-03-11 12:38:01 +00:00
func GetChatInfo(chatID int64) (*ChatInfoDTO, error) {
2023-03-11 11:30:12 +00:00
if err := AddChat(chatID); err != nil {
return nil, err
}
2023-03-11 10:30:38 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
if err != nil {
return nil, err
}
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
chatInfoDTO := &ChatInfoDTO{}
if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil {
return nil, err
}
return chatInfoDTO, err
}
2023-03-11 12:38:01 +00:00
func SetStatusToChat(chatID int64, status UserState) error {
2023-03-11 10:30:38 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
_, 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
func GetStat(chatID int64) (map[string]int, error) {
if err := 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(cfg.MongoURL))
if err != nil {
return nil, err
}
2023-03-11 12:00:50 +00:00
loc, _ := time.LoadLocation("Asia/Novosibirsk")
t := time.Now().In(loc).Add(-24 * time.Hour)
2023-03-12 06:53:50 +00:00
res := map[string]int{}
2023-03-11 11:30:12 +00:00
collection := client.Database(cfg.DBName).Collection(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
caloriesCollection := client.Database(cfg.DBName).Collection(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 := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
res["Калории"] += result.Count
}
if err := cursor.Err(); err != nil {
log.Fatal(err)
}
2023-03-11 11:30:12 +00:00
return res, err
}
2023-03-11 12:38:01 +00:00
func GetAllChats() ([]int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
if err != nil {
return nil, err
}
collection := client.Database(cfg.DBName).Collection(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()) {
var result ChatInfoDTO
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
}