view calsync/mongoclient/mongoclient.go @ 51:a9b720445bcf

now roughly syncs cals and events to mongodb, one time
author drewp@bigasterisk.com
date Mon, 19 Aug 2024 14:42:27 -0700
parents dade5bbd02e3
children 1f84bd5ce8e0
line wrap: on
line source

package mongoclient

import (
	"context"
	"log"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type MongoClient struct {
	ctx              context.Context
	client           *mongo.Client
	db               *mongo.Database
	calsCollection   *mongo.Collection
	eventsCollection *mongo.Collection
}

// docs in eventsCollection
type MongoEvent struct {
	// e.g.
	// {
	// 	"_id" : "http://bigasterisk.com/calendar/08s5m1_20140929T030000Z",
	// 	"htmlLink" : "https://www.google.com/calendar/event?eid=MDhz",
	// 	"title" : "meeting",
	// 	"feedId" : "drewpca@gmail.com",
	// 	"feedTitle" : "drewpca@gmail.com",
	// 	"endTimeUnspecified" : false,
	// 	"start" : "2014-09-28T20:00:00-07:00",
	// 	"startDate" : "2014-09-28",
	// 	"startTime" : ISODate("2014-09-29T03:00:00Z"),
	// 	"end" : "2014-09-28T21:00:00-07:00",
	// 	"endDate" : "2014-09-28",
	// 	"endTime" : ISODate("2014-09-29T04:00:00Z")
	// }

	Url                string    `bson:"_id"`
	GoogleId           string    `bson:"googleId"`
	CalendarUrl        string    `bson:"calendarUrl"`
	HtmlLink           string    `bson:"htmlLink"`
	Title              string    `bson:"title"`
	EndTimeUnspecified bool      `bson:"endTimeUnspecified"`
	Start              string    `bson:"start"`
	StartDate          string    `bson:"startDate"`
	StartTime          time.Time `bson:"startTime"`
	End                string    `bson:"end"`
	EndDate            string    `bson:"endDate"`
	EndTime            time.Time `bson:"endTime"`
	LastUpdated        time.Time `bson:"lastUpdated"`
}

func New(ctx context.Context) (*MongoClient, error) {
	log.Println("todo: using fixed ip")
	mclient, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://10.43.98.221:27017"))
	if err != nil {
		return nil, err
	}
	db := mclient.Database("pim")
	return &MongoClient{
			ctx,
			mclient,
			db,
			db.Collection("test_gcalendar_cals"),
			db.Collection("test_gcalendar")},
		nil
}

func (c *MongoClient) Close() {
	c.client.Disconnect(c.ctx)
}