DemoDataInitializer.java
6,005 bytes
| 1 | package com.tasteprint.demo; |
|---|---|
| 2 | |
| 3 | import java.time.Clock; |
| 4 | import java.time.LocalDate; |
| 5 | import java.util.UUID; |
| 6 | |
| 7 | import org.springframework.boot.ApplicationArguments; |
| 8 | import org.springframework.boot.ApplicationRunner; |
| 9 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 10 | import org.springframework.stereotype.Component; |
| 11 | |
| 12 | import com.tasteprint.account.AccountService; |
| 13 | import com.tasteprint.account.UpdateProfileRequest; |
| 14 | import com.tasteprint.journey.SaveTripRequest; |
| 15 | import com.tasteprint.journey.TripService; |
| 16 | import com.tasteprint.social.ChallengeService; |
| 17 | import com.tasteprint.social.ChallengeView; |
| 18 | import com.tasteprint.social.CreateChallengeRequest; |
| 19 | import com.tasteprint.tasting.SaveTastingRequest; |
| 20 | import com.tasteprint.tasting.TastingService; |
| 21 | |
| 22 | @Component |
| 23 | @ConditionalOnProperty(name = "app.demo-data-enabled", havingValue = "true") |
| 24 | class DemoDataInitializer implements ApplicationRunner { |
| 25 | |
| 26 | private final AccountService accounts; |
| 27 | private final TastingService tastings; |
| 28 | private final TripService trips; |
| 29 | private final ChallengeService challenges; |
| 30 | private final Clock clock; |
| 31 | |
| 32 | DemoDataInitializer(AccountService accounts, TastingService tastings, |
| 33 | TripService trips, ChallengeService challenges, Clock clock) { |
| 34 | this.accounts = accounts; |
| 35 | this.tastings = tastings; |
| 36 | this.trips = trips; |
| 37 | this.challenges = challenges; |
| 38 | this.clock = clock; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public void run(ApplicationArguments args) { |
| 43 | UUID demoUser = accounts.ensureDemoAccount( |
| 44 | "Rasmus", "demo@tasteprint.app", "tasteprint", true |
| 45 | ); |
| 46 | UUID maria = accounts.ensureDemoAccount( |
| 47 | "Maria", "maria@tasteprint.app", "tasteprint", true |
| 48 | ); |
| 49 | accounts.update(demoUser, new UpdateProfileRequest( |
| 50 | "Rasmus", "Tallinn", "EE", "I plan trips around the dishes I have not tried yet.", null, true |
| 51 | )); |
| 52 | accounts.update(maria, new UpdateProfileRequest( |
| 53 | "Maria", "Lisbon", "PT", "Street food first, museum second.", null, true |
| 54 | )); |
| 55 | |
| 56 | LocalDate today = LocalDate.now(clock); |
| 57 | if (!tastings.hasAny(demoUser)) { |
| 58 | seedDemoTastings(demoUser, today); |
| 59 | } |
| 60 | if (!tastings.hasAny(maria)) { |
| 61 | seedMariaTastings(maria, today); |
| 62 | } |
| 63 | if (!trips.hasAny(demoUser)) { |
| 64 | trips.create(demoUser, new SaveTripRequest( |
| 65 | "JP", "Osaka", today.plusDays(24), today.plusDays(31) |
| 66 | )); |
| 67 | } |
| 68 | if (!challenges.hasAny(demoUser)) { |
| 69 | ChallengeView challenge = challenges.create(demoUser, new CreateChallengeRequest( |
| 70 | "Japan beyond sushi", "JP", today.minusDays(14), today.plusDays(30) |
| 71 | )); |
| 72 | challenges.join(maria, challenge.joinCode()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private void seedDemoTastings(UUID userId, LocalDate today) { |
| 77 | record(userId, "ramen", "Tokyo", "JP", today.minusDays(210), 5, |
| 78 | "The broth changed what I thought ramen was.", |
| 79 | "https://images.unsplash.com/photo-1569718212165-3a8278d5f624?auto=format&fit=crop&w=1200&q=80"); |
| 80 | record(userId, "sushi", "Tokyo", "JP", today.minusDays(208), 5, |
| 81 | "The rice temperature was the detail I had never noticed.", |
| 82 | "https://images.unsplash.com/photo-1579871494447-9811cf80d66c?auto=format&fit=crop&w=1200&q=80"); |
| 83 | record(userId, "tacos-al-pastor", "Mexico City", "MX", today.minusDays(145), 5, |
| 84 | "Late-night counter, pineapple, and a very fast second order.", |
| 85 | "https://images.unsplash.com/photo-1551504734-5ee1c4a1479b?auto=format&fit=crop&w=1200&q=80"); |
| 86 | record(userId, "chilaquiles", "Mexico City", "MX", today.minusDays(143), 4, |
| 87 | "Breakfast that made the previous night feel manageable.", null); |
| 88 | record(userId, "pastel-de-nata", "Lisbon", "PT", today.minusDays(94), 5, |
| 89 | "Still warm, with cinnamon and an espresso.", null); |
| 90 | record(userId, "pho", "Hanoi", "VN", today.minusDays(63), 5, |
| 91 | "A quiet morning bowl with herbs added one pinch at a time.", |
| 92 | "https://images.unsplash.com/photo-1582878826629-29b7ad1cdc43?auto=format&fit=crop&w=1200&q=80"); |
| 93 | record(userId, "khachapuri", "Tallinn", "EE", today.minusDays(35), 4, |
| 94 | "Tried the Adjarian version close to home.", null); |
| 95 | record(userId, "kama", "Tallinn", "EE", today.minusDays(22), 4, |
| 96 | "Best with tart kefir and berries.", null); |
| 97 | record(userId, "kiluleib", "Tallinn", "EE", today.minusDays(12), 5, |
| 98 | "Rye, sprat, egg, onion. Nothing extra needed.", null); |
| 99 | record(userId, "matcha", "Tallinn", "EE", today.minusDays(7), 4, |
| 100 | "Earthier and less bitter than the versions I knew.", null); |
| 101 | } |
| 102 | |
| 103 | private void seedMariaTastings(UUID userId, LocalDate today) { |
| 104 | record(userId, "ramen", "Kyoto", "JP", today.minusDays(180), 4, null, null); |
| 105 | record(userId, "takoyaki", "Osaka", "JP", today.minusDays(178), 5, null, null); |
| 106 | record(userId, "sushi", "Tokyo", "JP", today.minusDays(175), 5, null, null); |
| 107 | record(userId, "pad-kra-pao", "Bangkok", "TH", today.minusDays(88), 5, null, null); |
| 108 | record(userId, "mango-sticky-rice", "Bangkok", "TH", today.minusDays(86), 4, null, null); |
| 109 | record(userId, "ceviche", "Lima", "PE", today.minusDays(44), 5, null, null); |
| 110 | record(userId, "pastel-de-nata", "Lisbon", "PT", today.minusDays(20), 5, null, null); |
| 111 | record(userId, "daifuku", "Lisbon", "PT", today.minusDays(5), 4, null, null); |
| 112 | } |
| 113 | |
| 114 | private void record(UUID userId, String dishSlug, String city, String countryCode, |
| 115 | LocalDate date, int rating, String note, String photoUrl) { |
| 116 | tastings.record(userId, new SaveTastingRequest( |
| 117 | dishSlug, null, city, countryCode, date, rating, note, photoUrl, null, null |
| 118 | )); |
| 119 | } |
| 120 | } |
| 121 | |