Example #1
0
        public async Task <UsermanagerResponse> CreateUserAsync(ApplicationUser user, string password, string role)
        {
            var result = await _userManager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, role);

                return(UsermanagerResponse.TaskCompletedSuccessfully());
            }

            IEnumerable <string> errors = result.Errors.Select(error => error.Description);

            return(UsermanagerResponse.TaskFailed(errors));
        }
Example #2
0
        public async Task <UsermanagerResponse> DeleteUserAsync(string userId)
        {
            var user = await GetUserWithCities(userId);

            if (user == null)
            {
                return(UsermanagerResponse.TaskCompletedSuccessfully());
            }


            List <Barrack>            barracks            = new List <Barrack>();
            List <CityHall>           cityHalls           = new List <CityHall>();
            List <CityWall>           cityWalls           = new List <CityWall>();
            List <Farm>               farms               = new List <Farm>();
            List <ResourceProduction> resourceProductions = new List <ResourceProduction>();
            List <Warehouse>          warehouses          = new List <Warehouse>();
            List <UnitsInCity>        unitsInCities       = new List <UnitsInCity>();

            foreach (var city in user.Cities)
            {
                barracks.Add(_db.Barracks.FirstOrDefault(x => x.CityId.Equals(city.Id)));
                cityHalls.Add(_db.CityHalls.FirstOrDefault(x => x.CityId.Equals(city.Id)));
                cityWalls.Add(_db.CityWalls.FirstOrDefault(x => x.CityId.Equals(city.Id)));
                farms.Add(_db.Farms.FirstOrDefault(x => x.CityId.Equals(city.Id)));
                resourceProductions.AddRange(_db.ResourceProductions.Where(x => x.CityId.Equals(city.Id)).ToList());
                warehouses.Add(_db.Warehouses.FirstOrDefault(x => x.CityId.Equals(city.Id)));
            }
            foreach (var barrack in barracks)
            {
                foreach (var units in barrack.UnitsInCity)
                {
                    unitsInCities.Add(units);
                }
            }

            var result = await _userManager.DeleteAsync(user);

            foreach (var item in barracks)
            {
                _db.Barracks.Remove(item);
            }

            foreach (var item in cityHalls)
            {
                _db.CityHalls.Remove(item);
            }

            foreach (var item in cityWalls)
            {
                _db.CityWalls.Remove(item);
            }

            foreach (var item in farms)
            {
                _db.Farms.Remove(item);
            }

            foreach (var item in resourceProductions)
            {
                _db.ResourceProductions.Remove(item);
            }

            foreach (var item in warehouses)
            {
                _db.Warehouses.Remove(item);
            }

            foreach (var item in unitsInCities)
            {
                _db.UnitsInCities.Remove(item);
            }

            await _db.SaveChangesAsync();

            if (result.Succeeded)
            {
                return(UsermanagerResponse.TaskCompletedSuccessfully());
            }

            IEnumerable <string> errors = result.Errors.Select(error => error.Description);

            return(UsermanagerResponse.TaskFailed(errors));
        }