From 41471694d597132c6f1b8d2e2d644edf78395ae2 Mon Sep 17 00:00:00 2001 From: GuwandykA Date: Tue, 7 Jul 2026 15:16:40 +0500 Subject: [PATCH] change table column name --- db/migrations/20260702125108_init.down.sql | 4 +- db/migrations/20260702125108_init.up.sql | 17 +- ...706155000_rename_status_to_active.down.sql | 2 +- ...60706155000_rename_status_to_active.up.sql | 2 +- docs/docs.go | 166 +++++++++--------- docs/swagger.json | 166 +++++++++--------- docs/swagger.yaml | 145 ++++++++------- internal/admin/paymentcard/db/postgresql.go | 49 ++---- internal/admin/paymentcard/dto.go | 26 +-- internal/admin/reasonscard/db/postgresql.go | 56 +++--- internal/admin/reasonscard/dto.go | 19 +- 11 files changed, 318 insertions(+), 334 deletions(-) diff --git a/db/migrations/20260702125108_init.down.sql b/db/migrations/20260702125108_init.down.sql index 7458abe..b389ab3 100644 --- a/db/migrations/20260702125108_init.down.sql +++ b/db/migrations/20260702125108_init.down.sql @@ -1,7 +1,7 @@ DROP TABLE IF EXISTS loan_order_required_docs; DROP TABLE IF EXISTS loan_types; -DROP TABLE IF EXISTS reasons_cards; -DROP TABLE IF EXISTS payment_cards; +DROP TABLE IF EXISTS card_states; +DROP TABLE IF EXISTS card_types; DROP TABLE IF EXISTS branches; DROP TABLE IF EXISTS provinces; DROP TABLE IF EXISTS users; diff --git a/db/migrations/20260702125108_init.up.sql b/db/migrations/20260702125108_init.up.sql index b9a7c6f..377eacb 100644 --- a/db/migrations/20260702125108_init.up.sql +++ b/db/migrations/20260702125108_init.up.sql @@ -54,21 +54,22 @@ CREATE TABLE IF NOT EXISTS branches ( "created_at" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL ); -CREATE TABLE IF NOT EXISTS payment_cards ( +CREATE TABLE IF NOT EXISTS card_types ( id bigserial PRIMARY KEY, - price numeric(12, 2) NOT NULL DEFAULT 0, - title json NOT NULL DEFAULT '{}'::json, - content json NOT NULL DEFAULT '{}'::json, + name json NOT NULL DEFAULT '{}'::json, + price character varying(255) NOT NULL DEFAULT '', + notes character varying(255) NOT NULL DEFAULT '', active boolean NOT NULL DEFAULT true, "updated_at" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, "created_at" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL ); -CREATE TABLE IF NOT EXISTS reasons_cards ( +CREATE TABLE IF NOT EXISTS card_states ( id bigserial PRIMARY KEY, - title json NOT NULL DEFAULT '{}'::json, - price numeric(12, 2) NOT NULL DEFAULT 0, - content json NOT NULL DEFAULT '{}'::json, + name json NOT NULL DEFAULT '{}'::json, + price character varying(255) NOT NULL DEFAULT '', + notes character varying(255) NOT NULL DEFAULT '', + active boolean NOT NULL DEFAULT true, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL ); diff --git a/db/migrations/20260706155000_rename_status_to_active.down.sql b/db/migrations/20260706155000_rename_status_to_active.down.sql index 2d96174..d52185c 100644 --- a/db/migrations/20260706155000_rename_status_to_active.down.sql +++ b/db/migrations/20260706155000_rename_status_to_active.down.sql @@ -1,4 +1,4 @@ ALTER TABLE loan_order_required_docs RENAME COLUMN active TO status; -ALTER TABLE payment_cards RENAME COLUMN active TO status; +ALTER TABLE card_types RENAME COLUMN active TO status; ALTER TABLE provinces RENAME COLUMN active TO status; ALTER TABLE users RENAME COLUMN active TO status; diff --git a/db/migrations/20260706155000_rename_status_to_active.up.sql b/db/migrations/20260706155000_rename_status_to_active.up.sql index 3f1bf61..1ac21e4 100644 --- a/db/migrations/20260706155000_rename_status_to_active.up.sql +++ b/db/migrations/20260706155000_rename_status_to_active.up.sql @@ -1,4 +1,4 @@ ALTER TABLE users RENAME COLUMN status TO active; ALTER TABLE provinces RENAME COLUMN status TO active; -ALTER TABLE payment_cards RENAME COLUMN status TO active; +ALTER TABLE card_types RENAME COLUMN status TO active; ALTER TABLE loan_order_required_docs RENAME COLUMN status TO active; diff --git a/docs/docs.go b/docs/docs.go index 7a83863..6b152d2 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -51,8 +51,8 @@ const docTemplate = `{ }, { "type": "string", - "description": "Filter by unique number", - "name": "unikal_nomer", + "description": "Filter by unique code", + "name": "unique_code", "in": "query" }, { @@ -595,7 +595,7 @@ const docTemplate = `{ "parameters": [ { "type": "string", - "description": "Filter by title (en, ru, tm)", + "description": "Filter by name", "name": "document_name", "in": "query" }, @@ -1979,45 +1979,56 @@ const docTemplate = `{ "type": "object", "required": [ "address", - "provinceId", - "title", - "unikalNomer" + "name", + "region" ], "properties": { + "active": { + "type": "boolean" + }, "address": { "$ref": "#/definitions/branches.LocalizedText" }, "billingPassword": { "type": "string" }, - "billingPasswordSber": { + "billingSberPassword": { "type": "string" }, - "billingPasswordSwift": { + "billingSberUsername": { "type": "string" }, - "billingPasswordVisa": { + "billingSwiftPassword": { + "type": "string" + }, + "billingSwiftUsername": { "type": "string" }, "billingUsername": { "type": "string" }, - "billingUsernameSber": { + "billingVisaMasterPassword": { "type": "string" }, - "billingUsernameSwift": { + "billingVisaMasterUsername": { "type": "string" }, - "billingUsernameVisa": { - "type": "string" + "name": { + "$ref": "#/definitions/branches.LocalizedText" + }, + "phoneNumbers": { + "type": "array", + "items": { + "type": "string" + } }, "provinceId": { "type": "integer" }, - "title": { - "$ref": "#/definitions/branches.LocalizedText" + "region": { + "type": "string" }, - "unikalNomer": { + "uniqueCode": { "type": "string" } } @@ -2040,45 +2051,56 @@ const docTemplate = `{ "type": "object", "required": [ "address", - "provinceId", - "title", - "unikalNomer" + "name", + "region" ], "properties": { + "active": { + "type": "boolean" + }, "address": { "$ref": "#/definitions/branches.LocalizedText" }, "billingPassword": { "type": "string" }, - "billingPasswordSber": { + "billingSberPassword": { "type": "string" }, - "billingPasswordSwift": { + "billingSberUsername": { "type": "string" }, - "billingPasswordVisa": { + "billingSwiftPassword": { + "type": "string" + }, + "billingSwiftUsername": { "type": "string" }, "billingUsername": { "type": "string" }, - "billingUsernameSber": { + "billingVisaMasterPassword": { "type": "string" }, - "billingUsernameSwift": { + "billingVisaMasterUsername": { "type": "string" }, - "billingUsernameVisa": { - "type": "string" + "name": { + "$ref": "#/definitions/branches.LocalizedText" + }, + "phoneNumbers": { + "type": "array", + "items": { + "type": "string" + } }, "provinceId": { "type": "integer" }, - "title": { - "$ref": "#/definitions/branches.LocalizedText" + "region": { + "type": "string" }, - "unikalNomer": { + "uniqueCode": { "type": "string" } } @@ -2086,39 +2108,17 @@ const docTemplate = `{ "loansdocument.CreateLoanDocumentDTO": { "type": "object", "required": [ - "contentEn", - "contentRu", - "contentTm", - "title" + "name", + "value" ], "properties": { "active": { "type": "boolean" }, - "contentEn": { + "name": { "type": "string" }, - "contentRu": { - "type": "string" - }, - "contentTm": { - "type": "string" - }, - "title": { - "$ref": "#/definitions/loansdocument.LocalizedTitle" - } - } - }, - "loansdocument.LocalizedTitle": { - "type": "object", - "properties": { - "en": { - "type": "string" - }, - "ru": { - "type": "string" - }, - "tm": { + "value": { "type": "string" } } @@ -2134,26 +2134,18 @@ const docTemplate = `{ "loansdocument.UpdateLoanDocumentDTO": { "type": "object", "required": [ - "contentEn", - "contentRu", - "contentTm", - "title" + "name", + "value" ], "properties": { "active": { "type": "boolean" }, - "contentEn": { + "name": { "type": "string" }, - "contentRu": { + "value": { "type": "string" - }, - "contentTm": { - "type": "string" - }, - "title": { - "$ref": "#/definitions/loansdocument.LocalizedTitle" } } }, @@ -2166,6 +2158,9 @@ const docTemplate = `{ "title" ], "properties": { + "active": { + "type": "boolean" + }, "content": { "$ref": "#/definitions/loantype.LocalizedTitle" }, @@ -2203,6 +2198,9 @@ const docTemplate = `{ "title" ], "properties": { + "active": { + "type": "boolean" + }, "content": { "$ref": "#/definitions/loantype.LocalizedTitle" }, @@ -2220,7 +2218,6 @@ const docTemplate = `{ "paymentcard.CreatePaymentCardDTO": { "type": "object", "required": [ - "content", "price", "title" ], @@ -2228,11 +2225,11 @@ const docTemplate = `{ "active": { "type": "boolean" }, - "content": { - "$ref": "#/definitions/paymentcard.LocalizedText" + "notes": { + "type": "string" }, "price": { - "type": "number" + "type": "string" }, "title": { "$ref": "#/definitions/paymentcard.LocalizedText" @@ -2264,7 +2261,6 @@ const docTemplate = `{ "paymentcard.UpdatePaymentCardDTO": { "type": "object", "required": [ - "content", "price", "title" ], @@ -2272,11 +2268,11 @@ const docTemplate = `{ "active": { "type": "boolean" }, - "content": { - "$ref": "#/definitions/paymentcard.LocalizedText" + "notes": { + "type": "string" }, "price": { - "type": "number" + "type": "string" }, "title": { "$ref": "#/definitions/paymentcard.LocalizedText" @@ -2336,16 +2332,18 @@ const docTemplate = `{ "reasonscard.CreateReasonCardDTO": { "type": "object", "required": [ - "content", "price", "title" ], "properties": { - "content": { - "$ref": "#/definitions/reasonscard.LocalizedText" + "active": { + "type": "boolean" + }, + "notes": { + "type": "string" }, "price": { - "type": "number" + "type": "string" }, "title": { "$ref": "#/definitions/reasonscard.LocalizedText" @@ -2369,16 +2367,18 @@ const docTemplate = `{ "reasonscard.UpdateReasonCardDTO": { "type": "object", "required": [ - "content", "price", "title" ], "properties": { - "content": { - "$ref": "#/definitions/reasonscard.LocalizedText" + "active": { + "type": "boolean" + }, + "notes": { + "type": "string" }, "price": { - "type": "number" + "type": "string" }, "title": { "$ref": "#/definitions/reasonscard.LocalizedText" diff --git a/docs/swagger.json b/docs/swagger.json index e24aeb6..8897f48 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -45,8 +45,8 @@ }, { "type": "string", - "description": "Filter by unique number", - "name": "unikal_nomer", + "description": "Filter by unique code", + "name": "unique_code", "in": "query" }, { @@ -589,7 +589,7 @@ "parameters": [ { "type": "string", - "description": "Filter by title (en, ru, tm)", + "description": "Filter by name", "name": "document_name", "in": "query" }, @@ -1973,45 +1973,56 @@ "type": "object", "required": [ "address", - "provinceId", - "title", - "unikalNomer" + "name", + "region" ], "properties": { + "active": { + "type": "boolean" + }, "address": { "$ref": "#/definitions/branches.LocalizedText" }, "billingPassword": { "type": "string" }, - "billingPasswordSber": { + "billingSberPassword": { "type": "string" }, - "billingPasswordSwift": { + "billingSberUsername": { "type": "string" }, - "billingPasswordVisa": { + "billingSwiftPassword": { + "type": "string" + }, + "billingSwiftUsername": { "type": "string" }, "billingUsername": { "type": "string" }, - "billingUsernameSber": { + "billingVisaMasterPassword": { "type": "string" }, - "billingUsernameSwift": { + "billingVisaMasterUsername": { "type": "string" }, - "billingUsernameVisa": { - "type": "string" + "name": { + "$ref": "#/definitions/branches.LocalizedText" + }, + "phoneNumbers": { + "type": "array", + "items": { + "type": "string" + } }, "provinceId": { "type": "integer" }, - "title": { - "$ref": "#/definitions/branches.LocalizedText" + "region": { + "type": "string" }, - "unikalNomer": { + "uniqueCode": { "type": "string" } } @@ -2034,45 +2045,56 @@ "type": "object", "required": [ "address", - "provinceId", - "title", - "unikalNomer" + "name", + "region" ], "properties": { + "active": { + "type": "boolean" + }, "address": { "$ref": "#/definitions/branches.LocalizedText" }, "billingPassword": { "type": "string" }, - "billingPasswordSber": { + "billingSberPassword": { "type": "string" }, - "billingPasswordSwift": { + "billingSberUsername": { "type": "string" }, - "billingPasswordVisa": { + "billingSwiftPassword": { + "type": "string" + }, + "billingSwiftUsername": { "type": "string" }, "billingUsername": { "type": "string" }, - "billingUsernameSber": { + "billingVisaMasterPassword": { "type": "string" }, - "billingUsernameSwift": { + "billingVisaMasterUsername": { "type": "string" }, - "billingUsernameVisa": { - "type": "string" + "name": { + "$ref": "#/definitions/branches.LocalizedText" + }, + "phoneNumbers": { + "type": "array", + "items": { + "type": "string" + } }, "provinceId": { "type": "integer" }, - "title": { - "$ref": "#/definitions/branches.LocalizedText" + "region": { + "type": "string" }, - "unikalNomer": { + "uniqueCode": { "type": "string" } } @@ -2080,39 +2102,17 @@ "loansdocument.CreateLoanDocumentDTO": { "type": "object", "required": [ - "contentEn", - "contentRu", - "contentTm", - "title" + "name", + "value" ], "properties": { "active": { "type": "boolean" }, - "contentEn": { + "name": { "type": "string" }, - "contentRu": { - "type": "string" - }, - "contentTm": { - "type": "string" - }, - "title": { - "$ref": "#/definitions/loansdocument.LocalizedTitle" - } - } - }, - "loansdocument.LocalizedTitle": { - "type": "object", - "properties": { - "en": { - "type": "string" - }, - "ru": { - "type": "string" - }, - "tm": { + "value": { "type": "string" } } @@ -2128,26 +2128,18 @@ "loansdocument.UpdateLoanDocumentDTO": { "type": "object", "required": [ - "contentEn", - "contentRu", - "contentTm", - "title" + "name", + "value" ], "properties": { "active": { "type": "boolean" }, - "contentEn": { + "name": { "type": "string" }, - "contentRu": { + "value": { "type": "string" - }, - "contentTm": { - "type": "string" - }, - "title": { - "$ref": "#/definitions/loansdocument.LocalizedTitle" } } }, @@ -2160,6 +2152,9 @@ "title" ], "properties": { + "active": { + "type": "boolean" + }, "content": { "$ref": "#/definitions/loantype.LocalizedTitle" }, @@ -2197,6 +2192,9 @@ "title" ], "properties": { + "active": { + "type": "boolean" + }, "content": { "$ref": "#/definitions/loantype.LocalizedTitle" }, @@ -2214,7 +2212,6 @@ "paymentcard.CreatePaymentCardDTO": { "type": "object", "required": [ - "content", "price", "title" ], @@ -2222,11 +2219,11 @@ "active": { "type": "boolean" }, - "content": { - "$ref": "#/definitions/paymentcard.LocalizedText" + "notes": { + "type": "string" }, "price": { - "type": "number" + "type": "string" }, "title": { "$ref": "#/definitions/paymentcard.LocalizedText" @@ -2258,7 +2255,6 @@ "paymentcard.UpdatePaymentCardDTO": { "type": "object", "required": [ - "content", "price", "title" ], @@ -2266,11 +2262,11 @@ "active": { "type": "boolean" }, - "content": { - "$ref": "#/definitions/paymentcard.LocalizedText" + "notes": { + "type": "string" }, "price": { - "type": "number" + "type": "string" }, "title": { "$ref": "#/definitions/paymentcard.LocalizedText" @@ -2330,16 +2326,18 @@ "reasonscard.CreateReasonCardDTO": { "type": "object", "required": [ - "content", "price", "title" ], "properties": { - "content": { - "$ref": "#/definitions/reasonscard.LocalizedText" + "active": { + "type": "boolean" + }, + "notes": { + "type": "string" }, "price": { - "type": "number" + "type": "string" }, "title": { "$ref": "#/definitions/reasonscard.LocalizedText" @@ -2363,16 +2361,18 @@ "reasonscard.UpdateReasonCardDTO": { "type": "object", "required": [ - "content", "price", "title" ], "properties": { - "content": { - "$ref": "#/definitions/reasonscard.LocalizedText" + "active": { + "type": "boolean" + }, + "notes": { + "type": "string" }, "price": { - "type": "number" + "type": "string" }, "title": { "$ref": "#/definitions/reasonscard.LocalizedText" diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 974a95c..25ae85d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -38,35 +38,42 @@ definitions: type: object branches.CreateBranchDTO: properties: + active: + type: boolean address: $ref: '#/definitions/branches.LocalizedText' billingPassword: type: string - billingPasswordSber: + billingSberPassword: type: string - billingPasswordSwift: + billingSberUsername: type: string - billingPasswordVisa: + billingSwiftPassword: + type: string + billingSwiftUsername: type: string billingUsername: type: string - billingUsernameSber: + billingVisaMasterPassword: type: string - billingUsernameSwift: - type: string - billingUsernameVisa: + billingVisaMasterUsername: type: string + name: + $ref: '#/definitions/branches.LocalizedText' + phoneNumbers: + items: + type: string + type: array provinceId: type: integer - title: - $ref: '#/definitions/branches.LocalizedText' - unikalNomer: + region: + type: string + uniqueCode: type: string required: - address - - provinceId - - title - - unikalNomer + - name + - region type: object branches.LocalizedText: properties: @@ -79,62 +86,54 @@ definitions: type: object branches.UpdateBranchDTO: properties: + active: + type: boolean address: $ref: '#/definitions/branches.LocalizedText' billingPassword: type: string - billingPasswordSber: + billingSberPassword: type: string - billingPasswordSwift: + billingSberUsername: type: string - billingPasswordVisa: + billingSwiftPassword: + type: string + billingSwiftUsername: type: string billingUsername: type: string - billingUsernameSber: + billingVisaMasterPassword: type: string - billingUsernameSwift: - type: string - billingUsernameVisa: + billingVisaMasterUsername: type: string + name: + $ref: '#/definitions/branches.LocalizedText' + phoneNumbers: + items: + type: string + type: array provinceId: type: integer - title: - $ref: '#/definitions/branches.LocalizedText' - unikalNomer: + region: + type: string + uniqueCode: type: string required: - address - - provinceId - - title - - unikalNomer + - name + - region type: object loansdocument.CreateLoanDocumentDTO: properties: active: type: boolean - contentEn: + name: type: string - contentRu: + value: type: string - contentTm: - type: string - title: - $ref: '#/definitions/loansdocument.LocalizedTitle' required: - - contentEn - - contentRu - - contentTm - - title - type: object - loansdocument.LocalizedTitle: - properties: - en: - type: string - ru: - type: string - tm: - type: string + - name + - value type: object loansdocument.UpdateLoanDocumentActiveDTO: properties: @@ -145,22 +144,18 @@ definitions: properties: active: type: boolean - contentEn: + name: type: string - contentRu: + value: type: string - contentTm: - type: string - title: - $ref: '#/definitions/loansdocument.LocalizedTitle' required: - - contentEn - - contentRu - - contentTm - - title + - name + - value type: object loantype.CreateLoanTypeDTO: properties: + active: + type: boolean content: $ref: '#/definitions/loantype.LocalizedTitle' loanTerm: @@ -186,6 +181,8 @@ definitions: type: object loantype.UpdateLoanTypeDTO: properties: + active: + type: boolean content: $ref: '#/definitions/loantype.LocalizedTitle' loanTerm: @@ -204,14 +201,13 @@ definitions: properties: active: type: boolean - content: - $ref: '#/definitions/paymentcard.LocalizedText' + notes: + type: string price: - type: number + type: string title: $ref: '#/definitions/paymentcard.LocalizedText' required: - - content - price - title type: object @@ -233,14 +229,13 @@ definitions: properties: active: type: boolean - content: - $ref: '#/definitions/paymentcard.LocalizedText' + notes: + type: string price: - type: number + type: string title: $ref: '#/definitions/paymentcard.LocalizedText' required: - - content - price - title type: object @@ -279,14 +274,15 @@ definitions: type: object reasonscard.CreateReasonCardDTO: properties: - content: - $ref: '#/definitions/reasonscard.LocalizedText' + active: + type: boolean + notes: + type: string price: - type: number + type: string title: $ref: '#/definitions/reasonscard.LocalizedText' required: - - content - price - title type: object @@ -301,14 +297,15 @@ definitions: type: object reasonscard.UpdateReasonCardDTO: properties: - content: - $ref: '#/definitions/reasonscard.LocalizedText' + active: + type: boolean + notes: + type: string price: - type: number + type: string title: $ref: '#/definitions/reasonscard.LocalizedText' required: - - content - price - title type: object @@ -335,9 +332,9 @@ paths: in: query name: branch_name type: string - - description: Filter by unique number + - description: Filter by unique code in: query - name: unikal_nomer + name: unique_code type: string - default: 1 description: Page number @@ -683,7 +680,7 @@ paths: get: description: Returns paginated loan documents with optional filters parameters: - - description: Filter by title (en, ru, tm) + - description: Filter by name in: query name: document_name type: string diff --git a/internal/admin/paymentcard/db/postgresql.go b/internal/admin/paymentcard/db/postgresql.go index add1a68..482c098 100644 --- a/internal/admin/paymentcard/db/postgresql.go +++ b/internal/admin/paymentcard/db/postgresql.go @@ -23,9 +23,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) paymentcard return &repository{client: client, logger: logger} } -const cardColumns = `c.id, c.price, c.title, c.content, c.active` +const cardColumns = `c.id, c.price, c.name, c.notes, c.active` -const cardReturningColumns = `id, price, title, content, active` +const cardReturningColumns = `id, price, name, notes, active` func (r *repository) List(ctx context.Context, filter paymentcard.ListFilter) (paymentcard.ListPaymentCardDTO, error) { fromClause, whereClause, args := buildListQuery(filter) @@ -65,7 +65,7 @@ func (r *repository) List(ctx context.Context, filter paymentcard.ListFilter) (p } func buildListQuery(filter paymentcard.ListFilter) (fromClause, whereClause string, args []interface{}) { - fromClause = ` FROM payment_cards c` + fromClause = ` FROM card_types c` var conditions []string @@ -73,7 +73,7 @@ func buildListQuery(filter paymentcard.ListFilter) (fromClause, whereClause stri args = append(args, likePattern(filter.CardName)) n := len(args) conditions = append(conditions, fmt.Sprintf(`( - c.title->>'en' ILIKE $%[1]d OR c.title->>'ru' ILIKE $%[1]d OR c.title->>'tm' ILIKE $%[1]d + c.name->>'en' ILIKE $%[1]d OR c.name->>'ru' ILIKE $%[1]d OR c.name->>'tm' ILIKE $%[1]d )`, n)) } @@ -97,7 +97,7 @@ func likePattern(term string) string { } func (r *repository) GetByID(ctx context.Context, id int64) (paymentcard.PaymentCardDTO, error) { - query := fmt.Sprintf(`SELECT %s FROM payment_cards c WHERE c.id = $1`, cardColumns) + query := fmt.Sprintf(`SELECT %s FROM card_types c WHERE c.id = $1`, cardColumns) item, err := scanPaymentCard(r.client.QueryRow(ctx, query, id)) if errors.Is(err, pgx.ErrNoRows) { return paymentcard.PaymentCardDTO{}, err @@ -106,7 +106,7 @@ func (r *repository) GetByID(ctx context.Context, id int64) (paymentcard.Payment } func (r *repository) Create(ctx context.Context, dto paymentcard.CreatePaymentCardDTO) (paymentcard.PaymentCardDTO, error) { - title, content, err := marshalJSON(dto.Title, dto.Content) + name, err := marshalName(dto.Title) if err != nil { return paymentcard.PaymentCardDTO{}, err } @@ -117,26 +117,26 @@ func (r *repository) Create(ctx context.Context, dto paymentcard.CreatePaymentCa } query := fmt.Sprintf(` - INSERT INTO payment_cards (price, title, content, active) + INSERT INTO card_types (price, name, notes, active) VALUES ($1, $2, $3, $4) RETURNING %s`, cardReturningColumns) - return scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, title, content, active)) + return scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, active)) } func (r *repository) Update(ctx context.Context, id int64, dto paymentcard.UpdatePaymentCardDTO) (paymentcard.PaymentCardDTO, error) { - title, content, err := marshalJSON(dto.Title, dto.Content) + name, err := marshalName(dto.Title) if err != nil { return paymentcard.PaymentCardDTO{}, err } query := fmt.Sprintf(` - UPDATE payment_cards SET - price = $1, title = $2, content = $3, active = $4 + UPDATE card_types SET + price = $1, name = $2, notes = $3, active = $4 WHERE id = $5 RETURNING %s`, cardReturningColumns) - item, err := scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, title, content, dto.Active, id)) + item, err := scanPaymentCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, dto.Active, id)) if errors.Is(err, pgx.ErrNoRows) { return paymentcard.PaymentCardDTO{}, err } @@ -145,7 +145,7 @@ func (r *repository) Update(ctx context.Context, id int64, dto paymentcard.Updat func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (paymentcard.PaymentCardDTO, error) { query := fmt.Sprintf(` - UPDATE payment_cards SET active = $1 + UPDATE card_types SET active = $1 WHERE id = $2 RETURNING %s`, cardReturningColumns) @@ -157,7 +157,7 @@ func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (p } func (r *repository) Delete(ctx context.Context, id int64) error { - tag, err := r.client.Exec(ctx, `DELETE FROM payment_cards WHERE id = $1`, id) + tag, err := r.client.Exec(ctx, `DELETE FROM card_types WHERE id = $1`, id) if err != nil { return err } @@ -167,16 +167,8 @@ func (r *repository) Delete(ctx context.Context, id int64) error { return nil } -func marshalJSON(title, content paymentcard.LocalizedText) ([]byte, []byte, error) { - titleJSON, err := json.Marshal(title) - if err != nil { - return nil, nil, err - } - contentJSON, err := json.Marshal(content) - if err != nil { - return nil, nil, err - } - return titleJSON, contentJSON, nil +func marshalName(title paymentcard.LocalizedText) ([]byte, error) { + return json.Marshal(title) } type scannable interface { @@ -185,17 +177,14 @@ type scannable interface { func scanPaymentCard(row scannable) (paymentcard.PaymentCardDTO, error) { var item paymentcard.PaymentCardDTO - var titleJSON, contentJSON []byte + var nameJSON []byte if err := row.Scan( - &item.Id, &item.Price, &titleJSON, &contentJSON, &item.Active, + &item.Id, &item.Price, &nameJSON, &item.Notes, &item.Active, ); err != nil { return paymentcard.PaymentCardDTO{}, err } - if err := json.Unmarshal(titleJSON, &item.Title); err != nil { - return paymentcard.PaymentCardDTO{}, err - } - if err := json.Unmarshal(contentJSON, &item.Content); err != nil { + if err := json.Unmarshal(nameJSON, &item.Title); err != nil { return paymentcard.PaymentCardDTO{}, err } return item, nil diff --git a/internal/admin/paymentcard/dto.go b/internal/admin/paymentcard/dto.go index 218b144..b9c4807 100644 --- a/internal/admin/paymentcard/dto.go +++ b/internal/admin/paymentcard/dto.go @@ -7,25 +7,25 @@ type LocalizedText struct { } type PaymentCardDTO struct { - Id int64 `json:"id"` - Price float64 `json:"price"` - Title LocalizedText `json:"title"` - Content LocalizedText `json:"content"` - Active bool `json:"active"` + Id int64 `json:"id"` + Price string `json:"price"` + Title LocalizedText `json:"title"` + Notes string `json:"notes"` + Active bool `json:"active"` } type CreatePaymentCardDTO struct { - Price float64 `json:"price" binding:"required"` - Title LocalizedText `json:"title" binding:"required"` - Content LocalizedText `json:"content" binding:"required"` - Active *bool `json:"active"` + Price string `json:"price" binding:"required"` + Title LocalizedText `json:"title" binding:"required"` + Notes string `json:"notes"` + Active *bool `json:"active"` } type UpdatePaymentCardDTO struct { - Price float64 `json:"price" binding:"required"` - Title LocalizedText `json:"title" binding:"required"` - Content LocalizedText `json:"content" binding:"required"` - Active bool `json:"active"` + Price string `json:"price" binding:"required"` + Title LocalizedText `json:"title" binding:"required"` + Notes string `json:"notes"` + Active bool `json:"active"` } type UpdatePaymentCardActiveDTO struct { diff --git a/internal/admin/reasonscard/db/postgresql.go b/internal/admin/reasonscard/db/postgresql.go index 6f4742e..f4b962a 100644 --- a/internal/admin/reasonscard/db/postgresql.go +++ b/internal/admin/reasonscard/db/postgresql.go @@ -23,9 +23,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) reasonscard return &repository{client: client, logger: logger} } -const reasonColumns = `r.id, r.title, r.price, r.content, r.created_at, r.updated_at` +const reasonColumns = `r.id, r.price, r.name, r.notes, r.active, r.created_at, r.updated_at` -const reasonReturningColumns = `id, title, price, content, created_at, updated_at` +const reasonReturningColumns = `id, price, name, notes, active, created_at, updated_at` func (r *repository) List(ctx context.Context, filter reasonscard.ListFilter) (reasonscard.ListReasonCardDTO, error) { fromClause, whereClause, args := buildListQuery(filter) @@ -65,14 +65,14 @@ func (r *repository) List(ctx context.Context, filter reasonscard.ListFilter) (r } func buildListQuery(filter reasonscard.ListFilter) (fromClause, whereClause string, args []interface{}) { - fromClause = ` FROM reasons_cards r` + fromClause = ` FROM card_states r` var conditions []string if filter.ReasonName != "" { args = append(args, likePattern(filter.ReasonName)) n := len(args) conditions = append(conditions, fmt.Sprintf(`( - r.title->>'en' ILIKE $%[1]d OR r.title->>'ru' ILIKE $%[1]d OR r.title->>'tm' ILIKE $%[1]d + r.name->>'en' ILIKE $%[1]d OR r.name->>'ru' ILIKE $%[1]d OR r.name->>'tm' ILIKE $%[1]d )`, n)) } @@ -91,7 +91,7 @@ func likePattern(term string) string { } func (r *repository) GetByID(ctx context.Context, id int64) (reasonscard.ReasonCardDTO, error) { - query := fmt.Sprintf(`SELECT %s FROM reasons_cards r WHERE r.id = $1`, reasonColumns) + query := fmt.Sprintf(`SELECT %s FROM card_states r WHERE r.id = $1`, reasonColumns) item, err := scanReasonCard(r.client.QueryRow(ctx, query, id)) if errors.Is(err, pgx.ErrNoRows) { return reasonscard.ReasonCardDTO{}, err @@ -100,33 +100,38 @@ func (r *repository) GetByID(ctx context.Context, id int64) (reasonscard.ReasonC } func (r *repository) Create(ctx context.Context, dto reasonscard.CreateReasonCardDTO) (reasonscard.ReasonCardDTO, error) { - title, content, err := marshalJSON(dto.Title, dto.Content) + name, err := marshalName(dto.Title) if err != nil { return reasonscard.ReasonCardDTO{}, err } + active := true + if dto.Active != nil { + active = *dto.Active + } + query := fmt.Sprintf(` - INSERT INTO reasons_cards (title, price, content) - VALUES ($1, $2, $3) + INSERT INTO card_states (price, name, notes, active) + VALUES ($1, $2, $3, $4) RETURNING %s`, reasonReturningColumns) - return scanReasonCard(r.client.QueryRow(ctx, query, title, dto.Price, content)) + return scanReasonCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, active)) } func (r *repository) Update(ctx context.Context, id int64, dto reasonscard.UpdateReasonCardDTO) (reasonscard.ReasonCardDTO, error) { - title, content, err := marshalJSON(dto.Title, dto.Content) + name, err := marshalName(dto.Title) if err != nil { return reasonscard.ReasonCardDTO{}, err } query := fmt.Sprintf(` - UPDATE reasons_cards SET - title = $1, price = $2, content = $3, + UPDATE card_states SET + price = $1, name = $2, notes = $3, active = $4, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') - WHERE id = $4 + WHERE id = $5 RETURNING %s`, reasonReturningColumns) - item, err := scanReasonCard(r.client.QueryRow(ctx, query, title, dto.Price, content, id)) + item, err := scanReasonCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, dto.Active, id)) if errors.Is(err, pgx.ErrNoRows) { return reasonscard.ReasonCardDTO{}, err } @@ -134,7 +139,7 @@ func (r *repository) Update(ctx context.Context, id int64, dto reasonscard.Updat } func (r *repository) Delete(ctx context.Context, id int64) error { - tag, err := r.client.Exec(ctx, `DELETE FROM reasons_cards WHERE id = $1`, id) + tag, err := r.client.Exec(ctx, `DELETE FROM card_states WHERE id = $1`, id) if err != nil { return err } @@ -144,16 +149,8 @@ func (r *repository) Delete(ctx context.Context, id int64) error { return nil } -func marshalJSON(title, content reasonscard.LocalizedText) ([]byte, []byte, error) { - titleJSON, err := json.Marshal(title) - if err != nil { - return nil, nil, err - } - contentJSON, err := json.Marshal(content) - if err != nil { - return nil, nil, err - } - return titleJSON, contentJSON, nil +func marshalName(title reasonscard.LocalizedText) ([]byte, error) { + return json.Marshal(title) } type scannable interface { @@ -162,17 +159,14 @@ type scannable interface { func scanReasonCard(row scannable) (reasonscard.ReasonCardDTO, error) { var item reasonscard.ReasonCardDTO - var titleJSON, contentJSON []byte + var nameJSON []byte if err := row.Scan( - &item.Id, &titleJSON, &item.Price, &contentJSON, &item.CreatedAt, &item.UpdatedAt, + &item.Id, &item.Price, &nameJSON, &item.Notes, &item.Active, &item.CreatedAt, &item.UpdatedAt, ); err != nil { return reasonscard.ReasonCardDTO{}, err } - if err := json.Unmarshal(titleJSON, &item.Title); err != nil { - return reasonscard.ReasonCardDTO{}, err - } - if err := json.Unmarshal(contentJSON, &item.Content); err != nil { + if err := json.Unmarshal(nameJSON, &item.Title); err != nil { return reasonscard.ReasonCardDTO{}, err } return item, nil diff --git a/internal/admin/reasonscard/dto.go b/internal/admin/reasonscard/dto.go index 344f3f0..4d5503c 100644 --- a/internal/admin/reasonscard/dto.go +++ b/internal/admin/reasonscard/dto.go @@ -11,22 +11,25 @@ type LocalizedText struct { type ReasonCardDTO struct { Id int64 `json:"id"` Title LocalizedText `json:"title"` - Price float64 `json:"price"` - Content LocalizedText `json:"content"` + Price string `json:"price"` + Notes string `json:"notes"` + Active bool `json:"active"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` } type CreateReasonCardDTO struct { - Title LocalizedText `json:"title" binding:"required"` - Price float64 `json:"price" binding:"required"` - Content LocalizedText `json:"content" binding:"required"` + Title LocalizedText `json:"title" binding:"required"` + Price string `json:"price" binding:"required"` + Notes string `json:"notes"` + Active *bool `json:"active"` } type UpdateReasonCardDTO struct { - Title LocalizedText `json:"title" binding:"required"` - Price float64 `json:"price" binding:"required"` - Content LocalizedText `json:"content" binding:"required"` + Title LocalizedText `json:"title" binding:"required"` + Price string `json:"price" binding:"required"` + Notes string `json:"notes"` + Active bool `json:"active"` } type ListFilter struct {