Exemplo n.º 1
0
        public PersonsQuery(IPersonRepository personRepository, IRelationRepository relationRepository)
        {
            Name = "PersonsRelationsQuery";

            Field <PersonsType>(
                "person",
                arguments: new QueryArguments(new QueryArgument <IntGraphType> {
                Name = "Id"
            }),
                resolve: context => personRepository.GetById(context.GetArgument <int>("Id"))
                );

            Field <ListGraphType <PersonsType> >(
                "persons", "Returns list of persons",
                resolve: context => personRepository.GetAll()
                );

            Field <PersonalRelationsType>(
                "relation",
                arguments: new QueryArguments(new QueryArgument <IntGraphType> {
                Name = "Id"
            }),
                resolve: context => relationRepository.GetById(context.GetArgument <int>("Id"))
                );

            Field <ListGraphType <PersonalRelationsType> >(
                "relations", "returns list of all persons relations",
                resolve: context => relationRepository.GetAll()
                );
        }
Exemplo n.º 2
0
        public Relation GetById(int id)
        {
            var entity = repository.GetById(id);

            return(entity);
        }
Exemplo n.º 3
0
        public PersonsMutation(IPersonRepository personRepository, IRelationRepository relationRepository)
        {
            Name = "PersonalRelationsMutation";

            #region Person
            FieldAsync <PersonsType>(
                "createPerson",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <PersonsInputType> > {
                Name = "person"
            }
                    ),
                resolve: async context =>
            {
                var personInput = context.GetArgument <Person>("person");
                return(await personRepository.Add(personInput));
                //return "Person has been created succesfully.";
            }
                );

            FieldAsync <PersonsType>(
                "updatePerson",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <PersonsInputType> > {
                Name = "person"
            },
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "personId"
            }
                    ),
                resolve: async context =>
            {
                var personInput = context.GetArgument <Person>("person");
                var personId    = context.GetArgument <int>("personId");

                var personInfoRetrived = await personRepository.GetById(personId);
                if (personInfoRetrived == null)
                {
                    context.Errors.Add(new ExecutionError("Couldn't find Person info."));
                    return(null);
                }
                personInfoRetrived.NatIdNr     = personInput.NatIdNr;
                personInfoRetrived.Nationality = personInput.Nationality;
                personInfoRetrived.Email       = personInput.Email;
                personInfoRetrived.FirstName   = personInput.FirstName;
                personInfoRetrived.LastName    = personInput.LastName;
                personInfoRetrived.DateOfBirth = personInput.DateOfBirth;
                personInfoRetrived.DateOfDeath = personInput.DateOfDeath;
                personInfoRetrived.Address     = personInput.Address;
                personInfoRetrived.Sex         = personInput.Sex;
                return(await personRepository.Update(personInfoRetrived));
                //return $"Person ID {personId} with Name {personInfoRetrived.FullName} has been updated succesfully.";
            }
                );

            FieldAsync <StringGraphType>(
                "deletePerson",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "personId"
            }),
                resolve: async context =>
            {
                var personId = context.GetArgument <int>("personId");

                var personInfoRetrived = await personRepository.GetById(personId);
                if (personInfoRetrived == null)
                {
                    context.Errors.Add(new ExecutionError("Couldn't find Person info."));
                    return(null);
                }
                await personRepository.Delete(personId);
                return($"Person ID {personId} with Name {personInfoRetrived.FullName} has been deleted succesfully.");
            }
                );
            #endregion


            #region Personal Relations
            FieldAsync <PersonalRelationsType>(
                "addRelation",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <PersonalRelationsInputType> > {
                Name = "relation"
            }),
                resolve: async context =>
            {
                var personalDisease = context.GetArgument <PersonalRelation>("relation");
                return(await relationRepository.Add(personalDisease));
                //return $"Relationship has been created succesfully.";
            }
                );

            FieldAsync <PersonalRelationsType>(
                "updateRelation",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <PersonalRelationsInputType> > {
                Name = "relation"
            },
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "relationId"
            }
                    ),
                resolve: async context =>
            {
                var relationInput = context.GetArgument <PersonalRelation>("relation");
                var relationId    = context.GetArgument <int>("relationId");

                var relationInfoRetrived = await relationRepository.GetById(relationId);
                if (relationInfoRetrived == null)
                {
                    context.Errors.Add(new ExecutionError("Couldn't find Relation info."));
                    return(null);
                }
                relationInfoRetrived.PersonId            = relationInput.PersonId;
                relationInfoRetrived.RelativeId          = relationInput.RelativeId;
                relationInfoRetrived.RelationType        = relationInput.RelationType;
                relationInfoRetrived.ReverseRelationType = relationInput.ReverseRelationType;
                return(await relationRepository.Update(relationInfoRetrived));
                //return $"Relation ID {relationId} has been updated succesfully.";
            }
                );

            FieldAsync <StringGraphType>(
                "deleteRelation",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "relationId"
            }),
                resolve: async context =>
            {
                var relationId = context.GetArgument <int>("relationId");

                var relationInfoRetrived = await relationRepository.GetById(relationId);
                if (relationInfoRetrived == null)
                {
                    context.Errors.Add(new ExecutionError("Couldn't find Personal Relation info."));
                    return(null);
                }
                await relationRepository.Delete(relationId);
                return($"Personal Relation ID {relationId} has been deleted succesfully.");
            }
                );
            #endregion
        }