Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

#77: Added On This Day Query #80

Merged
merged 2 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions api/internal/graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions api/internal/graph/resolvers/mediaitem.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"iris/api/internal/graph/generated"
"iris/api/internal/models"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand Down Expand Up @@ -134,6 +135,52 @@ func (r *queryResolver) MediaItems(ctx context.Context, page *int, limit *int) (
}, nil
}

func (r *queryResolver) OnThisDay(ctx context.Context) ([]*models.OnThisDayResponse, error) {
matchStage := bson.D{{Key: "$match", Value: bson.D{{
Key: "$expr", Value: bson.D{{
Key: "$and", Value: bson.A{
bson.D{{Key: "$eq", Value: bson.A{
bson.D{{Key: "$dayOfMonth", Value: "$mediaMetadata.creationTime"}},
bson.D{{Key: "$dayOfMonth", Value: time.Now()}},
}}},
bson.D{{Key: "$eq", Value: bson.A{
bson.D{{Key: "$month", Value: "$mediaMetadata.creationTime"}},
bson.D{{Key: "$month", Value: time.Now()}},
}}},
},
}},
}}}}
groupStage := bson.D{{Key: "$group", Value: bson.D{
{
Key: "_id", Value: bson.D{{
Key: "year", Value: bson.D{{Key: "$year", Value: "$mediaMetadata.creationTime"}},
}},
},
{
Key: "mediaItems", Value: bson.D{{
Key: "$push", Value: "$$ROOT",
}},
},
}}}

cur, err := r.DB.Collection(models.ColMediaItems).Aggregate(ctx, mongo.Pipeline{
matchStage,
groupStage,
bson.D{{Key: "$sort", Value: bson.D{{Key: "_id", Value: -1}}}},
})
if err != nil {
return nil, err
}

var result []*models.OnThisDayResponse

if err = cur.All(ctx, &result); err != nil {
return nil, err
}

return result, nil
}

// MediaItem returns generated.MediaItemResolver implementation.
func (r *Resolver) MediaItem() generated.MediaItemResolver { return &mediaItemResolver{r} }

Expand Down
5 changes: 5 additions & 0 deletions api/internal/models/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions api/schema/mediaitem.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ type MediaItemConnection {
totalCount: Int!
}

type OnThisDayResponse {
year: Int!
mediaItems: [MediaItem!]
}

extend type Query {
mediaItem(id: String!): MediaItem!
mediaItems(page: Int, limit: Int): MediaItemConnection!
onThisDay: [OnThisDayResponse!]
}

extend type Mutation {
Expand Down