Exemplo n.º 1
0
        public async Task ActiveBrother_IsReturned()
        {
            int id = _dbContext.Brother.Add(new Brother {
                FirstName = "First", LastName = "Last"
            }).Entity.Id;
            await _dbContext.SaveChangesAsync();

            BrotherController  controller = new BrotherController(_dbContext, null, null);
            BrotherDetailModel result     = ((OkObjectResult)await controller.GetBrother(id)).Value as BrotherDetailModel;

            Assert.Multiple(() => {
                Assert.That(result, Is.Not.Null);
                Assert.That(result.Id, Is.EqualTo(id));
                Assert.That(result.FirstName, Is.EqualTo("First"));
                Assert.That(result.LastName, Is.EqualTo("Last"));
            });
        }
        public async Task <IActionResult> GetBrother(int id)
        {
            BrotherDetailModel brother =
                _dbContext.Brother
                .Include(b => b.BrotherPosition)
                .ThenInclude(position => position.Position)
                .Include(b => b.BrotherMajor)
                .ThenInclude(major => major.Major)
                .Include(b => b.BrotherMinor)
                .ThenInclude(minor => minor.Minor)
                .Include(b => b.Answer)
                .ThenInclude(answer => answer.Question)
                .Select(b => b.ToBrotherDetailModel())
                .FirstOrDefault(b => b.Id == id);

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

            // Add the little brothers and big brother. Could do this via EFCore for the big brother with some effort,
            // but not the little brothers. It can't do the query in the reverse as far as I can tell.
            brother.BigBrother = _dbContext
                                 .Brother
                                 .Include(b => b.InactiveBrother)
                                 .FirstOrDefault(big => big.Id == _dbContext.Brother.First(b => b.Id == brother.Id).BigBrotherId)?
                                 .ToRelatedBrotherModel();

            brother.LittleBrothers = _dbContext
                                     .Brother
                                     .Include(b => b.InactiveBrother)
                                     .Where(b => b.BigBrotherId == brother.Id)
                                     .Select(b => b.ToRelatedBrotherModel());

            return(Ok(brother));
        }