307 lines
12 KiB
Bash
Executable File
307 lines
12 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Database seeding script for SOA Art Gallery System
|
|
# This script creates sample galleries, artworks, and reviews for testing
|
|
|
|
API_BASE="https://api.local"
|
|
TOKEN_ENDPOINT="http://auth.local:8080/realms/master/protocol/openid-connect/token"
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
echo -e "${BLUE}🌱 SOA Database Seeding Script${NC}"
|
|
echo "=================================="
|
|
|
|
# Function to get Bearer token
|
|
get_token() {
|
|
echo -e "${YELLOW}🔑 Getting Bearer token...${NC}"
|
|
TOKEN=$(curl -s -k -X POST "$TOKEN_ENDPOINT" \
|
|
-H "Content-Type: application/x-www-form-urlencoded" \
|
|
-d "grant_type=password" \
|
|
-d "client_id=soa" \
|
|
-d "client_secret=mysecret" \
|
|
-d "username=alexis" \
|
|
-d "password=password" | jq -r '.access_token')
|
|
|
|
if [ "$TOKEN" == "null" ] || [ -z "$TOKEN" ]; then
|
|
echo -e "${RED}❌ Failed to get token. Make sure Keycloak is running and configured.${NC}"
|
|
exit 1
|
|
fi
|
|
echo -e "${GREEN}✅ Token obtained successfully${NC}"
|
|
}
|
|
|
|
# Function to make API calls
|
|
api_call() {
|
|
local method=$1
|
|
local endpoint=$2
|
|
local data=$3
|
|
|
|
echo -e "${BLUE}[DEBUG] $method $API_BASE$endpoint${NC}" >&2
|
|
|
|
local response
|
|
if [ -n "$data" ]; then
|
|
response=$(curl -s -k -w "\nHTTP_CODE:%{http_code}" -X "$method" "$API_BASE$endpoint" \
|
|
-H "Authorization: Bearer $TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$data")
|
|
else
|
|
response=$(curl -s -k -w "\nHTTP_CODE:%{http_code}" -X "$method" "$API_BASE$endpoint" \
|
|
-H "Authorization: Bearer $TOKEN")
|
|
fi
|
|
|
|
local http_code=$(echo "$response" | grep "HTTP_CODE:" | cut -d: -f2)
|
|
local body=$(echo "$response" | sed '/HTTP_CODE:/d')
|
|
|
|
echo -e "${BLUE}[DEBUG] Response: $http_code${NC}" >&2
|
|
if [ "$http_code" -ge 400 ]; then
|
|
echo -e "${RED}[ERROR] $body${NC}" >&2
|
|
fi
|
|
|
|
echo "$body"
|
|
}
|
|
|
|
# Function to create galleries
|
|
create_galleries() {
|
|
echo -e "${YELLOW}🏛️ Creating sample galleries...${NC}"
|
|
|
|
# Gallery 1: Modern Art Collection
|
|
GALLERY1=$(api_call "POST" "/api/private/gallery" '{
|
|
"title": "Modern Art Collection",
|
|
"description": "A curated collection of contemporary artworks featuring abstract compositions, digital art, and mixed media pieces from emerging artists.",
|
|
"is_public": true,
|
|
"publication_date": "2025-01-01T10:00:00Z"
|
|
}')
|
|
echo -e "${BLUE}[DEBUG] Gallery1 response: $GALLERY1${NC}" >&2
|
|
GALLERY1_ID=$(echo "$GALLERY1" | jq -r '.id // empty')
|
|
if [ -z "$GALLERY1_ID" ] || [ "$GALLERY1_ID" == "null" ]; then
|
|
echo -e "${RED}❌ Failed to create Modern Art Collection${NC}"
|
|
echo -e "${RED}Response: $GALLERY1${NC}"
|
|
return 1
|
|
fi
|
|
echo -e "${GREEN}✅ Created gallery: Modern Art Collection (ID: $GALLERY1_ID)${NC}"
|
|
|
|
# Gallery 2: Digital Dreams
|
|
GALLERY2=$(api_call "POST" "/api/private/gallery" '{
|
|
"title": "Digital Dreams",
|
|
"description": "Exploring the intersection of technology and creativity through digital artworks, NFT collections, and interactive media installations.",
|
|
"is_public": true,
|
|
"publication_date": "2025-02-15T14:30:00Z"
|
|
}')
|
|
GALLERY2_ID=$(echo "$GALLERY2" | jq -r '.id // empty')
|
|
if [ -z "$GALLERY2_ID" ] || [ "$GALLERY2_ID" == "null" ]; then
|
|
echo -e "${RED}❌ Failed to create Digital Dreams${NC}"
|
|
return 1
|
|
fi
|
|
echo -e "${GREEN}✅ Created gallery: Digital Dreams (ID: $GALLERY2_ID)${NC}"
|
|
|
|
# Gallery 3: Private Showcase (not public)
|
|
GALLERY3=$(api_call "POST" "/api/private/gallery" '{
|
|
"title": "Private Showcase",
|
|
"description": "An exclusive collection of rare and valuable artworks, available only to invited members and collectors.",
|
|
"is_public": false
|
|
}')
|
|
GALLERY3_ID=$(echo $GALLERY3 | jq -r '.id')
|
|
echo -e "${GREEN}✅ Created gallery: Private Showcase (ID: $GALLERY3_ID)${NC}"
|
|
|
|
# Gallery 4: Street Art Revolution
|
|
GALLERY4=$(api_call "POST" "/api/private/gallery" '{
|
|
"title": "Street Art Revolution",
|
|
"description": "Celebrating urban culture and street art movements from around the world, featuring graffiti, murals, and public installations.",
|
|
"is_public": true,
|
|
"publication_date": "2025-03-20T09:00:00Z"
|
|
}')
|
|
GALLERY4_ID=$(echo $GALLERY4 | jq -r '.id')
|
|
echo -e "${GREEN}✅ Created gallery: Street Art Revolution (ID: $GALLERY4_ID)${NC}"
|
|
}
|
|
|
|
# Function to create artworks
|
|
create_artworks() {
|
|
echo -e "${YELLOW}🎨 Creating sample artworks...${NC}"
|
|
|
|
# Artworks for Gallery 1 (Modern Art Collection)
|
|
api_call "POST" "/api/private/gallery/$GALLERY1_ID/artwork" '{
|
|
"title": "Abstract Harmony",
|
|
"description": "A vibrant abstract composition exploring the relationship between color and emotion through bold brushstrokes and geometric forms.",
|
|
"image_url": "https://picsum.photos/800/600?random=1",
|
|
"medium": "Acrylic on canvas",
|
|
"dimensions": "120x90 cm",
|
|
"creation_year": 2024,
|
|
"price": 2500.00,
|
|
"is_visible": true,
|
|
"is_for_sale": true
|
|
}'
|
|
echo -e "${GREEN}✅ Created artwork: Abstract Harmony${NC}"
|
|
|
|
api_call "POST" "/api/private/gallery/$GALLERY1_ID/artwork" '{
|
|
"title": "Urban Reflections",
|
|
"description": "A contemporary piece capturing the essence of city life through mixed media and metallic accents.",
|
|
"image_url": "https://picsum.photos/800/600?random=2",
|
|
"medium": "Mixed media",
|
|
"dimensions": "100x70 cm",
|
|
"creation_year": 2024,
|
|
"price": 1800.00,
|
|
"is_visible": true,
|
|
"is_for_sale": true
|
|
}'
|
|
echo -e "${GREEN}✅ Created artwork: Urban Reflections${NC}"
|
|
|
|
# Artworks for Gallery 2 (Digital Dreams)
|
|
api_call "POST" "/api/private/gallery/$GALLERY2_ID/artwork" '{
|
|
"title": "Neural Networks",
|
|
"description": "A digital masterpiece generated using AI algorithms, exploring the beauty of artificial intelligence and machine learning patterns.",
|
|
"image_url": "https://picsum.photos/800/600?random=3",
|
|
"medium": "Digital art (NFT)",
|
|
"dimensions": "4K resolution",
|
|
"creation_year": 2024,
|
|
"price": 0.5,
|
|
"is_visible": true,
|
|
"is_for_sale": true
|
|
}'
|
|
echo -e "${GREEN}✅ Created artwork: Neural Networks${NC}"
|
|
|
|
api_call "POST" "/api/private/gallery/$GALLERY2_ID/artwork" '{
|
|
"title": "Cyberpunk Cityscape",
|
|
"description": "A futuristic vision of urban landscapes, blending neon aesthetics with digital photography and post-processing techniques.",
|
|
"image_url": "https://picsum.photos/800/600?random=4",
|
|
"medium": "Digital photography",
|
|
"dimensions": "Print: 150x100 cm",
|
|
"creation_year": 2024,
|
|
"price": 1200.00,
|
|
"is_visible": true,
|
|
"is_for_sale": false
|
|
}'
|
|
echo -e "${GREEN}✅ Created artwork: Cyberpunk Cityscape${NC}"
|
|
|
|
# Artworks for Gallery 3 (Private Showcase)
|
|
api_call "POST" "/api/private/gallery/$GALLERY3_ID/artwork" '{
|
|
"title": "Exclusive Masterpiece",
|
|
"description": "A rare and valuable piece from a renowned artist, available only to select collectors and gallery members.",
|
|
"image_url": "https://picsum.photos/800/600?random=5",
|
|
"medium": "Oil on canvas",
|
|
"dimensions": "200x150 cm",
|
|
"creation_year": 2023,
|
|
"price": 50000.00,
|
|
"is_visible": false,
|
|
"is_for_sale": true
|
|
}' > /dev/null
|
|
echo -e "${GREEN}✅ Created artwork: Exclusive Masterpiece${NC}"
|
|
|
|
# Artworks for Gallery 4 (Street Art Revolution)
|
|
api_call "POST" "/api/private/gallery/$GALLERY4_ID/artwork" '{
|
|
"title": "Wall of Expression",
|
|
"description": "A powerful street art piece addressing social issues through bold colors and symbolic imagery.",
|
|
"image_url": "https://picsum.photos/800/600?random=6",
|
|
"medium": "Spray paint on brick",
|
|
"dimensions": "300x200 cm",
|
|
"creation_year": 2024,
|
|
"is_visible": true,
|
|
"is_for_sale": false
|
|
}' > /dev/null
|
|
echo -e "${GREEN}✅ Created artwork: Wall of Expression${NC}"
|
|
|
|
api_call "POST" "/api/private/gallery/$GALLERY4_ID/artwork" '{
|
|
"title": "Graffiti Evolution",
|
|
"description": "Documenting the evolution of graffiti art from underground culture to mainstream recognition.",
|
|
"image_url": "https://picsum.photos/800/600?random=7",
|
|
"medium": "Photographic series",
|
|
"dimensions": "Various sizes",
|
|
"creation_year": 2024,
|
|
"price": 800.00,
|
|
"is_visible": true,
|
|
"is_for_sale": true
|
|
}' > /dev/null
|
|
echo -e "${GREEN}✅ Created artwork: Graffiti Evolution${NC}"
|
|
}
|
|
|
|
# Function to create reviews
|
|
create_reviews() {
|
|
echo -e "${YELLOW}💬 Creating sample reviews...${NC}"
|
|
|
|
# Gallery reviews
|
|
api_call "POST" "/api/private/gallery/$GALLERY1_ID/review" '{
|
|
"grade": 5,
|
|
"description": "Absolutely stunning collection! The curation is excellent and each piece tells a compelling story. The Modern Art Collection truly captures the essence of contemporary creativity."
|
|
}' > /dev/null
|
|
echo -e "${GREEN}✅ Created gallery review for Modern Art Collection${NC}"
|
|
|
|
api_call "POST" "/api/private/gallery/$GALLERY2_ID/review" '{
|
|
"grade": 4,
|
|
"description": "Fascinating exploration of digital art! The intersection of technology and creativity is beautifully showcased here. Some pieces are truly groundbreaking."
|
|
}' > /dev/null
|
|
echo -e "${GREEN}✅ Created gallery review for Digital Dreams${NC}"
|
|
|
|
api_call "POST" "/api/private/gallery/$GALLERY4_ID/review" '{
|
|
"grade": 5,
|
|
"description": "Powerful and thought-provoking! This gallery does an amazing job of highlighting the cultural significance of street art and its evolution into fine art."
|
|
}' > /dev/null
|
|
echo -e "${GREEN}✅ Created gallery review for Street Art Revolution${NC}"
|
|
|
|
# Get artwork IDs for artwork reviews
|
|
ARTWORKS=$(api_call "GET" "/api/private/gallery/$GALLERY1_ID/artworks")
|
|
ARTWORK1_ID=$(echo $ARTWORKS | jq -r '.[0].id')
|
|
ARTWORK2_ID=$(echo $ARTWORKS | jq -r '.[1].id')
|
|
|
|
if [ "$ARTWORK1_ID" != "null" ] && [ -n "$ARTWORK1_ID" ]; then
|
|
api_call "POST" "/api/private/artwork/$ARTWORK1_ID/review" '{
|
|
"grade": 5,
|
|
"description": "Abstract Harmony is a masterpiece! The use of color and form creates an emotional response that is both immediate and lasting. Truly exceptional work."
|
|
}' > /dev/null
|
|
echo -e "${GREEN}✅ Created artwork review for Abstract Harmony${NC}"
|
|
fi
|
|
|
|
if [ "$ARTWORK2_ID" != "null" ] && [ -n "$ARTWORK2_ID" ]; then
|
|
api_call "POST" "/api/private/artwork/$ARTWORK2_ID/review" '{
|
|
"grade": 4,
|
|
"description": "Urban Reflections captures the energy of city life beautifully. The mixed media approach adds depth and texture that photographs cannot convey."
|
|
}' > /dev/null
|
|
echo -e "${GREEN}✅ Created artwork review for Urban Reflections${NC}"
|
|
fi
|
|
}
|
|
|
|
# Function to display summary
|
|
display_summary() {
|
|
echo ""
|
|
echo -e "${BLUE}📊 Seeding Summary${NC}"
|
|
echo "=================="
|
|
echo -e "${GREEN}✅ Created 4 galleries:${NC}"
|
|
echo " • Modern Art Collection (Public)"
|
|
echo " • Digital Dreams (Public)"
|
|
echo " • Private Showcase (Private)"
|
|
echo " • Street Art Revolution (Public)"
|
|
echo ""
|
|
echo -e "${GREEN}✅ Created 7 artworks across galleries${NC}"
|
|
echo -e "${GREEN}✅ Created 5 reviews (galleries and artworks)${NC}"
|
|
echo ""
|
|
echo -e "${YELLOW}🔗 Test the API:${NC}"
|
|
echo " Public galleries: curl -k $API_BASE/api/public/galleries"
|
|
echo " Private galleries: curl -k -H \"Authorization: Bearer \$TOKEN\" $API_BASE/api/private/galleries"
|
|
echo ""
|
|
echo -e "${GREEN}🎉 Database seeding completed successfully!${NC}"
|
|
}
|
|
|
|
# Main execution
|
|
main() {
|
|
get_token
|
|
create_galleries
|
|
create_artworks
|
|
create_reviews
|
|
display_summary
|
|
}
|
|
|
|
# Check dependencies
|
|
if ! command -v jq &> /dev/null; then
|
|
echo -e "${RED}❌ jq is required but not installed. Please install jq first.${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v curl &> /dev/null; then
|
|
echo -e "${RED}❌ curl is required but not installed. Please install curl first.${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# Run the script
|
|
main |