22from django .core .exceptions import ObjectDoesNotExist
33from .models import Exercise
44from .types import ExerciseType
5+ from .util import order_objects_by_position
56from enum import Enum
67
78class ExerciseCategory (Enum ):
@@ -21,6 +22,9 @@ class ExerciseInput(graphene.InputObjectType):
2122 position = graphene .Int ()
2223
2324
25+
26+
27+
2428class CreateExerciseMutation (graphene .Mutation ):
2529 exercise = graphene .Field (ExerciseType )
2630
@@ -29,17 +33,14 @@ class Arguments:
2933 category_enum = graphene .Argument (graphene .Enum .from_enum (ExerciseCategory ))
3034
3135 def mutate (self , info , exercise_data = None , category_enum = None ):
32- exercises = Exercise .objects .all ()
33- num_exercises = exercises .count ()
34- if (exercises is not None ):
35- last_exercise = exercises .order_by ('-position' ).first ()
36- if (last_exercise is not None ):
37- last_position = last_exercise .position
38- if (last_position is not None and last_position != num_exercises ):
39- for idx , val in enumerate (Exercise .objects ):
40- val .position = idx + 1
41- val .save ()
42-
36+ num_exercises = len (Exercise .objects )
37+ sort = False
38+ if (exercise_data .position is not None ):
39+ position = exercise_data .position
40+ sort = True
41+ else :
42+ position = num_exercises + 1
43+
4344 exercise = Exercise (
4445 unit = exercise_data .unit ,
4546 name = exercise_data .name ,
@@ -48,8 +49,66 @@ def mutate(self, info, exercise_data=None, category_enum=None):
4849 code = exercise_data .code ,
4950 test = exercise_data .test ,
5051 category = category_enum ,
51- position = num_exercises + 1
52+ position = position
5253 )
5354 exercise .save ()
55+ if (sort is True ):
56+ exercises = Exercise .objects .all ()
57+ order_objects_by_position (exercises )
58+
59+ return CreateExerciseMutation (exercise = exercise )
60+
61+
62+ class UpdateExerciseMutation (graphene .Mutation ):
63+ exercise = graphene .Field (ExerciseType )
64+
65+ class Arguments :
66+ exercise_data = ExerciseInput (required = True )
67+
68+ @staticmethod
69+ def get_object (id ):
70+ return Exercise .objects .get (pk = id )
71+
72+
73+ def mutate (self , info , exercise_data = None ):
74+ exercise = UpdateExerciseMutation .get_object (exercise_data .id )
75+ if (exercise_data is not exercise ):
76+ if exercise_data .unit :
77+ exercise .unit = exercise_data .unit
78+ if exercise_data .name :
79+ exercise .name = exercise_data .name
80+ if exercise_data .description :
81+ exercise .description = exercise_data .description
82+ if exercise_data .content :
83+ exercise .content = exercise_data .content
84+ if exercise_data .category :
85+ exercise .category = exercise_data .category
86+ if exercise_data .code :
87+ exercise .code = exercise_data .code
88+ if exercise_data .test :
89+ exercise .test = exercise_data .test
90+ if exercise_data .position :
91+ exercise .position = exercise_data .position
92+ exercise .save ()
93+ exercises = Exercise .objects .all ()
94+ order_objects_by_position (exercises )
95+
96+ exercise .save ()
97+
98+ return UpdateExerciseMutation (exercise = exercise )
99+
100+
101+ class DeleteExerciseMutation (graphene .Mutation ):
102+ class Arguments :
103+ id = graphene .ID (required = True )
104+
105+ success = graphene .Boolean ()
106+
107+ def mutate (self , info , id ):
108+ try :
109+ Exercise .objects .get (pk = id ).delete ()
110+ success = True
111+ except ObjectDoesNotExist :
112+ success = False
54113
55- return CreateExerciseMutation ( exercise = exercise )
114+ return DeleteExerciseMutation ( success = success )
0 commit comments