mv mongo config to secrets
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-03-13 02:38:48 +07:00
parent af0b834bf3
commit f066e01d21
8 changed files with 159 additions and 118 deletions
+22
View File
@@ -0,0 +1,22 @@
package db
import "time"
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),
}
}
+22
View File
@@ -0,0 +1,22 @@
package db
const (
UserStateNone = UserState("")
UserStateGo = UserState("Go")
UserStateEat = UserState("Eat")
)
type UserState string
type Chat struct {
ChatID int64 `bson:"chat_id"`
}
type ChatInfo struct {
ChatID int64 `bson:"chat_id"`
Status string `bson:"status"`
}
func (c *ChatInfo) GetStatus() UserState {
return UserState(c.Status)
}
+37 -92
View File
@@ -10,88 +10,33 @@ import (
"valera/config"
)
const (
UserStateNone = UserState("")
UserStateGo = UserState("Go")
UserStateEat = UserState("Eat")
)
type UserState string
type chatDTO struct {
ChatID int64 `bson:"chat_id"`
type DB struct {
cfg *config.Config
}
type ChatInfoDTO struct {
ChatID int64 `bson:"chat_id"`
Status string `bson:"status"`
}
func (c *ChatInfoDTO) GetStatus() UserState {
return UserState(c.Status)
}
type Workout struct {
ChatID int64 `bson:"chat_id"`
Name string `bson:"name"`
Count int `bson:"count"`
CreatedAt time.Time `bson:"created_at"`
Username string `bson:"username"`
}
func NewWorkout(
name string,
count int,
username string,
) *Workout {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
return &Workout{
Name: name,
Count: count,
Username: username,
CreatedAt: time.Now().In(loc),
func NewDB(
mongoURL string,
dbName string,
) *DB {
return &DB{
cfg: config.NewConfig(mongoURL, dbName),
}
}
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),
}
}
var cfg *config.Config
func init() {
cfg = config.NewConfig()
}
func AddChat(chatID int64) error {
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(cfg.MongoURL))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
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, &chatDTO{ChatID: chatID}); err != nil {
if _, err := collection.InsertOne(ctx, &Chat{ChatID: chatID}); err != nil {
return err
}
return nil
@@ -99,11 +44,11 @@ func AddChat(chatID int64) error {
return result.Err()
}
func AddWorkout(chatID int64, workout *Workout) error {
func (db *DB) AddWorkout(chatID int64, workout *Workout) error {
if workout.Count <= 0 {
return nil
}
if err := AddChat(chatID); err != nil {
if err := db.AddChat(chatID); err != nil {
return err
}
workout.ChatID = chatID
@@ -111,12 +56,12 @@ func AddWorkout(chatID int64, workout *Workout) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName)
collection := client.Database(db.cfg.DBName).Collection(db.cfg.WorkoutsCollectionName)
_, err = collection.InsertOne(
ctx,
workout,
@@ -124,11 +69,11 @@ func AddWorkout(chatID int64, workout *Workout) error {
return err
}
func AddCalories(chatID int64, calories *Calories) error {
func (db *DB) AddCalories(chatID int64, calories *Calories) error {
if calories.Count <= 0 {
return nil
}
if err := AddChat(chatID); err != nil {
if err := db.AddChat(chatID); err != nil {
return err
}
calories.ChatID = chatID
@@ -136,12 +81,12 @@ func AddCalories(chatID int64, calories *Calories) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(cfg.DBName).Collection(cfg.CaloriesCollectionName)
collection := client.Database(db.cfg.DBName).Collection(db.cfg.CaloriesCollectionName)
_, err = collection.InsertOne(
ctx,
calories,
@@ -149,36 +94,36 @@ func AddCalories(chatID int64, calories *Calories) error {
return err
}
func GetChatInfo(chatID int64) (*ChatInfoDTO, error) {
if err := AddChat(chatID); err != nil {
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(cfg.MongoURL))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return nil, err
}
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
chatInfoDTO := &ChatInfoDTO{}
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 SetStatusToChat(chatID int64, status UserState) error {
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(cfg.MongoURL))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return err
}
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
_, err = collection.UpdateOne(
ctx,
bson.M{"chat_id": chatID},
@@ -187,14 +132,14 @@ func SetStatusToChat(chatID int64, status UserState) error {
return err
}
func GetStat(chatID int64) (map[string]int, error) {
if err := AddChat(chatID); err != nil {
func (db *DB) GetStat(chatID int64) (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(cfg.MongoURL))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return nil, err
}
@@ -204,7 +149,7 @@ func GetStat(chatID int64) (map[string]int, error) {
res := map[string]int{}
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName)
collection := client.Database(db.cfg.DBName).Collection(db.cfg.WorkoutsCollectionName)
cursor, err := collection.Find(
ctx,
bson.M{
@@ -226,7 +171,7 @@ func GetStat(chatID int64) (map[string]int, error) {
log.Fatal(err)
}
caloriesCollection := client.Database(cfg.DBName).Collection(cfg.CaloriesCollectionName)
caloriesCollection := client.Database(db.cfg.DBName).Collection(db.cfg.CaloriesCollectionName)
caloriesCursor, err := caloriesCollection.Find(
ctx,
bson.M{
@@ -251,23 +196,23 @@ func GetStat(chatID int64) (map[string]int, error) {
return res, err
}
func GetAllChats() ([]int64, error) {
func (db *DB) GetAllChats() ([]int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil {
return nil, err
}
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
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 ChatInfoDTO
var result ChatInfo
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
+25
View File
@@ -0,0 +1,25 @@
package db
import "time"
type Workout struct {
ChatID int64 `bson:"chat_id"`
Name string `bson:"name"`
Count int `bson:"count"`
CreatedAt time.Time `bson:"created_at"`
Username string `bson:"username"`
}
func NewWorkout(
name string,
count int,
username string,
) *Workout {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
return &Workout{
Name: name,
Count: count,
Username: username,
CreatedAt: time.Now().In(loc),
}
}