From 60c2ff3d8e486d8228afe105430043dc6440c580 Mon Sep 17 00:00:00 2001 From: amir-ys Date: Wed, 8 Apr 2026 01:02:42 +0330 Subject: [PATCH] feat: add product entity and migrations --- .../1775595704_create_categories_table.sql | 10 ++++++ .../1775595705_create_products_table.sql | 17 ++++++++++ ...5595706_create_category_products_table.sql | 11 +++++++ ...1775595707_create_product_images_table.sql | 11 +++++++ productapp/service/category/entity.go | 10 ++++++ productapp/service/category/param.go | 1 + productapp/service/category/service.go | 1 + productapp/service/category/validator.go | 1 + productapp/service/entity.go | 1 - productapp/service/param.go | 1 - productapp/service/product/entity.go | 31 +++++++++++++++++++ productapp/service/product/param.go | 1 + productapp/service/product/service.go | 1 + productapp/service/product/validator.go | 1 + productapp/service/service.go | 1 - productapp/service/validator.go | 1 - 16 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 productapp/repository/migrations/1775595704_create_categories_table.sql create mode 100644 productapp/repository/migrations/1775595705_create_products_table.sql create mode 100644 productapp/repository/migrations/1775595706_create_category_products_table.sql create mode 100644 productapp/repository/migrations/1775595707_create_product_images_table.sql create mode 100644 productapp/service/category/entity.go create mode 100644 productapp/service/category/param.go create mode 100644 productapp/service/category/service.go create mode 100644 productapp/service/category/validator.go delete mode 100644 productapp/service/entity.go delete mode 100644 productapp/service/param.go create mode 100644 productapp/service/product/entity.go create mode 100644 productapp/service/product/param.go create mode 100644 productapp/service/product/service.go create mode 100644 productapp/service/product/validator.go delete mode 100644 productapp/service/service.go delete mode 100644 productapp/service/validator.go diff --git a/productapp/repository/migrations/1775595704_create_categories_table.sql b/productapp/repository/migrations/1775595704_create_categories_table.sql new file mode 100644 index 00000000..a1d7701e --- /dev/null +++ b/productapp/repository/migrations/1775595704_create_categories_table.sql @@ -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`; diff --git a/productapp/repository/migrations/1775595705_create_products_table.sql b/productapp/repository/migrations/1775595705_create_products_table.sql new file mode 100644 index 00000000..23e210c1 --- /dev/null +++ b/productapp/repository/migrations/1775595705_create_products_table.sql @@ -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`; diff --git a/productapp/repository/migrations/1775595706_create_category_products_table.sql b/productapp/repository/migrations/1775595706_create_category_products_table.sql new file mode 100644 index 00000000..307e13c7 --- /dev/null +++ b/productapp/repository/migrations/1775595706_create_category_products_table.sql @@ -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`; diff --git a/productapp/repository/migrations/1775595707_create_product_images_table.sql b/productapp/repository/migrations/1775595707_create_product_images_table.sql new file mode 100644 index 00000000..ac9e1fb3 --- /dev/null +++ b/productapp/repository/migrations/1775595707_create_product_images_table.sql @@ -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`; diff --git a/productapp/service/category/entity.go b/productapp/service/category/entity.go new file mode 100644 index 00000000..42169c58 --- /dev/null +++ b/productapp/service/category/entity.go @@ -0,0 +1,10 @@ +package category + +import "time" + +type Category struct { + ID uint + Name string + Slug string + CreatedAt time.Time +} diff --git a/productapp/service/category/param.go b/productapp/service/category/param.go new file mode 100644 index 00000000..79661ada --- /dev/null +++ b/productapp/service/category/param.go @@ -0,0 +1 @@ +package category diff --git a/productapp/service/category/service.go b/productapp/service/category/service.go new file mode 100644 index 00000000..79661ada --- /dev/null +++ b/productapp/service/category/service.go @@ -0,0 +1 @@ +package category diff --git a/productapp/service/category/validator.go b/productapp/service/category/validator.go new file mode 100644 index 00000000..79661ada --- /dev/null +++ b/productapp/service/category/validator.go @@ -0,0 +1 @@ +package category diff --git a/productapp/service/entity.go b/productapp/service/entity.go deleted file mode 100644 index 6d43c336..00000000 --- a/productapp/service/entity.go +++ /dev/null @@ -1 +0,0 @@ -package service diff --git a/productapp/service/param.go b/productapp/service/param.go deleted file mode 100644 index 6d43c336..00000000 --- a/productapp/service/param.go +++ /dev/null @@ -1 +0,0 @@ -package service diff --git a/productapp/service/product/entity.go b/productapp/service/product/entity.go new file mode 100644 index 00000000..b3dc5901 --- /dev/null +++ b/productapp/service/product/entity.go @@ -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 +} diff --git a/productapp/service/product/param.go b/productapp/service/product/param.go new file mode 100644 index 00000000..e6ae0918 --- /dev/null +++ b/productapp/service/product/param.go @@ -0,0 +1 @@ +package product diff --git a/productapp/service/product/service.go b/productapp/service/product/service.go new file mode 100644 index 00000000..e6ae0918 --- /dev/null +++ b/productapp/service/product/service.go @@ -0,0 +1 @@ +package product diff --git a/productapp/service/product/validator.go b/productapp/service/product/validator.go new file mode 100644 index 00000000..e6ae0918 --- /dev/null +++ b/productapp/service/product/validator.go @@ -0,0 +1 @@ +package product diff --git a/productapp/service/service.go b/productapp/service/service.go deleted file mode 100644 index 6d43c336..00000000 --- a/productapp/service/service.go +++ /dev/null @@ -1 +0,0 @@ -package service diff --git a/productapp/service/validator.go b/productapp/service/validator.go deleted file mode 100644 index 6d43c336..00000000 --- a/productapp/service/validator.go +++ /dev/null @@ -1 +0,0 @@ -package service