コード例 #1
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            if (PersonRepository.Get(id) != null)
            {
                var personResult = PersonRepository.Get(id);
                var personObj    = new Person
                {
                    Id         = personResult.Id,
                    FirstName  = personResult.FirstName,
                    LastName   = personResult.LastName,
                    Enabled    = personUpdate.Enabled,
                    Authorised = personUpdate.Authorised,
                    Colours    = personUpdate.Colours
                };
                //if update is successful, return the person variable.
                return(Ok(personObj));
            }
            else
            {
                return(NotFound()); //return notfound if the variable is null.
            }
        }
コード例 #2
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            try
            {
                var oldPerson = PersonRepository.Get(id);
                if (oldPerson == null)
                {
                    return(NotFound());
                }

                oldPerson.Authorised = personUpdate.Authorised;
                oldPerson.Enabled    = personUpdate.Enabled;
                oldPerson.Colours    = personUpdate.Colours;

                return(Ok(PersonRepository.Update(oldPerson)));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to get person"));
            }
        }
コード例 #3
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            Person personByID = PersonRepository.Get(id);

            if (personByID != null)
            {
                personByID.Authorised = personUpdate.Authorised;
                personByID.Enabled    = personUpdate.Enabled;
                personByID.Colours    = personUpdate.Colours;

                return(new JsonResult(PersonRepository.Update(personByID)));
            }
            else
            {
                return(new NotFoundResult());
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            #region Test
            //Account a1 = new Account("Harry");
            //Account a2 = new Account("Ron");
            //SpyNotifications spy = new SpyNotifications(a2);
            //a1.Send(a1.CreateMessage("Hi, Ann! How are you?", a2));
            //a1.Send(a1.CreateMessage("I'm Alex, from Kyiv", a2));
            //a2.Send(a2.CreateMessage("Hi, Alex! I'm from Lviv", a1));
            //a1.ShowDialog(a2.Username);
            #endregion

            Lesson lesson = new Lesson();
            lesson.OnStart       += OnStart_Program;
            lesson.OnLessonEvent += Method;
            lesson.OnLessonEvent += (sender, e) => Console.WriteLine("Anonym on event");
            OnLessonStart onLesson = () => Console.WriteLine("in object");
            lesson?.OnStart?.Invoke();

            MathOperation plus     = (a, b) => a + b;
            MathOperation minus    = (a, b) => a - b;
            MathOperation miltiply = (a, b) => a * b;
            MathOperation divide   = (arg1, arg2) => arg1 / arg2;

            PersonUpdate update = p => p.Q++; // 1 in chain
            update += (p) => p.Age--;         // 2 in chain
            update += update;                 // 4 in chain
            update += update;                 // 8 in chain
            update += (p) => Console.WriteLine(p.ToString());

            Person person = new Person("QWERTY", 25, 0);
            update(person);
        }
コード例 #5
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            var person = PersonRepository.Get(id);

            if (person != null)
            {
                person.Authorised = personUpdate.Authorised;
                person.Enabled    = personUpdate.Enabled;
                person.Colours    = personUpdate.Colours;

                var updatedPerson = PersonRepository.Update(person);
                if (updatedPerson != null)
                {
                    return(Ok(updatedPerson));
                }
            }
            return(NotFound());
        }
コード例 #6
0
        public IActionResult PutPerson(int id, PersonUpdate model)
        {
            var choose = _context.Person.Find(id);

            choose.InjectFrom(model);
            _context.SaveChanges();
            return(NoContent());
        }
コード例 #7
0
        /// <summary>
        ///     Creates a person asyncronously.
        /// </summary>
        /// <param name="update">The values used to create the person</param>
        /// <param name="cancellationToken">Token allowing the request to be cancelled</param>
        /// <returns>The full represenation of the created person and their precinct</returns>
        public async Task <CreatePersonResponse> CreateAsync(PersonUpdate update, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(update, nameof(update));

            var url     = UrlProvider.GetV1PersonCreateUrl();
            var content = ObjectChangesJsonSerializer.SerializeChangesToJson(null, new CreatePersonRequest(update));

            return((await PostJsonAsync <CreatePersonResponse>(url, content, cancellationToken, true)).Payload);
        }
コード例 #8
0
        /// <summary>
        ///     Updates a person with the provided id to have the provided data syncronously. It returns a full representation of
        ///     the updated person.
        /// </summary>
        /// <param name="personId">The ID of the person to update</param>
        /// <param name="update">The values to update</param>
        /// <param name="cancellationToken">Token allowing the request to be cancelled</param>
        /// <returns>The full represenation of the updated person and their precinct</returns>
        public UpdatePersonResponse Update(int personId, PersonUpdate update,
                                           CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(update, nameof(update));

            var url     = UrlProvider.GetV1PersonUpdateUrl(personId);
            var content = ObjectChangesJsonSerializer.SerializeChangesToJson(null, new UpdatePersonRequest(update));

            return(PutJson <UpdatePersonResponse>(url, content, cancellationToken, true).Payload);
        }
コード例 #9
0
        public IActionResult Update(PersonUpdate personUpdate)
        {
            var result = _personUpdateService.Update(personUpdate);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
コード例 #10
0
        /// <summary>
        ///     Attempts to match the input person resource to a person already in the nation asyncronously.
        ///     If a match is found, the matched person is updated (without overriding data).
        ///     If a match is not found, a new person is created. Matches are found using one of the following IDs:
        ///     CiviCrmId, CountyFileId, CatalistId, ExternalId, Email, FacebookUsername, NgpId, SalesForceId, TwitterLogin, VanId.
        /// </summary>
        /// <param name="update">The values to update / match on</param>
        /// <param name="cancellationToken">Token allowing the request to be cancelled</param>
        /// <returns>The full represenation of the updated person, their precinct and whether they were created or updated</returns>
        public async Task <AddPersonResponse> AddAsync(PersonUpdate update,
                                                       CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(update, nameof(update));

            var url      = UrlProvider.GetV1PersonAddUrl();
            var content  = ObjectChangesJsonSerializer.SerializeChangesToJson(null, new UpdatePersonRequest(update));
            var response = await PutJsonAsync <AddPersonResponse>(url, content, cancellationToken, true);

            response.Payload.WasNewPersonCreated = response.HttpStatusCode == HttpStatusCode.Created;
            return(response.Payload);
        }
コード例 #11
0
            public void PutsCorrectUrl()
            {
                var connection = Substitute.For <IApiConnection>();
                var client     = new PersonsClient(connection);

                var editPerson = new PersonUpdate {
                    Name = "name"
                };

                client.Edit(123, editPerson);

                connection.Received().Put <Person>(Arg.Is <Uri>(u => u.ToString() == "persons/123"),
                                                   Arg.Is <PersonUpdate>(d => d.Name == "name"));
            }
コード例 #12
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person
            //using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the
            //person should be returned
            //from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            //dont understand the method is not written to receive a json object but instead an int and personUpdate
            //do i need to be turning the data i send into id and person update?



            // personUpdate = Newtonsoft.Json.JsonConvert.DeserializeObject<PersonUpdate>(personUpdate);



            var personToUpdate = PersonRepository.Get(id);


            {
                Person personWithUpdates = new Person
                {
                    Id         = id,
                    FirstName  = personToUpdate.FirstName,
                    LastName   = personToUpdate.LastName,
                    Authorised = personUpdate.Authorised,
                    Enabled    = personUpdate.Enabled,
                    Colours    = personUpdate.Colours
                };
                Person callbackFromUpdate = PersonRepository.Update(personWithUpdates);
                if (callbackFromUpdate == null)
                {
                    return(NotFound());
                }
                else
                {
                    return(Ok(callbackFromUpdate));
                };
            }

            throw new NotImplementedException();
        }
コード例 #13
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            bool personFound = this.PersonRepository.Update(personUpdate);

            //throw new NotImplementedException();
        }
コード例 #14
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            //get person from id.
            var person = this.PersonRepository.Get(id);

            if (person != null)
            {
                //check if relevant (changable on the application) values are altered, if so - amend the person's details.
                if (person.Authorised != personUpdate.Authorised)
                {
                    person.Authorised = personUpdate.Authorised;
                }
                if (person.Enabled != personUpdate.Enabled)
                {
                    person.Enabled = personUpdate.Enabled;
                }
                if (person.Colours != personUpdate.Colours)
                {
                    person.Colours = personUpdate.Colours;
                }
                //Use update function on person from specified id.
                var updatedPerson = this.PersonRepository.Update(person);

                if (updatedPerson != null)
                {
                    return(this.Ok(updatedPerson));
                }
                else
                {
                    return(this.NotFound());
                }
            }
            else
            {
                //if null return notFound.
                return(this.NotFound());
            }
        }
コード例 #15
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            System.Diagnostics.Debug.WriteLine(personUpdate.ToString());

            //// Get a Person istance from PersonRepository with Id id
            Person p = PersonRepository.Get(id);

            //// Check if the Person p is null
            if (p == null)
            {
                //// If p is null the return NotFound
                return(NotFound());
            }


            //// If not null will update p's values for Authorised, Enabled and Colours
            /// with those from personUpdaye
            p.Authorised = personUpdate.Authorised;
            p.Enabled    = personUpdate.Enabled;
            p.Colours    = personUpdate.Colours;

            //// Get a Person istance from PersonRepository when doing Update
            p = PersonRepository.Update(p);

            //// Check if the Person p is not null
            if (p != null)
            {
                //// If p is not null the return the Json instance of p
                return(new JsonResult(p));
            }

            //// Otherwise return NotFound
            return(NotFound());
        }
コード例 #16
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            // Anna Lycett:
            // I believe that this would involve a statement not
            // unlike PeopleRepository.Person[id] = <value to update
            // the object>.
            // There will also need to be a null check, which if
            // true, would return 'NotFound' as specified in the
            // instructions.


            throw new NotImplementedException();
        }
コード例 #17
0
        static void Main(string[] args)
        {
            #region Test
            Account a1        = new Account("Harry");
            Account a2        = new Account("Ron");
            Account a3        = new Account("Wilhelm");
            Account a4        = new Account("Barry");
            Group   testGroup = new Group("Testgroup", a4);

            SpyNotifications spy = new SpyNotifications(a2);
            a1.SendToGroup(a1.CreateGroupMessage("Hi, Ann! How are you?", testGroup), testGroup);
            a1.SendToGroup(a1.CreateGroupMessage("I'm Alex, from Kyiv", testGroup), testGroup);
            a2.SendToGroup(a2.CreateGroupMessage("Hi, Alex! I'm from Lviv", testGroup), testGroup);
            a1.ShowDialog(a2.Username);
            #endregion

            //Lesson lesson = new Lesson();
            //lesson.OnStart += OnStart_Program;
            //lesson.OnLessonEvent += Method;
            //lesson.OnLessonEvent += (sender, e) => Console.WriteLine("Anonym on event");
            //OnLessonStart onLesson = () => Console.WriteLine("in object");
            //lesson?.OnStart?.Invoke();

            //MathOperation plus = (a, b) => a + b;
            //MathOperation minus = (a, b) => a - b;
            //MathOperation miltiply = (a, b) => a * b;
            //MathOperation divide = (arg1, arg2) => arg1 / arg2;

            PersonUpdate update = (p) => p.Q++; // 1 in chain
            update += (p) => p.Age--;           // 2 in chain
            update += update;                   // 4 in chain
            update += update;                   // 8 in chain
            update += (p) => Console.WriteLine(p.ToString());

            //Person person = new Person("QWERTY", 25, 0);
            //update(person);
        }
コード例 #18
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.

            // get the original person
            Person existingPerson = PersonRepository.Get(id);

            // check we have them
            if (existingPerson == null)
            {
                return(this.NotFound());
            }

            // The PersonRepository needs a Person, so create a new person with the updated details
            Person updatedPerson = new Person()
            {
                Id         = existingPerson.Id,
                FirstName  = existingPerson.FirstName,
                LastName   = existingPerson.LastName,
                Authorised = personUpdate.Authorised,
                Enabled    = personUpdate.Enabled,
                Colours    = personUpdate.Colours
            };

            // send the updated details to the Repository
            var response = PersonRepository.Update(updatedPerson);

            return(this.Ok(response));
        }
コード例 #19
0
 public IResult Update(PersonUpdate personUpdate)
 {
     _personUpdateDal.Update(personUpdate);
     return(new SuccessResult());
 }
コード例 #20
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.


            /*
             * BUG: appends new colours onto old this.person.colours list
             *
             * I have spotted a bug in the where by if you change the persons favorite colour
             * it submits the old colour and the new colour.
             * I have tried to fix this problem in a number of ways.
             *
             * below  I tried to implement a solution that works for some cases.
             *
             * SOLUTION:
             * check if the colours list is the same as the updated list if it is the same then update the perosn but leave the colours.
             * if it is not the same then cut  off the first extraneous element. and update the person object with the new colours.
             *
             * I am aware there is some issues with the solution for example when you want to change all three options to a singluar option
             * it does not work, I know there must be a more elegant and simple solution and would love to talk more about what needs to be done.
             *
             * if I had a similar problem in the future I would ask a team memeber to help me out with fixing the issue.
             *
             */



            var allPeople        = PersonRepository.GetAll();
            var PersonOfInterest = this.getPersonByID(allPeople, id);

            var colours      = personUpdate.Colours.ToList();
            var firstelement = colours[0];

            if (colours.Count > 1)
            {
                colours.Remove(firstelement);
            }

            foreach (var colour1 in PersonOfInterest.Colours)
            {
                foreach (var colour2 in colours)
                {
                    if (colour1.Name == colour2.Name)
                    {
                        PersonOfInterest.Authorised = personUpdate.Authorised;
                        PersonOfInterest.Enabled    = personUpdate.Enabled;
                        //PersonOfInterest.Colours = personUpdate.Colours;

                        if (PersonOfInterest != null)
                        {
                            return(Content(Jss.Serialize(PersonOfInterest)));
                        }
                        else
                        {
                            return(NotFound());
                        }
                    }
                    else
                    {
                        PersonOfInterest.Authorised = personUpdate.Authorised;
                        PersonOfInterest.Enabled    = personUpdate.Enabled;
                        PersonOfInterest.Colours    = colours;

                        if (PersonOfInterest != null)
                        {
                            return(Content(Jss.Serialize(PersonOfInterest)));
                        }
                        else
                        {
                            return(NotFound());
                        }
                    }
                }
            }
            return(NotFound());
        }
コード例 #21
0

        
コード例 #22
0
        public IActionResult Update(int id, PersonUpdate personUpdate)
        {
            // TODO: Step 3
            //
            // Implement an endpoint that receives a JSON object to
            // update a person using the PeopleRepository based on
            // the id parameter. Once the person has been successfully
            // updated, the person should be returned from the endpoint.
            // If null is returned from the PeopleRepository then a
            // NotFound should be returned.



            if (!ModelState.IsValid)
            {
                return(NotFound());
            }

            //get existing person
            var    ExistingPerson = PersonRepository.Get(id);
            Person UpdatedPerson;

            if (ExistingPerson == null)
            {
                return(NotFound());
            }

            //update with limited properties
            ExistingPerson.Authorised = personUpdate.Authorised;
            ExistingPerson.Enabled    = personUpdate.Enabled;
            ExistingPerson.Colours    = personUpdate.Colours;

            //Update method call
            UpdatedPerson = PersonRepository.Update(ExistingPerson);


            // EntityState.Modified not needed as no db context

            //return correct HttpActionResult
            return(Ok(UpdatedPerson));



            //tested using Postman
            //constructed JSON object to pass in is e.g.
            //{
            // "authorised" : true,
            // "enabled" : true,
            // "Colours" : [
            //  {
            //   "id":1,
            //   "name":"Red"

            //        },
            //  {
            //   "id":2,"name":"Green"
            //  },
            //  {
            //   "id":3,"name":"Blue"
            //  }

            // ]

            //}


            //One other thing I would have done is bring in Automapper and used DTOs with the objects in the API controller
        }
コード例 #23
0
        public void Run()
        {
            using (var service = new PersonService())
            {
                string firstName = "Test", lastName = $"Person{DateTime.UtcNow.ToString("yyyyMMddHHmmss")}";

                var update = new PersonUpdate
                {
                    FirstName    = firstName,
                    LastName     = lastName,
                    SupportLevel = SupportLevel.Undecided
                };
                update.SetCustomField("test1", "blah1");

                // Create a new person
                var createResult = service.Create(update);

                var personId = createResult.Person.Id;

                // Update the new person's support level
                var updateResult = service.Update(personId, new PersonUpdate
                {
                    SupportLevel = SupportLevel.Supporter
                });

                // Match the new person
                var matched = service.Match(new MatchPersonParameters
                {
                    FirstName = firstName,
                    LastName  = lastName
                });

                // Search for the new person
                var searchResults = service.Search(new SearchPeopleParameters
                {
                    FirstName = firstName,
                    LastName  = lastName
                }).FirstOrDefault();

                // Add some tags to the person
                service.AddTags(personId, new List <string> {
                    "testtag1", "testtag2"
                });

                // Remove one of the tags we added
                service.RemoveTags(personId, new List <string> {
                    "testtag1"
                });

                // Add a private note to the person
                service.AddPrivateNote(personId, "Test private note");

                // Get the person details
                var showResult = service.Show(personId, true);

                // Get first 5 voters near Chicago
                var nearby = service.GetNearby(new GetNearbyPeopleParameters(new Coordinates(41.8781, -87.6298), 5000))
                             .SetLimit(5)
                             .ToList();

                // Get first 2 people from the person index
                var indexPeople = service.GetIndex()
                                  .SetLimit(2)
                                  .ToList();

                // Get the first 2 pages of people from the people index. Each page will contain 2 people.
                var indexPages = service.GetIndexAsPages(2)
                                 .SetLimit(2)
                                 .ToList();

                // Delete the test person we created
                service.Destroy(personId);
            }
        }
コード例 #24
0
 public IResult Insert(PersonUpdate personUpdate)
 {
     _personUpdateDal.Insert(personUpdate);
     return(new SuccessResult());
 }