// POST: api/Person/1
 public void Post([FromBody] BigCompany.Contracts.Person value)
 {
     // modify or update an existing person
     if (value == null)
     {
         return;
     }
     _repository.Add(value);
 }
        public void Add(BigCompany.Contracts.Person dtoPerson)
        {
            // get the existing or instantiate a new person
            Person dataAccessPerson = null;

            if (dtoPerson.Id != default(int))
            {
                dataAccessPerson = _context.People.FirstOrDefault(p => p.Id == dtoPerson.Id);
            }
            if (dataAccessPerson == null)
            {
                dataAccessPerson = new Person();
                _context.People.Add(dataAccessPerson);
            }

            // update the values on the person
            dataAccessPerson.DateOfBirth = dtoPerson.DateOfBirth;
            dataAccessPerson.FirstName   = dtoPerson.FirstName;
            dataAccessPerson.LastName    = dtoPerson.LastName;

            // todo: separate API calls for posting interests  ex: /api/person/1/interests/1
            if (dataAccessPerson.Interests == null)
            {
                dataAccessPerson.Interests = new List <PersonInterest>();
            }
            else if (dataAccessPerson.Interests.Any())
            {
                dataAccessPerson.Interests.Clear();
            }
            foreach (var dtoInterest in dtoPerson.Interests)
            {
                dataAccessPerson.Interests.Add(new PersonInterest {
                    Interest = dtoInterest
                });
            }

            // todo: separate API calls for posting image(s)
            if (string.IsNullOrEmpty(dtoPerson.ImageBase64) == false)
            {
                dataAccessPerson.Image = new PersonImage {
                    ImageBase64 = dtoPerson.ImageBase64
                };
            }

            _context.SaveChanges();
        }
Esempio n. 3
0
        public void PostTests()
        {
            // ARRANGE
            var controller = new PeopleSearch.Web.Controllers.API.PersonController(_personRepository);
            var dtoPerson  = new BigCompany.Contracts.Person()
            {
                DateOfBirth = new DateTime(1985, 5, 22),
                FirstName   = "UniqueString",
                LastName    = "Last",
                ImageBase64 = "test",
                Interests   = new[] { "Snowboarding", "Skydiving", "Faberge Eggs" }
            };

            // ACT
            controller.Post(dtoPerson);

            // ASSERT
            _persons.ToList().Any(x => x.FirstName == "UniqueString").Should().BeTrue();
            _mockContext.Verify(x => x.SaveChanges());
        }