Skip to content

Commit

Permalink
Add star information to schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
basilfx committed Aug 7, 2017
1 parent 8405380 commit d5a1c0a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
50 changes: 49 additions & 1 deletion photod-backend/photod/api/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.urls import reverse
from django.shortcuts import get_object_or_404
from django.db.models import IntegerField, Case, Value, When
from django.contrib.auth import get_user_model

Expand Down Expand Up @@ -238,10 +239,12 @@ class Meta:
filmstrips = DjangoFilterConnectionField(
Filmstrip, filterset_class=FilmstripFilter)

url = graphene.String()

faces_count = graphene.Int()
locations_count = graphene.Int()

url = graphene.String()
starred = graphene.Boolean()

def resolve_url(self, args, context, info):
return reverse(media, args=[self.id])
Expand All @@ -252,6 +255,9 @@ def resolve_faces_count(self, args, context, info):
def resolve_locations_count(self, args, context, info):
return self.locations.count()

def resolve_starred(self, args, context, info):
return self.stars.filter(user=context.user).exists()


class User(DjangoObjectType):
class Meta:
Expand Down Expand Up @@ -311,6 +317,45 @@ def mutate(root, args, context, info):
])


class View(graphene.Mutation):
class Input:
id = graphene.ID()

count = graphene.Int()

@staticmethod
def mutate(root, args, context, info):
media_file = get_object_or_404(models.MediaFile, args.get("id"))

view, created = models.View.get_or_create(
media_file=media_file, user=context.user)
view.count += 1
view.save()

return View(result=view.count)


class Star(graphene.Mutation):
class Input:
id = graphene.ID(description="Identifier of media file.")
star = graphene.Boolean(description="True if star should be added.")

star = graphene.Boolean()

@staticmethod
def mutate(root, args, context, info):
media_file = get_object_or_404(models.MediaFile, args.get("id"))

if args.get("star"):
star, _ = models.Star.get_or_create(
media_file=media_file, user=context.user)
else:
models.Star.filter(
media_file=media_file, user=context.user).delete()

return Star(bool(args.get("star")))


class Query(graphene.ObjectType):
node = relay.Node.Field()

Expand Down Expand Up @@ -378,5 +423,8 @@ def resolve_me(self, args, context, info):
class Mutation(graphene.ObjectType):
search = Search.Field()

star = Star.Field()
view = View.Field()


schema = graphene.Schema(query=Query, mutation=Mutation)
2 changes: 2 additions & 0 deletions photod-frontend/src/components/Thumbnail.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type MediaFile = BaseMediaFile & {
duration: ?number,
facesCount: number,
locationsCount: number,
starred: boolean,
};

/**
Expand Down Expand Up @@ -318,6 +319,7 @@ Thumbnail.fragment = gql`
created
facesCount
locationsCount
starred
palette(first: 1) {
edges {
node {
Expand Down

0 comments on commit d5a1c0a

Please sign in to comment.