From bcd2135fb8afe0eedbd90b4345f86cdfcee182da Mon Sep 17 00:00:00 2001 From: prabhuomkar Date: Sun, 14 Nov 2021 07:45:19 +0530 Subject: [PATCH] Rebased with master --- api/internal/graph/generated/generated.go | 156 ++++++++++++++++++ .../graph/resolvers/mediaitem.resolvers.go | 47 ++++++ api/internal/models/models_gen.go | 5 + api/schema/mediaitem.graphql | 6 + 4 files changed, 214 insertions(+) diff --git a/api/internal/graph/generated/generated.go b/api/internal/graph/generated/generated.go index 15e4c73..15ae4b6 100644 --- a/api/internal/graph/generated/generated.go +++ b/api/internal/graph/generated/generated.go @@ -130,6 +130,11 @@ type ComplexityRoot struct { Upload func(childComplexity int, file graphql.Upload) int } + OnThisDayResponse struct { + MediaItems func(childComplexity int) int + Year func(childComplexity int) int + } + Photo struct { ApertureFNumber func(childComplexity int) int CameraMake func(childComplexity int) int @@ -150,6 +155,7 @@ type ComplexityRoot struct { Favourites func(childComplexity int, page *int, limit *int) int MediaItem func(childComplexity int, id string) int MediaItems func(childComplexity int, page *int, limit *int) int + OnThisDay func(childComplexity int) int Search func(childComplexity int, q *string, id *string, page *int, limit *int) int } } @@ -181,6 +187,7 @@ type QueryResolver interface { Entity(ctx context.Context, id string) (*models.Entity, error) MediaItem(ctx context.Context, id string) (*models.MediaItem, error) MediaItems(ctx context.Context, page *int, limit *int) (*models.MediaItemConnection, error) + OnThisDay(ctx context.Context) ([]*models.OnThisDayResponse, error) Search(ctx context.Context, q *string, id *string, page *int, limit *int) (*models.MediaItemConnection, error) Autocomplete(ctx context.Context, q string) ([]*models.AutocompleteResponse, error) Favourites(ctx context.Context, page *int, limit *int) (*models.MediaItemConnection, error) @@ -602,6 +609,20 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Mutation.Upload(childComplexity, args["file"].(graphql.Upload)), true + case "OnThisDayResponse.mediaItems": + if e.complexity.OnThisDayResponse.MediaItems == nil { + break + } + + return e.complexity.OnThisDayResponse.MediaItems(childComplexity), true + + case "OnThisDayResponse.year": + if e.complexity.OnThisDayResponse.Year == nil { + break + } + + return e.complexity.OnThisDayResponse.Year(childComplexity), true + case "Photo.apertureFNumber": if e.complexity.Photo.ApertureFNumber == nil { break @@ -759,6 +780,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Query.MediaItems(childComplexity, args["page"].(*int), args["limit"].(*int)), true + case "Query.onThisDay": + if e.complexity.Query.OnThisDay == nil { + break + } + + return e.complexity.Query.OnThisDay(childComplexity), true + case "Query.search": if e.complexity.Query.Search == nil { break @@ -945,9 +973,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 { @@ -3720,6 +3754,38 @@ func (ec *executionContext) _Query_mediaItems(ctx context.Context, field graphql return ec.marshalNMediaItemConnection2ᚖirisᚋapiᚋinternalᚋmodelsᚐMediaItemConnection(ctx, field.Selections, res) } +func (ec *executionContext) _Query_onThisDay(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "Query", + Field: field, + Args: nil, + IsMethod: true, + IsResolver: true, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Query().OnThisDay(rctx) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]*models.OnThisDayResponse) + fc.Result = res + return ec.marshalOOnThisDayResponse2ᚕᚖirisᚋapiᚋinternalᚋmodelsᚐOnThisDayResponseᚄ(ctx, field.Selections, res) +} + func (ec *executionContext) _Query_search(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -5581,6 +5647,35 @@ func (ec *executionContext) _Mutation(ctx context.Context, sel ast.SelectionSet) return out } +var onThisDayResponseImplementors = []string{"OnThisDayResponse"} + +func (ec *executionContext) _OnThisDayResponse(ctx context.Context, sel ast.SelectionSet, obj *models.OnThisDayResponse) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, onThisDayResponseImplementors) + + out := graphql.NewFieldSet(fields) + var invalids uint32 + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("OnThisDayResponse") + case "year": + out.Values[i] = ec._OnThisDayResponse_year(ctx, field, obj) + if out.Values[i] == graphql.Null { + invalids++ + } + case "mediaItems": + out.Values[i] = ec._OnThisDayResponse_mediaItems(ctx, field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch() + if invalids > 0 { + return graphql.Null + } + return out +} + var photoImplementors = []string{"Photo"} func (ec *executionContext) _Photo(ctx context.Context, sel ast.SelectionSet, obj *models.Photo) graphql.Marshaler { @@ -5728,6 +5823,17 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr } return res }) + case "onThisDay": + field := field + out.Concurrently(i, func() (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Query_onThisDay(ctx, field) + return res + }) case "search": field := field out.Concurrently(i, func() (res graphql.Marshaler) { @@ -6189,6 +6295,16 @@ func (ec *executionContext) marshalNMediaItemConnection2ᚖirisᚋapiᚋinternal return ec._MediaItemConnection(ctx, sel, v) } +func (ec *executionContext) marshalNOnThisDayResponse2ᚖirisᚋapiᚋinternalᚋmodelsᚐOnThisDayResponse(ctx context.Context, sel ast.SelectionSet, v *models.OnThisDayResponse) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + return ec._OnThisDayResponse(ctx, sel, v) +} + func (ec *executionContext) unmarshalNString2string(ctx context.Context, v interface{}) (string, error) { res, err := graphql.UnmarshalString(v) return res, graphql.ErrorOnPath(ctx, err) @@ -6703,6 +6819,46 @@ func (ec *executionContext) marshalOMediaMetaData2ᚖirisᚋapiᚋinternalᚋmod return ec._MediaMetaData(ctx, sel, v) } +func (ec *executionContext) marshalOOnThisDayResponse2ᚕᚖirisᚋapiᚋinternalᚋmodelsᚐOnThisDayResponseᚄ(ctx context.Context, sel ast.SelectionSet, v []*models.OnThisDayResponse) graphql.Marshaler { + if v == nil { + return graphql.Null + } + ret := make(graphql.Array, len(v)) + var wg sync.WaitGroup + isLen1 := len(v) == 1 + if !isLen1 { + wg.Add(len(v)) + } + for i := range v { + i := i + fc := &graphql.FieldContext{ + Index: &i, + Result: &v[i], + } + ctx := graphql.WithFieldContext(ctx, fc) + f := func(i int) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = nil + } + }() + if !isLen1 { + defer wg.Done() + } + ret[i] = ec.marshalNOnThisDayResponse2ᚖirisᚋapiᚋinternalᚋmodelsᚐOnThisDayResponse(ctx, sel, v[i]) + } + if isLen1 { + f(i) + } else { + go f(i) + } + + } + wg.Wait() + return ret +} + func (ec *executionContext) marshalOPhoto2ᚖirisᚋapiᚋinternalᚋmodelsᚐPhoto(ctx context.Context, sel ast.SelectionSet, v *models.Photo) graphql.Marshaler { if v == nil { return graphql.Null diff --git a/api/internal/graph/resolvers/mediaitem.resolvers.go b/api/internal/graph/resolvers/mediaitem.resolvers.go index 91b9fcd..9f04aa0 100644 --- a/api/internal/graph/resolvers/mediaitem.resolvers.go +++ b/api/internal/graph/resolvers/mediaitem.resolvers.go @@ -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" @@ -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} } diff --git a/api/internal/models/models_gen.go b/api/internal/models/models_gen.go index 7012430..6c3fbeb 100644 --- a/api/internal/models/models_gen.go +++ b/api/internal/models/models_gen.go @@ -34,6 +34,11 @@ type MediaItemConnection struct { TotalCount int `json:"totalCount"` } +type OnThisDayResponse struct { + Year int `json:"year"` + MediaItems []*MediaItem `json:"mediaItems"` +} + type UpdateAlbumInput struct { Name string `json:"name"` Description *string `json:"description"` diff --git a/api/schema/mediaitem.graphql b/api/schema/mediaitem.graphql index ca80489..761a657 100644 --- a/api/schema/mediaitem.graphql +++ b/api/schema/mediaitem.graphql @@ -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 {