public async Task <Page <HouseListItem> > GetHouses( CancellationToken cancellationToken, [FromQuery] DefaultListBinding binding) { var query = _context.Houses .AsNoTracking() .Select(o => new HouseListItem { Id = o.Id, Address = o.Address, LivesHereCounter = o.People.Count() }); var items = await query .Skip(binding.Offset) .Take(binding.Limit) .ToListAsync(); return(new Page <HouseListItem> { Limit = binding.Limit, Offset = binding.Offset, Total = await query.CountAsync(), Items = items }); }
public async Task <Page <PersonView> > GetPeople( CancellationToken cancellationToken, [FromQuery] DefaultListBinding binding) { var address = User.GetAddress(); var house = await _houseRepository.GetByAddress(address, cancellationToken); var query = _context.People .AsNoTracking() .Include(o => o.House) .Where(o => o.House == house) .Select(o => new PersonView { Id = o.Id, Name = o.Name, Surname = o.Surname, Login = User.IsInRole("admin") == true ? o.Login : null }); var items = await query .Skip(binding.Offset) .Take(binding.Limit) .ToListAsync(); return(new Page <PersonView> { Limit = binding.Limit, Offset = binding.Offset, Total = await query.CountAsync(), Items = items }); }
public async Task <Page <CreatePersonRequestListItem> > GetCreatePersonRequests( CancellationToken cancellationToken, [FromQuery] DefaultListBinding binding) { var houseId = User.GetId(); var query = _context.CreatePersonRequests .AsNoTracking() .Include(o => o.House) .Where(o => o.House.Id == houseId) .Select(o => new CreatePersonRequestListItem { Id = o.Id, Name = o.Name, Surname = o.Surname, Login = o.Login, Password = o.Password }); var items = await query .Skip(binding.Offset) .Take(binding.Limit) .ToListAsync(); return(new Page <CreatePersonRequestListItem> { Limit = binding.Limit, Offset = binding.Offset, Total = await query.CountAsync(), Items = items }); }
public async Task <ActionResult <Page <VotingListItem> > > GetVotingsPerson( CancellationToken cancellationToken, [FromQuery] DefaultListBinding binding, [FromQuery] bool onlyOpened = false ) { var house = await _houseRepository.GetByAddress(User.GetAddress(), cancellationToken); var person = await _personRepository.Get(User.GetId(), cancellationToken); var query = _context.Votings .AsNoTracking() .Include(o => o.Variants) .ThenInclude(variant => variant.Votes) .ThenInclude(vote => vote.Person) .Include(o => o.House) .Where(o => o.House.Id == house.Id) .Where(o => onlyOpened == true ? o.IsClosed == false : true) .Select(o => new VotingListItem { Id = o.Id, Title = o.Title, IsClosed = o.IsClosed, Variants = o.Variants.Select(variant => new VariantView { Id = variant.Id, Title = variant.Title, Count = variant.Votes.Count, Votes = variant.Votes.Select(vote => new VoteView { Id = vote.Id, Person = new PersonView { Id = vote.Person.Id, Name = vote.Person.Name, Surname = vote.Person.Surname } }).ToList() }).ToList() }); var items = await query .Skip(binding.Offset) .Take(binding.Limit) .ToListAsync(); foreach (var item in items) { item.IsVoted = await IsVoted(person.Id, item.Id); } return(new Page <VotingListItem> { Limit = binding.Limit, Offset = binding.Offset, Total = await query.CountAsync(), Items = items }); }