update db and added weight
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2023-04-16 17:08:40 +07:00
parent 3f9447f0b8
commit 09fe5f2324
7 changed files with 86 additions and 25 deletions

2
go.mod
View File

@ -1,6 +1,6 @@
module valera
go 1.18
go 1.20
require (
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible

View File

@ -2,6 +2,7 @@ package db
import (
"context"
"fmt"
"log"
"time"
@ -152,7 +153,7 @@ func (db *DB) SetPause(chatID int64, duration time.Duration) error {
return err
}
func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error) {
func (db *DB) GetStatBetween(chatID int64, startTime time.Time, endTime time.Time) (map[string]float64, error) {
ctx := context.Background()
if err := db.AddChat(chatID); err != nil {
@ -164,9 +165,17 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error
cursor, err := db.workoutsColection.Find(
ctx,
bson.M{
"chat_id": chatID,
"created_at": bson.M{"$gt": t},
"chat_id": chatID,
"created_at": bson.M{
"$gte": startTime,
"$lt": endTime,
},
},
options.Find().SetSort(
bson.M{
"created_at": 1,
},
),
)
if err != nil {
return nil, err
@ -176,6 +185,7 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
fmt.Println(result)
res[result.Name] += float64(result.Count)
}
if err := cursor.Err(); err != nil {
@ -185,9 +195,17 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error
caloriesCursor, err := db.caloriesColection.Find(
ctx,
bson.M{
"chat_id": chatID,
"created_at": bson.M{"$gt": t},
"chat_id": chatID,
"created_at": bson.M{
"$gte": startTime,
"$lt": endTime,
},
},
options.Find().SetSort(
bson.M{
"created_at": 1,
},
),
)
if err != nil {
return nil, err
@ -206,9 +224,17 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error
weightCursor, err := db.weightColection.Find(
ctx,
bson.M{
"chat_id": chatID,
"created_at": bson.M{"$gt": t},
"chat_id": chatID,
"created_at": bson.M{
"$gte": startTime,
"$lt": endTime,
},
},
options.Find().SetSort(
bson.M{
"created_at": 1,
},
),
)
if err != nil {
return nil, err

View File

@ -1,14 +1,36 @@
package times
import "time"
import (
"time"
)
var (
loc *time.Location
)
func init() {
loc, _ = time.LoadLocation("Asia/Novosibirsk")
}
func GetNow() time.Time {
t := time.Now().In(loc)
return t
}
func GetStartDay() time.Time {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
t := time.Now().In(loc)
t := GetNow()
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
return t
}
func GetStartDayMinus(days int) time.Time {
t := GetStartDay()
for i := 0; i < days; i++ {
t = t.Add(-24 * time.Hour)
}
return t
}
func GetStartWeek() time.Time {
t := GetStartDay()
t = t.Add(-6 * 24 * time.Hour)

View File

@ -2,11 +2,12 @@ package clear_bot_state
import (
"net/http"
"valera/internal/db"
"valera/internal/states"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/states"
)
var names = []string{

View File

@ -4,12 +4,13 @@ import (
"fmt"
"log"
"net/http"
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
)
var names = []string{
@ -39,7 +40,7 @@ func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo) error {
log.Println(err)
return nil
}
stat, err := s.dataBase.GetStatAfter(chatInfo.ChatID, times.GetStartDay())
stat, err := s.dataBase.GetStatBetween(chatInfo.ChatID, times.GetStartDay(), times.GetNow())
if err != nil {
log.Println(err)
return nil

View File

@ -4,13 +4,13 @@ import (
"fmt"
"net/http"
"time"
"valera/internal/db"
"valera/internal/states"
"valera/internal/modules/times"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
)
var names = []string{
@ -81,7 +81,7 @@ func (s *statBotState) GetHandler() (string, func(http.ResponseWriter, *http.Req
func (s *statBotState) sendStatToChatAfter(chatID int64, prefix string, t time.Time) error {
msgText := prefix
stat, err := s.dataBase.GetStatAfter(chatID, t)
stat, err := s.dataBase.GetStatBetween(chatID, t, times.GetNow())
if err != nil {
return err
}

View File

@ -6,11 +6,13 @@ import (
"net/http"
"strconv"
"strings"
"valera/internal/db"
"valera/internal/states"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
)
var names = []string{
@ -44,7 +46,16 @@ func (s *weightBotState) Do(text string, chatInfo *db.ChatInfo) error {
log.Println(err)
return nil
}
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("%vкг, записал.", weight)))
stat, err := s.dataBase.GetStatBetween(chatInfo.ChatID, times.GetStartDayMinus(1), times.GetStartDay())
if err != nil {
log.Println(err)
return nil
}
dWeight := 0.0
if stat["Вес кг"] != 0 {
dWeight = weight - stat["Вес кг"]
}
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("%vкг, записал.\nИзменение веса: %vкг", weight, dWeight)))
return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone)
}