コード例 #1
0
        public async Task UpsertChildAsync(int parentId, Core.DTOs.Person childDto)
        {
            var repository = this.uow.GetRepository<Person>();
            var parent = await repository.GetAsync(x => x.Id == parentId, x => x.Include(p => p.OutgoingRelations))
                ?? throw new EntityNotFoundException($"Parent with id: {parentId} was not found.");

            if (childDto.Age == 0)
            {
                childDto.Age = await this.ageService.GetRandomAgeAsync();
            }

            // if new child
            if (childDto.Id == 0)
            {
                await repository.InsertAsync(childDto.ToEntity().AddParent(parent));
            }
            else
            {
                var child = await repository.GetAsync(x => x.Id == childDto.Id, x => x.Include(p => p.IncomingRelations))
                    ?? throw new EntityNotFoundException($"Child with id: {childDto.Id} was not found.");

                child.Update(childDto);

                if (!child.HasParent(parent))
                {
                    child.AddParent(parent);
                }
            }

            await this.uow.CommitAsync();
        }
コード例 #2
0
        private void PutFamilyTreeIntoCache(int personId, Core.DTOs.Person familyTree)
        {
            var cacheEntryOptions = new MemoryCacheEntryOptions()
                                    .SetSlidingExpiration(TimeSpan.FromMinutes(1))
                                    .SetAbsoluteExpiration(TimeSpan.FromMinutes(5));

            this.cache.Set(personId, familyTree, cacheEntryOptions);
        }
コード例 #3
0
 public static Core.Entities.Person ToEntity(this Core.DTOs.Person person)
 {
     return(new Core.Entities.Person
     {
         Name = person.Name,
         Surname = person.Surname,
         Age = person.Age
     });
 }
コード例 #4
0
        public async Task UpsertAsync(Core.DTOs.Person personDto)
        {
            var repository = this.uow.GetRepository<Person>();
            var person = await repository.GetAsync(x => x.Id == personDto.Id);

            if (person == null)
            {
                await repository.InsertAsync(personDto.ToEntity());
            }
            else
            {
                person.Update(personDto);
            }

            await this.uow.CommitAsync();
        }
コード例 #5
0
ファイル: PersonTests.cs プロジェクト: PBJailBreak/FamilyTree
        public void Update_ShouldUpdateAllPropertiesToMatch()
        {
            var newName    = "John";
            var newSurname = "Johnaitis";
            var newAge     = 33;

            var personDto = new Core.DTOs.Person
            {
                Name    = newName,
                Surname = newSurname,
                Age     = newAge
            };

            this.person.Update(personDto);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(person.Name, newName);
                Assert.AreEqual(person.Surname, newSurname);
                Assert.AreEqual(person.Age, newAge);
            });
        }
コード例 #6
0
        public IActionResult AddChild([FromRoute] int personId, [FromBody] Core.DTOs.Person person)
        {
            this.backgroundJobs.Enqueue(() => this.service.UpsertChildAsync(personId, person));

            return(Ok());
        }
コード例 #7
0
 public ActionResult AddPerson([FromBody] Core.DTOs.Person person)
 {
     this.backgroundJobs.Enqueue(() => this.service.UpsertAsync(person));
     return(Ok());
 }