Setting up logger data models
This commit is contained in:
		@ -3,6 +3,7 @@ package main
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.mongodb.org/mongo-driver/mongo"
 | 
						"go.mongodb.org/mongo-driver/mongo"
 | 
				
			||||||
	"go.mongodb.org/mongo-driver/mongo/options"
 | 
						"go.mongodb.org/mongo-driver/mongo/options"
 | 
				
			||||||
@ -25,8 +26,13 @@ func main() {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Panic(err)
 | 
							log.Panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						// create a context in order to disconnect
 | 
				
			||||||
 | 
						ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
 | 
				
			||||||
 | 
						defer cancel()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// close connection
 | 
				
			||||||
	defer func() {
 | 
						defer func() {
 | 
				
			||||||
		if err = mongoClient.Disconnect(context.TODO()); err != nil {
 | 
							if err = mongoClient.Disconnect(ctx); err != nil {
 | 
				
			||||||
			panic(err)
 | 
								panic(err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										78
									
								
								logger-service/data/models.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								logger-service/data/models.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,78 @@
 | 
				
			|||||||
 | 
					package data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"go.mongodb.org/mongo-driver/bson"
 | 
				
			||||||
 | 
						"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")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := collection.InsertOne(context.TODO(), LogEntry{
 | 
				
			||||||
 | 
							Name:      entry.Name,
 | 
				
			||||||
 | 
							Data:      entry.Data,
 | 
				
			||||||
 | 
							CreatedAt: time.Now(),
 | 
				
			||||||
 | 
							UpdatedAt: time.Now(),
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user