You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

import jwt
import strawberry
from django.conf import settings
from django.contrib.auth.models import User
from strawberry import ID
from strawberry.types import Info
from app.models import Task
from app.schema.types import TaskType
from articles.utils import GraphQLError, IsAuthenticated
@strawberry.type
class Mutation:
@strawberry.mutation(permission_classes=[IsAuthenticated])
def add_task(self, title: str, description: str, info: Info) -> TaskType:
task = Task.objects.create(title=title, description=description, user=info.context.user)
return task
@strawberry.mutation(permission_classes=[IsAuthenticated])
def change_done(self, task_id: ID) -> TaskType:
try:
task = Task.objects.get(id=task_id)
task.is_done = not task.is_done
task.save()
return task
except Task.DoesNotExist:
raise GraphQLError('Task not found')
@strawberry.mutation(permission_classes=[IsAuthenticated])
def delete_task(self, task_id: ID) -> bool:
try:
task = Task.objects.get(id=task_id)
task.delete()
return True
except Task.DoesNotExist:
raise GraphQLError('Task not found')
@strawberry.mutation
def sign_up(self, username: str, password: str) -> bool:
if User.objects.filter(username=username).exists():
raise GraphQLError('Username is unavailable')
user = User(username=username)
user.set_password(password)
user.save()
return True
@strawberry.mutation
def sign_in(self, username: str, password: str) -> str:
try:
user = User.objects.get(username=username)
if user.check_password(password):
token = jwt.encode({'id': user.pk}, settings.SECRET_KEY, algorithm='HS256')
return token
else:
raise User.DoesNotExist
except User.DoesNotExist:
raise GraphQLError('Username and/or password are incorrect')