feat: implement mongo APIs

This commit is contained in:
Krispire Admin
2026-05-10 00:42:06 +05:30
parent b055b8ba0c
commit 3f09ea1fe5
4 changed files with 19 additions and 10 deletions
+6 -5
View File
@@ -34,17 +34,18 @@ type LetterDoc struct {
type LetterSummaryDoc struct {
ID bson.ObjectID `bson:"_id" json:"id"`
Slug string `bson:"slug" json:"slug"`
Recipients []string `bson:"recipients" json:"recipients"`
Recipients []string `bson:"recipients,omitempty" json:"recipients"`
RecipientDisplay string `bson:"recipientDisplay" json:"recipient_display"`
Date *string `bson:"date,omitempty" json:"date,omitempty"`
Year *int `bson:"year,omitempty" json:"year,omitempty"`
Month *int `bson:"month,omitempty" json:"month,omitempty"`
Day *int `bson:"day,omitempty" json:"day,omitempty"`
Location *string `bson:"location,omitempty" json:"location,omitempty"`
}
var letterSummaryProjection = bson.M{
"slug": 1, "recipients": 1, "recipientDisplay": 1,
"date": 1, "year": 1, "month": 1, "location": 1,
"date": 1, "year": 1, "month": 1, "day": 1, "location": 1,
}
func (r *LetterRepo) GetBySlug(ctx context.Context, slug string) (*LetterDoc, error) {
@@ -61,7 +62,7 @@ func (r *LetterRepo) GetBySlug(ctx context.Context, slug string) (*LetterDoc, er
func (r *LetterRepo) GetByYear(ctx context.Context, year int) ([]LetterSummaryDoc, error) {
opts := options.Find().
SetSort(bson.M{"month": 1, "day": 1}).
SetSort(bson.D{{Key: "month", Value: 1}, {Key: "day", Value: 1}}).
SetProjection(letterSummaryProjection)
cursor, err := r.col.Find(ctx, bson.M{"year": year}, opts)
if err != nil {
@@ -77,7 +78,7 @@ func (r *LetterRepo) GetByYear(ctx context.Context, year int) ([]LetterSummaryDo
func (r *LetterRepo) GetByRecipient(ctx context.Context, name string) ([]LetterSummaryDoc, error) {
opts := options.Find().
SetSort(bson.M{"year": 1, "month": 1, "day": 1}).
SetSort(bson.D{{Key: "year", Value: 1}, {Key: "month", Value: 1}, {Key: "day", Value: 1}}).
SetProjection(letterSummaryProjection)
cursor, err := r.col.Find(ctx, bson.M{"recipients": name}, opts)
if err != nil {
@@ -93,7 +94,7 @@ func (r *LetterRepo) GetByRecipient(ctx context.Context, name string) ([]LetterS
func (r *LetterRepo) GetByLocation(ctx context.Context, location string) ([]LetterSummaryDoc, error) {
opts := options.Find().
SetSort(bson.M{"year": 1, "month": 1, "day": 1}).
SetSort(bson.D{{Key: "year", Value: 1}, {Key: "month", Value: 1}, {Key: "day", Value: 1}}).
SetProjection(letterSummaryProjection)
cursor, err := r.col.Find(ctx, bson.M{"location": location}, opts)
if err != nil {