udemy-go-microservices/logger-service/data/models.go

141 lines
3.0 KiB
Go
Raw Normal View History

2024-08-28 21:09:26 +00:00
package data
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
2024-08-29 11:12:38 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2024-08-28 21:09:26 +00:00
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var client *mongo.Client
func New(mongo *mongo.Client) Models {
client = mongo
return Models{
LogEntry: LogEntry{},
}
}
type Models struct {
LogEntry LogEntry
}
type LogEntry struct {
ID string `bson:"_id,omitempty" json:"id,omitempty"`
Name string `bson:"name" json:"name"`
Data string `bson:"data" json:"data"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
UpdatedAt time.Time `bson:"updated_at" json:"updated_at"`
}
func (l *LogEntry) Insert(entry LogEntry) error {
collection := client.Database("logs").Collection("logs")
2024-08-31 20:56:25 +00:00
newLog := LogEntry{
2024-08-28 21:09:26 +00:00
Name: entry.Name,
Data: entry.Data,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
2024-08-31 20:56:25 +00:00
}
_, err := collection.InsertOne(context.TODO(), newLog)
2024-08-28 21:09:26 +00:00
if err != nil {
2024-08-31 20:56:25 +00:00
log.Println("Insert error:", err)
2024-08-28 21:09:26 +00:00
return err
}
return nil
}
func (l *LogEntry) All() ([]*LogEntry, error) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
collection := client.Database("logs").Collection("logs")
opts := options.Find()
opts.SetSort(bson.D{{Key: "created_at", Value: -1}})
cursor, err := collection.Find(ctx, bson.D{}, opts)
if err != nil {
log.Println("Finding all docs error:", err)
return nil, err
}
defer cursor.Close(ctx)
var logs []*LogEntry
for cursor.Next(ctx) {
var item LogEntry
err := cursor.Decode(&item)
if err != nil {
log.Println("Error decoding log into slice:", err)
return nil, err
}
logs = append(logs, &item)
}
return logs, nil
}
2024-08-29 11:12:38 +00:00
func (l *LogEntry) GetOne(id string) (*LogEntry, error) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
collection := client.Database("logs").Collection("logs")
docID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
var entry LogEntry
err = collection.FindOne(ctx, bson.M{"_id": docID}).Decode(&entry)
if err != nil {
return nil, err
}
return &entry, nil
}
func (l *LogEntry) DropCollection() error {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
collection := client.Database("logs").Collection("logs")
if err := collection.Drop(ctx); err != nil {
return err
}
return nil
}
func (l *LogEntry) Update() (*mongo.UpdateResult, error) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
collection := client.Database("logs").Collection("logs")
docID, err := primitive.ObjectIDFromHex(l.ID)
if err != nil {
return nil, err
}
result, err := collection.UpdateOne(
ctx,
bson.M{"_id": docID},
bson.D{
{Key: "$set", Value: bson.D{
{Key: "name", Value: l.Name},
{Key: "data", Value: l.Data},
{Key: "updated_at", Value: time.Now()},
}},
})
if err != nil {
return nil, err
}
return result, nil
}