Exemple #1
0
        public async Task <ActionResult <CreateHouseView> > Create(
            CancellationToken cancellationToken,
            [FromQuery] string address,
            [FromBody] CreateHouseBinding binding
            )
        {
            if (String.IsNullOrEmpty(address))
            {
                return(BadRequest(address));
            }

            if (await _houseRepository.GetByAddress(address, cancellationToken) != null)
            {
                return(Conflict($"Address already exists: {address}"));
            }

            if (await _context.Houses.SingleOrDefaultAsync(o => o.Login == binding.Login, cancellationToken) != null)
            {
                return(Conflict($"Login already exists: {binding.Login}"));
            }

            var house = new House(Guid.NewGuid(), address, binding.Login, binding.Password);

            await _houseRepository.Save(house, cancellationToken);

            var token = await _jwt.Create(house, cancellationToken);

            return(Ok(new CreateHouseView
            {
                Id = house.Id,
                Address = house.Address,
                LivesHereCounter = house.People.Count(),
                Token = new TokenView {
                    AccessToken = token.Value
                }
            }));
        }
 public void Save()
 {
     _houseRepository.Save();
 }