private async Task <Location> GetOrCreateLocationAsync(ImportGenericLookupModel locationModel) { Location location = null; var locationId = await _crossReferenceService.GetEntityIdAsync(typeof(Location), LookupSourceKey, locationModel.RefId); if (locationId.HasValue) { location = await _locationRepository.TableNoTracking.FirstOrDefaultAsync(x => x.Name == locationModel.Name); } if (location == null) { var locNameParts = locationModel.Name.Split(','); // This isn't the most reliable, but hopefully will get us a good start... if (locNameParts.Count() != 2) { _logger.LogInformation($"Location name '{locationModel.Name}` cannot be parsed to be created as a location."); return(null); } var stateProvince = await _stateProvinceRepository.Table.FirstOrDefaultAsync(x => x.Abbreviation == locNameParts[1].ToUpper().Trim()); if (stateProvince == null) { // TODO: Maybe it's better to have a "default" state that we can assign to _logger.LogInformation($"Cannot find StateProvince for '{locNameParts[1].ToUpper()}"); return(null); } location = new Location { Name = locationModel.Name, StateProvinceId = stateProvince.Id }; await _locationRepository.CreateAsync(location); await _crossReferenceService.CreateAsync(typeof(Location), location.Id, LookupSourceKey, locationModel.RefId); } return(location); }
private async Task <int> GetOrCreateAlbumAsync(int userId, ImportGenericLookupModel albumModel) { var albumId = await _crossReferenceService.GetEntityIdAsync(typeof(Album), LookupSourceKey, albumModel.RefId); if (!albumId.HasValue) { var album = new Album { UserId = userId, Description = "imported", Title = albumModel.Name, CreatedOn = DateTime.UtcNow, UpdatedOn = DateTime.UtcNow }; await _albumRepository.CreateAsync(album); await _crossReferenceService.CreateAsync(typeof(Album), album.Id, LookupSourceKey, albumModel.RefId); albumId = album.Id; } return(albumId.Value); }
private async Task <int> GetOrCreateUserAsync(ImportGenericLookupModel userModel) { var userId = await _crossReferenceService.GetEntityIdAsync(typeof(User), LookupSourceKey, userModel.RefId); if (!userId.HasValue) { var userNameParts = userModel.Name.Split(' '); var user = new User { EmailAddress = "unk", FirstName = userNameParts[0], LastName = userNameParts[1], RegisteredOn = DateTime.UtcNow }; await _userService.CreateUserAsync(user); await _crossReferenceService.CreateAsync(typeof(User), user.Id, LookupSourceKey, userModel.RefId); userId = user.Id; } return(userId.Value); }