help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
go-rest-api
/
cmd
/
seeder
RSK World
go-rest-api
Go REST API - Enterprise-grade REST API with JWT Authentication + PostgreSQL + Redis Caching + Docker + Comprehensive Testing + Educational Design
seeder
  • main.go3.1 KB
main.go
cmd/seeder/main.go
Raw Download
Find: Go to:
/*
* Author: RSK World
* Email: help@rskworld.in / support@rskworld.in
* Website: https://rskworld.in/contact.php
* Year: 2026
*/

package main

import (
	"fmt"
	"log"

	"github.com/rskworld/go-rest-api/internal/config"
	"github.com/rskworld/go-rest-api/internal/database"
	"github.com/rskworld/go-rest-api/internal/models"
	"golang.org/x/crypto/bcrypt"
)

func main() {
	cfg := config.LoadConfig()
	database.Connect(cfg)

	// Auto migrate
	database.DB.AutoMigrate(&models.User{}, &models.Product{})

	// Seed Categories
	seedCategories()

	// Seed Users
	seedUsers()

	// Seed Products
	seedProducts()

	fmt.Println("Database seeded successfully!")
}

func seedCategories() {
	categories := []models.Category{
		{Name: "Electronics", Description: "Electronic devices and accessories"},
		{Name: "Books", Description: "Books and educational materials"},
		{Name: "Software", Description: "Software and digital products"},
	}

	for _, category := range categories {
		var count int64
		database.DB.Model(&models.Category{}).Where("name = ?", category.Name).Count(&count)
		if count == 0 {
			if err := database.DB.Create(&category).Error; err != nil {
				log.Printf("Failed to seed category %s: %v", category.Name, err)
			} else {
				log.Printf("Seeded category: %s", category.Name)
			}
		}
	}
}

func seedUsers() {
	password, _ := bcrypt.GenerateFromPassword([]byte("password123"), bcrypt.DefaultCost)
	users := []models.User{
		{Name: "Admin User", Email: "admin@rskworld.in", Password: string(password), Role: "admin", IsActive: true},
		{Name: "Regular User", Email: "user@rskworld.in", Password: string(password), Role: "user", IsActive: true},
	}

	for _, user := range users {
		var count int64
		database.DB.Model(&models.User{}).Where("email = ?", user.Email).Count(&count)
		if count == 0 {
			if err := database.DB.Create(&user).Error; err != nil {
				log.Printf("Failed to seed user %s: %v", user.Email, err)
			} else {
				log.Printf("Seeded user: %s", user.Email)
			}
		}
	}
}

func seedProducts() {
	// Get category IDs
	var booksCategory, electronicsCategory, softwareCategory models.Category
	database.DB.Where("name = ?", "Books").First(&booksCategory)
	database.DB.Where("name = ?", "Electronics").First(&electronicsCategory)
	database.DB.Where("name = ?", "Software").First(&softwareCategory)

	products := []models.Product{
		{Name: "Go Programming Book", Description: "Master Go efficiently", Price: 49.99, Stock: 100, CategoryID: booksCategory.ID},
		{Name: "Docker for Beginners", Description: "Learn Containerization", Price: 29.99, Stock: 50, CategoryID: booksCategory.ID},
		{Name: "REST API Course", Description: "Build Scalable APIs", Price: 99.99, Stock: 200, CategoryID: softwareCategory.ID},
	}

	for _, product := range products {
		var count int64
		database.DB.Model(&models.Product{}).Where("name = ?", product.Name).Count(&count)
		if count == 0 {
			if err := database.DB.Create(&product).Error; err != nil {
				log.Printf("Failed to seed product %s: %v", product.Name, err)
			} else {
				log.Printf("Seeded product: %s", product.Name)
			}
		}
	}
}
104 lines•3.1 KB
go

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer