Ejemplo n.º 1
0
        public async Task <Unit> Handle(SeedDataCommand request, CancellationToken cancellationToken)
        {
            await _identityDbContext.MigrateAsync(cancellationToken);

            await _dbContext.MigrateAsync(cancellationToken);

            if (await _userRepo.AnyAsync(u => true))
            {
                return(Unit.Value);
            }

            await _dbRunTimeConfig.CreateLocationIndex();

            var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var usersJson = File.ReadAllText(Path.Combine(dir, "System/Commands/SeedData/seedUserData.json"));
            var userList  = JsonConvert.DeserializeObject <List <User> >(usersJson);

            var photosJson = File.ReadAllText(Path.Combine(dir, "System/Commands/SeedData/seedPhotoData.json"));
            var photoList  = JsonConvert.DeserializeObject <List <Photo> >(photosJson);

            var locationsJson = File.ReadAllText(Path.Combine(dir, "System/Commands/SeedData/seedUserCoordinate.json"));

            locationsJson = locationsJson.Replace("\r\n\t", "");
            var coordinateList = JsonConvert.DeserializeObject <double[][]>(locationsJson);

            for (int i = 0; i < userList.Count; i++)
            {
                var currentUser    = userList[i];
                var securityUserId = await _securityUserManager.CreateUserAsync(currentUser.UserName, "password");

                var property = typeof(User).GetProperty(nameof(User.IdentityId));
                property.GetSetMethod(true).Invoke(currentUser, new object[] { securityUserId });

                if (photoList.ElementAtOrDefault(i) != null)
                {
                    currentUser.AddPhoto(photoList[i]);
                }
                currentUser.SetLocation(coordinateList[i]);
            }

            await _userRepo.AddRangeAsync(userList);

            return(Unit.Value);
        }