feat: add product entity and migrations

This commit is contained in:
amir-ys 2026-04-08 01:02:42 +03:30
parent a193abeb56
commit 60c2ff3d8e
16 changed files with 96 additions and 4 deletions

View File

@ -0,0 +1,10 @@
-- +migrate Up
CREATE TABLE `categories` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`slug` VARCHAR(255) NOT NULL UNIQUE,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_persian_ci;
-- +migrate Down
DROP TABLE IF EXISTS `categories`;

View File

@ -0,0 +1,17 @@
-- +migrate Up
CREATE TABLE `products` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`slug` VARCHAR(255) NOT NULL UNIQUE,
`description` TEXT,
`price` DECIMAL(10, 2) NOT NULL,
`stock` INT DEFAULT 0,
`is_active` BOOLEAN DEFAULT TRUE,
`features` JSON DEFAULT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` TIMESTAMP DEFAULT NULL,
FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_persian_ci;
-- +migrate Down
DROP TABLE IF EXISTS `products`;

View File

@ -0,0 +1,11 @@
-- +migrate Up
CREATE TABLE `category_products` (
`category_id` INT NOT NULL,
`product_id` INT NOT NULL,
PRIMARY KEY (`category_id`, `product_id`),
FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_persian_ci;
-- +migrate Down
DROP TABLE IF EXISTS `category_products`;

View File

@ -0,0 +1,11 @@
-- +migrate Up
CREATE TABLE `product_images` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`product_id` INT NOT NULL,
`image_path` VARCHAR(255) NOT NULL,
`is_primary` BOOLEAN DEFAULT FALSE,
FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_persian_ci;
-- +migrate Down
DROP TABLE IF EXISTS `product_images`;

View File

@ -0,0 +1,10 @@
package category
import "time"
type Category struct {
ID uint
Name string
Slug string
CreatedAt time.Time
}

View File

@ -0,0 +1 @@
package category

View File

@ -0,0 +1 @@
package category

View File

@ -0,0 +1 @@
package category

View File

@ -1 +0,0 @@
package service

View File

@ -1 +0,0 @@
package service

View File

@ -0,0 +1,31 @@
package product
import (
"database/sql"
"time"
)
type Product struct {
ID uint
Name string
Slug string
Description string
Price float64
Stock int
IsActive bool
Features string
CreatedAt time.Time
DeletedAt sql.NullTime
}
type ProductImage struct {
ID uint
ProductID uint
ImagePath string
IsPrimary bool
}
type CategoryProduct struct {
CategoryID uint
ProductID uint
}

View File

@ -0,0 +1 @@
package product

View File

@ -0,0 +1 @@
package product

View File

@ -0,0 +1 @@
package product

View File

@ -1 +0,0 @@
package service

View File

@ -1 +0,0 @@
package service