client.py 3.18 KB
Newer Older
ymir's avatar
ymir committed
1
2
# coding=utf-8
# import
ymir's avatar
ymir committed
3
from graphene import Field, List, NonNull, String, Mutation, ID, InputObjectType, Boolean
ymir's avatar
ymir committed
4
from dummypythongraphqlserver.entities import Client
ymir's avatar
ymir committed
5
from dummypythongraphqlserver.graphQL.node.client import NodeClientField
ymir's avatar
ymir committed
6
from dummypythongraphqlserver.graphQL.node.purchaseOrder import NodePurchaseOrderField
ymir's avatar
ymir committed
7
from dummypythongraphqlserver.graphQL.root import ReadInput, DeleteInput, UpdateInput
ymir's avatar
ymir committed
8
from dummypythongraphqlserver.dataAccess.client import ClientDAO
ymir's avatar
ymir committed
9
# input
ymir's avatar
ymir committed
10
class DeleteClientsInput(DeleteInput, InputObjectType):
ymir's avatar
ymir committed
11
12
    firstName = String()
    lastName = String()
ymir's avatar
ymir committed
13
class ReadClientsInput(ReadInput, DeleteClientsInput):
ymir's avatar
ymir committed
14
    pass
ymir's avatar
ymir committed
15
class CreateClientsInput(InputObjectType):
ymir's avatar
ymir committed
16
17
18
    firstName = String(required=True)
    lastName = String(required=True)
    addressId = ID(required=True)
ymir's avatar
ymir committed
19
class UpdateClientsInput(UpdateInput, CreateClientsInput):
ymir's avatar
ymir committed
20
    pass
ymir's avatar
ymir committed
21
# query
ymir's avatar
ymir committed
22
class RootClientField(NodeClientField):
ymir's avatar
ymir committed
23
    purchaseOrders = Field(List(NonNull(NodePurchaseOrderField)), required=True)
ymir's avatar
ymir committed
24
    def resolve_purchaseOrders(self,_):
ymir's avatar
ymir committed
25
        return NodePurchaseOrderField.resolvePurchaseOrders([self.id])
ymir's avatar
ymir committed
26
    pass
ymir's avatar
ymir committed
27
# mutation
ymir's avatar
ymir committed
28
29
class DeleteClients(Mutation):
    class Arguments:
ymir's avatar
ymir committed
30
        deleteClientsInput = DeleteClientsInput(required=True)
ymir's avatar
ymir committed
31
32
    # INFO : at least 1 return field is needed by graphene framework
    confirmation = Boolean(required=True)
ymir's avatar
ymir committed
33
34
    def mutate(self, _, deleteClientsInput):
        ClientDAO.delete(ids=deleteClientsInput.ids, firstName=deleteClientsInput.firstName, lastName=deleteClientsInput.lastName)
ymir's avatar
ymir committed
35
        return DeleteClients(confirmation=True)
ymir's avatar
ymir committed
36
    pass
ymir's avatar
ymir committed
37
38
class CreateClients(Mutation):
    class Arguments:
ymir's avatar
ymir committed
39
        createClientsInput = List(NonNull(CreateClientsInput), required=True)
ymir's avatar
ymir committed
40
        withAddress = Boolean()
ymir's avatar
ymir committed
41
    readClients = Field(List(NonNull(NodeClientField)), required=True)
ymir's avatar
ymir committed
42
43
44
    '''INFO : keep all parameters, even the ones you do not use later
    they will be used by graphen framework
    i.e : withAddress, withClient(s), withPurchaseOrders, withOrderLines'''
ymir's avatar
ymir committed
45
    def mutate(self, _, createClientsInput, withAddress=True):
ymir's avatar
ymir committed
46
        # create each client (and associated address id)
ymir's avatar
ymir committed
47
48
49
50
        entitiesClientsInputs = dict()
        for lineClientsInput in createClientsInput:
            entityClient = Client(firstName=lineClientsInput.firstName, lastName=lineClientsInput.lastName)
            entitiesClientsInputs[entityClient] = lineClientsInput.addressId
ymir's avatar
ymir committed
51
            pass
ymir's avatar
ymir committed
52
        clientOutputs = ClientDAO.create(entitiesClientsInputs)
ymir's avatar
ymir committed
53
        return CreateClients(readClients=clientOutputs)
ymir's avatar
ymir committed
54
    pass
ymir's avatar
ymir committed
55
56
class UpdateClients(Mutation):
    class Arguments:
ymir's avatar
ymir committed
57
        updateClientsInput = List(NonNull(UpdateClientsInput), required=True)
ymir's avatar
ymir committed
58
    confirmation = Boolean(required=True)
ymir's avatar
ymir committed
59
    def mutate(self, _, updateClientsInput):
ymir's avatar
ymir committed
60
        # update each client (and associated address id)
ymir's avatar
ymir committed
61
62
63
64
        entitiesClientsInputs = dict()
        for lineClientsInput in updateClientsInput:
            entityClient = Client(id=lineClientsInput.id, firstName=lineClientsInput.firstName, lastName=lineClientsInput.lastName)
            entitiesClientsInputs[entityClient] = lineClientsInput.addressId
ymir's avatar
ymir committed
65
            pass
ymir's avatar
ymir committed
66
        ClientDAO.update(entitiesClientsInputs)
ymir's avatar
ymir committed
67
68
        return UpdateClients(confirmation=True)
    pass
ymir's avatar
ymir committed
69
pass