Example #1
0
        public async Task <Guid> UpdateAsync(Guid id, UserForUpdationDto updationDto)
        {
            var user = await _entity.SingleOrDefaultAsync(r => r.Id == id);

            if (user == null)
            {
                throw new InvalidOperationException("Can not find object with this Id.");
            }
            foreach (PropertyInfo propertyInfo in updationDto.GetType().GetProperties())
            {
                string key = propertyInfo.Name;
                if (key != "Id" && user.GetType().GetProperty(key) != null)
                {
                    user.GetType().GetProperty(key).SetValue(user, propertyInfo.GetValue(updationDto, null));
                }
            }

            var roles = await _userManager.GetRolesAsync(user);

            await _userManager.RemoveFromRolesAsync(user, roles.ToArray());

            await _userManager.AddToRolesAsync(user, updationDto.RoleNames);

            _entity.Update(user);

            var currentUserStorages = await _context.UserStorages.Where(us => us.UserId == user.Id).ToListAsync();

            foreach (var userStorage in currentUserStorages)
            {
                var updatedId = updationDto.StorageIds.SingleOrDefault(s => s == userStorage.StorageId);
                if (updatedId == Guid.Empty)
                {
                    _context.UserStorages.Remove(userStorage);
                }
            }
            foreach (var storageId in updationDto.StorageIds)
            {
                var currentUserStorage = currentUserStorages.SingleOrDefault(us => us.StorageId == storageId);
                if (currentUserStorage == null)
                {
                    var userStorage = new UserStorageEntity()
                    {
                        StorageId = storageId,
                        UserId    = user.Id
                    };
                    await _context.AddAsync(userStorage);
                }
            }

            var updated = await _context.SaveChangesAsync();

            if (updated < 1)
            {
                throw new InvalidOperationException("Database context could not update data.");
            }
            return(id);
        }
        new public async Task <Guid> CreateAsync(StorageDto storageDto)
        {
            StorageEntity newStorage = new StorageEntity()
            {
                Name    = storageDto.Name,
                Address = storageDto.Address
            };

            await _entity.AddAsync(newStorage);

            // we need to generate product storages for all product
            var products = await _context.Products.ToListAsync();

            foreach (var product in products)
            {
                var productStorage = new ProductStorageEntity()
                {
                    ProductId = product.Id,
                    StorageId = newStorage.Id,
                    ProductProductionDateList = null,
                    Inventory             = 0,
                    CapitalPrice          = 0,
                    PlacePosition         = null,
                    CapitalPriceTrackings = null
                };
                await _context.AddAsync(productStorage);
            }
            // we need to create user storage for all owners

            var owners = await _userRepository.GetUsersWithRoleNameAsync(CONSTANT.ROLE_OWNER);

            foreach (var owner in owners)
            {
                var userStorage = new UserStorageEntity()
                {
                    StorageId = newStorage.Id,
                    UserId    = owner.Id
                };
                _context.UserStorages.Add(userStorage);
            }
            ;

            var created = await _context.SaveChangesAsync();

            if (created < 1)
            {
                throw new InvalidOperationException("Database context could not create data.");
            }
            return(newStorage.Id);
        }
Example #3
0
        public async Task <Guid> CreateAsync(UserForCreationDto creationDto)
        {
            UserEntity newUser = Activator.CreateInstance <UserEntity>();

            newUser.IsActive = true;

            foreach (PropertyInfo propertyInfo in creationDto.GetType().GetProperties())
            {
                if (newUser.GetType().GetProperty(propertyInfo.Name) != null)
                {
                    newUser.GetType().GetProperty(propertyInfo.Name).SetValue(newUser, propertyInfo.GetValue(creationDto, null));
                }
            }
            newUser.UserName = creationDto.UserName;
            await _userManager.CreateAsync(newUser, creationDto.Password);

            foreach (string roleName in creationDto.RoleNames)
            {
                await _userManager.AddToRoleAsync(newUser, roleName);
            }

            //    await _userManager.UpdateAsync(newUser);

            foreach (Guid storageId in creationDto.StorageIds)
            {
                var storage = await _context.Storages.SingleOrDefaultAsync(s => s.Id == storageId);

                if (storage == null)
                {
                    throw new Exception("Can not find storage with Id");
                }
                var userStorage = new UserStorageEntity()
                {
                    UserId    = newUser.Id,
                    StorageId = storageId
                };
                await _context.UserStorages.AddAsync(userStorage);

                await _context.SaveChangesAsync();
            }



            return(newUser.Id);
        }
Example #4
0
        private static async Task SetOwnerHasAllStorages(string userName, DatabaseContext context)
        {
            var user = await context.Users.SingleOrDefaultAsync(u => u.UserName == userName);

            var storages = await context.Storages.ToArrayAsync();

            foreach (var storage in storages)
            {
                var currentUserStorage = await context.UserStorages.SingleOrDefaultAsync(us => us.UserId == user.Id && us.StorageId == storage.Id);

                if (currentUserStorage == null)
                {
                    var newUserStorage = new UserStorageEntity
                    {
                        UserId    = user.Id,
                        StorageId = storage.Id
                    };
                    await context.UserStorages.AddAsync(newUserStorage);
                }
                ;
            }
            await context.SaveChangesAsync();
        }