From a8715fa8d246a93c63ade4018c37002500f32dc8 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Mon, 6 Feb 2023 11:57:58 +0100 Subject: [PATCH] Improved sample data. --- gymboard-api/sample_data/cities.csv | 3 +- gymboard-api/sample_data/countries.csv | 3 +- gymboard-api/sample_data/exercises.csv | 3 +- gymboard-api/sample_data/gyms.csv | 3 +- gymboard-api/sample_data/submissions.csv | 3 +- gymboard-api/sample_data/users.csv | 34 ++++- .../gymboard_api/util/SampleDataLoader.java | 117 +++++++++++------- 7 files changed, 114 insertions(+), 52 deletions(-) diff --git a/gymboard-api/sample_data/cities.csv b/gymboard-api/sample_data/cities.csv index 65dbce9..2ae7ad6 100644 --- a/gymboard-api/sample_data/cities.csv +++ b/gymboard-api/sample_data/cities.csv @@ -1,4 +1,5 @@ +country-code,short-name,name nl,groningen,Groningen nl,amsterdam,Amsterdam us,tampa,Tampa -us,new-york-city,New York City \ No newline at end of file +us,new-york-city,New York City diff --git a/gymboard-api/sample_data/countries.csv b/gymboard-api/sample_data/countries.csv index e13d59c..c91106d 100644 --- a/gymboard-api/sample_data/countries.csv +++ b/gymboard-api/sample_data/countries.csv @@ -1,6 +1,7 @@ +code,name us,United States nl,Netherlands de,Germany uk,United Kingdom dk,Denmark -mx,Mexico \ No newline at end of file +mx,Mexico diff --git a/gymboard-api/sample_data/exercises.csv b/gymboard-api/sample_data/exercises.csv index a5bd31d..0065136 100644 --- a/gymboard-api/sample_data/exercises.csv +++ b/gymboard-api/sample_data/exercises.csv @@ -1,5 +1,6 @@ +short-name,name barbell-bench-press,Barbell Bench Press barbell-squat,Barbell Squat barbell-deadlift,Barbell Deadlift barbell-overhead-press,Barbell Overhead Press -incline-dumbbell-bicep-curl,Incline Dumbbell Bicep Curl \ No newline at end of file +incline-dumbbell-bicep-curl,Incline Dumbbell Bicep Curl diff --git a/gymboard-api/sample_data/gyms.csv b/gymboard-api/sample_data/gyms.csv index 5a3c4f9..75c2bd0 100644 --- a/gymboard-api/sample_data/gyms.csv +++ b/gymboard-api/sample_data/gyms.csv @@ -1,3 +1,4 @@ +country-code,city-short-name,short-name,name,website-url,latitude,longitude,street-address nl,groningen,trainmore-munnekeholm,Trainmore Munnekeholm,https://trainmore.nl/clubs/munnekeholm/,53.215939,6.561549,"Munnekeholm 1, 9711 JA Groningen" nl,groningen,trainmore-oude-ebbinge,Trainmore Oude Ebbinge Non-Stop,https://trainmore.nl/clubs/oude-ebbinge/,53.2209,6.565976,Oude Ebbingestraat 54-58 -us,tampa,powerhouse-gym,Powerhouse Gym Athletic Club,http://www.pgathleticclub.com/,27.997223,-82.496237,"3251-A W Hillsborough Ave, Tampa, FL 33614, United States" \ No newline at end of file +us,tampa,powerhouse-gym,Powerhouse Gym Athletic Club,http://www.pgathleticclub.com/,27.997223,-82.496237,"3251-A W Hillsborough Ave, Tampa, FL 33614, United States" diff --git a/gymboard-api/sample_data/submissions.csv b/gymboard-api/sample_data/submissions.csv index e7ca0f3..a10e922 100644 --- a/gymboard-api/sample_data/submissions.csv +++ b/gymboard-api/sample_data/submissions.csv @@ -1,3 +1,4 @@ +exercise-short-name,raw-weight,weight-unit,reps,submitter-name,gym-id,video-filename barbell-overhead-press,60,KG,1,Andrew Lalis,nl_groningen_trainmore-munnekeholm,sample_video_ohp.mp4 incline-dumbbell-bicep-curl,14,KG,10,Andrew Lalis,nl_groningen_trainmore-munnekeholm,sample_video_curl.mp4 barbell-bench-press,105,KG,5,William Johnson,nl_groningen_trainmore-munnekeholm,sample_video_ohp.mp4 @@ -5,4 +6,4 @@ barbell-deadlift,210,LBS,1,Steve,us_tampa_powerhouse-gym,sample_video_curl.mp4 barbell-squat,225,LBS,8,Ronnie Coleman,us_tampa_powerhouse-gym,sample_video_ohp.mp4 barbell-overhead-press,110,KG,12,Larry Wheels,nl_groningen_trainmore-oude-ebbinge,sample_video_ohp.mp4 barbell-bench-press,785,LBS,1,Julius Maddox,us_tampa_powerhouse-gym,sample_video_ohp.mp4 -barbell-squat,350,KG,2,Daniel Zamani,nl_groningen_trainmore-oude-ebbinge,sample_video_curl.mp4 \ No newline at end of file +barbell-squat,350,KG,2,Daniel Zamani,nl_groningen_trainmore-oude-ebbinge,sample_video_curl.mp4 diff --git a/gymboard-api/sample_data/users.csv b/gymboard-api/sample_data/users.csv index 473acfb..678fa76 100644 --- a/gymboard-api/sample_data/users.csv +++ b/gymboard-api/sample_data/users.csv @@ -1,4 +1,30 @@ -jay.cutler@example.com,testpass,Jay Cutler, -mike.mentzer@example.com,testpass,Mike Mentzer, -ronnie.coleman@example.com,testpass,Ronnie 'Lightweight' Coleman, -andrew.lalis@example.com,testpass,Andrew Lalis,admin \ No newline at end of file +email,password,name,roles,following,birth-date,current-weight,current-weight-unit,sex,locale,account-private +jay.cutler@example.com,testpass,Jay Cutler,,"mike.mentzer@example.com +ronnie.coleman@example.com",1973-08-03,260,LBS,MALE,en-US,FALSE +mike.mentzer@example.com,testpass,Mike Mentzer,,"ronnie.coleman@example.com +jay.cutler@example.com +andrew.lalis@example.com",1951-11-15,244,LBS,MALE,en-US,FALSE +ronnie.coleman@example.com,testpass,Ronnie 'Lightweight' Coleman,,jay.cutler@example.com,1964-05-13,315,LBS,MALE,en-US,FALSE +andrew.lalis@example.com,testpass,Andrew Lalis,admin,"said.faroghi@example.com +klaus.lalis@example.com +zino.holwerda@example.com +tom.denboon@example.com +max.verbeek@example.com",1997-12-13,98,KG,MALE,en-US,FALSE +said.faroghi@example.com,testpass,Said Faroghi,,"andrew.lalis@example.com +zino.holwerda@example.com",1995-11-11,68,KG,MALE,en-US,TRUE +erblin.ibrahimi@example.com,testpass,Erblin Ibrahimi,,"max.verbeek@example.com +niels.bugel@example.com +ronnie.coleman@example.com",1998-09-28,101,KG,MALE,nl-NL,FALSE +max.verbeek@example.com,testpass,Max Verbeek,,"erblin.ibrahimi@example.com +niels.bugel@example.com +klaus.lalis@example.com",1998-01-24,85,KG,MALE,nl-NL,FALSE +niels.bugel@example.com,testpass,Niels Bugel,,"erblin.ibrahimi@example.com +max.verbeek@example.com +andrew.lalis@example.com",1996-04-21,95,KG,MALE,nl-NL,FALSE +zino.holwerda@example.com,testpass,Zino Holwerda,,"andrew.lalis@example.com +said.faroghi@example.com +tom.denboon@example.com +klaus.lalis@example.com",1997-04-22,91,KG,MALE,nl-NL,TRUE +tom.denboon@example.com,testpass,Tom den Boon,,zino.holwerda@example.com,1998-11-09,84,KG,MALE,nl-NL,FALSE +klaus.lalis@example.com,testpass,Klaus Lalis,,"andrew.lalis@example.com +zino.holwerda@example.com",2000-12-15,93,KG,MALE,en-US,FALSE diff --git a/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/util/SampleDataLoader.java b/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/util/SampleDataLoader.java index 55d5335..f52cc49 100644 --- a/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/util/SampleDataLoader.java +++ b/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/util/SampleDataLoader.java @@ -11,10 +11,13 @@ import nl.andrewlalis.gymboard_api.domains.api.model.exercise.Exercise; import nl.andrewlalis.gymboard_api.domains.api.service.cdn_client.CdnClient; import nl.andrewlalis.gymboard_api.domains.api.service.submission.ExerciseSubmissionService; import nl.andrewlalis.gymboard_api.domains.auth.dao.RoleRepository; +import nl.andrewlalis.gymboard_api.domains.auth.dao.UserPersonalDetailsRepository; +import nl.andrewlalis.gymboard_api.domains.auth.dao.UserPreferencesRepository; import nl.andrewlalis.gymboard_api.domains.auth.dao.UserRepository; import nl.andrewlalis.gymboard_api.domains.auth.dto.UserCreationPayload; import nl.andrewlalis.gymboard_api.domains.auth.model.Role; import nl.andrewlalis.gymboard_api.domains.auth.model.User; +import nl.andrewlalis.gymboard_api.domains.auth.model.UserPersonalDetails; import nl.andrewlalis.gymboard_api.domains.auth.service.UserService; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVRecord; @@ -31,8 +34,7 @@ import java.io.IOException; import java.math.BigDecimal; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashSet; -import java.util.Set; +import java.time.LocalDate; /** * Simple component that loads sample data that's useful when testing the application. @@ -47,6 +49,8 @@ public class SampleDataLoader implements ApplicationListener { - exerciseRepository.save(new Exercise(record.get(0), record.get(1))); + exerciseRepository.save(new Exercise(record.get("short-name"), record.get("name"))); }); loadCsv("countries", record -> { - countryRepository.save(new Country(record.get(0), record.get(1))); + countryRepository.save(new Country(record.get("code"), record.get("name"))); }); loadCsv("cities", record -> { - var country = countryRepository.findById(record.get(0)).orElseThrow(); - cityRepository.save(new City(record.get(1), record.get(2), country)); + var country = countryRepository.findById(record.get("country-code")).orElseThrow(); + String shortName = record.get("short-name"); + String name = record.get("name"); + cityRepository.save(new City(shortName, name, country)); }); loadCsv("gyms", record -> { - var city = cityRepository.findByShortNameAndCountryCode(record.get(1), record.get(0)).orElseThrow(); + var city = cityRepository.findByShortNameAndCountryCode( + record.get("city-short-name"), + record.get("country-code") + ).orElseThrow(); gymRepository.save(new Gym( city, - record.get(2), - record.get(3), - record.get(4), + record.get("short-name"), + record.get("name"), + record.get("website-url"), new GeoPoint( - new BigDecimal(record.get(5)), - new BigDecimal(record.get(6)) + new BigDecimal(record.get("latitude")), + new BigDecimal(record.get("longitude")) ), - record.get(7) + record.get("street-address") )); }); @@ -114,18 +126,16 @@ public class SampleDataLoader implements ApplicationListener videoIds = new HashSet<>(); loadCsv("submissions", record -> { - var exercise = exerciseRepository.findById(record.get(0)).orElseThrow(); - BigDecimal weight = new BigDecimal(record.get(1)); - WeightUnit unit = WeightUnit.parse(record.get(2)); - int reps = Integer.parseInt(record.get(3)); - String name = record.get(4); - CompoundGymId gymId = CompoundGymId.parse(record.get(5)); - String videoFilename = record.get(6); + var exercise = exerciseRepository.findById(record.get("exercise-short-name")).orElseThrow(); + BigDecimal weight = new BigDecimal(record.get("raw-weight")); + WeightUnit unit = WeightUnit.parse(record.get("weight-unit")); + int reps = Integer.parseInt(record.get("reps")); + String name = record.get("submitter-name"); + CompoundGymId gymId = CompoundGymId.parse(record.get("gym-id")); + String videoFilename = record.get("video-filename"); - // Upload the video to the CDN, and wait until it's done processing. log.info("Uploading video {} to CDN...", videoFilename); var video = cdnClient.uploads.uploadVideo(Path.of("sample_data", videoFilename), "video/mp4"); submissionService.createSubmission(gymId, new ExerciseSubmissionPayload( @@ -136,28 +146,21 @@ public class SampleDataLoader implements ApplicationListener removalSet = new HashSet<>(); - for (var videoId : videoIds) { - String status = cdnClient.uploads.getVideoProcessingStatus(videoId).status(); - if (status.equalsIgnoreCase("COMPLETED") || status.equalsIgnoreCase("FAILED")) { - removalSet.add(videoId); - } - } - videoIds.removeAll(removalSet); - Thread.sleep(1000); - } - loadCsv("users", record -> { - String email = record.get(0); - String password = record.get(1); - String name = record.get(2); - String[] roleNames = record.get(3).split("\\s*\\|\\s*"); + String email = record.get("email"); + String password = record.get("password"); + String name = record.get("name"); + String[] roleNames = record.get("roles").split("\\s*\\n\\s*"); + LocalDate birthDate = LocalDate.parse(record.get("birth-date")); + BigDecimal currentWeight = new BigDecimal(record.get("current-weight")); + WeightUnit currentWeightUnit = WeightUnit.parse(record.get("current-weight-unit")); + BigDecimal metricWeight = new BigDecimal(currentWeight.toString()); + if (currentWeightUnit == WeightUnit.POUNDS) { + metricWeight = WeightUnit.toKilograms(metricWeight); + } + UserPersonalDetails.PersonSex sex = UserPersonalDetails.PersonSex.parse(record.get("sex")); UserCreationPayload payload = new UserCreationPayload(email, password, name); var resp = userService.createUser(payload, false); @@ -169,6 +172,30 @@ public class SampleDataLoader implements ApplicationListener { + String email = record.get("email"); + String[] followingEmails = record.get("following").split("\\s*\\n\\s*"); + User user = userRepository.findByEmail(email).orElseThrow(); + for (String followingEmail : followingEmails) { + User userToFollow = userRepository.findByEmail(followingEmail).orElseThrow(); + userService.followUser(user.getId(), userToFollow.getId()); + } }); } @@ -181,7 +208,11 @@ public class SampleDataLoader implements ApplicationListener