From ed894c752019c3ccffe18f5aeb0f28aae61783e2 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Thu, 30 May 2024 16:27:33 +0330 Subject: [PATCH] fix(niki): remove is_main from address and add ame to address --- docs/docs.go | 14 +++++++------- docs/swagger.json | 14 +++++++------- docs/swagger.yaml | 10 +++++----- entity/address.go | 2 +- param/benefactor/address/add.go | 4 ++-- repository/mysql/address/create.go | 14 ++------------ repository/mysql/address/get.go | 2 +- .../1705399370_create_addresses_table.sql | 2 +- service/benefactor/address/add.go | 1 + validator/benefactor/address/add_address.go | 19 ++++++++----------- 10 files changed, 35 insertions(+), 47 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 12ecbb6..7311683 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -692,10 +692,6 @@ const docTemplate = `{ "type": "string", "example": "tehran" }, - "benefactor_id": { - "type": "integer", - "example": 1 - }, "city_id": { "type": "integer", "example": 1 @@ -708,6 +704,10 @@ const docTemplate = `{ "type": "number", "example": 22.22 }, + "name": { + "type": "string", + "example": "home" + }, "postal_code": { "type": "string", "example": "1234567890" @@ -1194,15 +1194,15 @@ const docTemplate = `{ "id": { "type": "integer" }, - "isMain": { - "type": "boolean" - }, "lat": { "type": "number" }, "lon": { "type": "number" }, + "name": { + "type": "string" + }, "postalCode": { "type": "string" }, diff --git a/docs/swagger.json b/docs/swagger.json index 3de7854..ececda1 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -681,10 +681,6 @@ "type": "string", "example": "tehran" }, - "benefactor_id": { - "type": "integer", - "example": 1 - }, "city_id": { "type": "integer", "example": 1 @@ -697,6 +693,10 @@ "type": "number", "example": 22.22 }, + "name": { + "type": "string", + "example": "home" + }, "postal_code": { "type": "string", "example": "1234567890" @@ -1183,15 +1183,15 @@ "id": { "type": "integer" }, - "isMain": { - "type": "boolean" - }, "lat": { "type": "number" }, "lon": { "type": "number" }, + "name": { + "type": "string" + }, "postalCode": { "type": "string" }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 51cc7f5..c836e84 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -4,9 +4,6 @@ definitions: address: example: tehran type: string - benefactor_id: - example: 1 - type: integer city_id: example: 1 type: integer @@ -16,6 +13,9 @@ definitions: lon: example: 22.22 type: number + name: + example: home + type: string postal_code: example: "1234567890" type: string @@ -331,12 +331,12 @@ definitions: type: integer id: type: integer - isMain: - type: boolean lat: type: number lon: type: number + name: + type: string postalCode: type: string provinceID: diff --git a/entity/address.go b/entity/address.go index 8a4ac1d..608885d 100644 --- a/entity/address.go +++ b/entity/address.go @@ -4,9 +4,9 @@ type Address struct { ID uint PostalCode string Address string + Name string Lat float64 Lon float64 - IsMain bool CityID uint ProvinceID uint BenefactorID uint diff --git a/param/benefactor/address/add.go b/param/benefactor/address/add.go index 37484a9..50f96a1 100644 --- a/param/benefactor/address/add.go +++ b/param/benefactor/address/add.go @@ -8,8 +8,8 @@ type BenefactorAddAddressRequest struct { Lat float64 `json:"lat" example:"22.23"` Lon float64 `json:"lon" example:"22.22"` CityID uint `json:"city_id" example:"1"` - ProvinceID uint `json:"-"` - BenefactorID uint `json:"benefactor_id" example:"1"` + BenefactorID uint `json:"-"` + Name string `json:"name" example:"home"` } type BenefactorAddAddressResponse struct { diff --git a/repository/mysql/address/create.go b/repository/mysql/address/create.go index 036a5bb..e84561c 100644 --- a/repository/mysql/address/create.go +++ b/repository/mysql/address/create.go @@ -20,20 +20,10 @@ func (d *DB) CreateBenefactorAddress(ctx context.Context, address entity.Address WithMessage("error querying for existing main address").WithKind(richerror.KindUnexpected) } address.ProvinceID = provinceID - // Check if the user already has a main address - var count int - err := d.conn.Conn().QueryRowContext(ctx, `SELECT COUNT(*) FROM addresses WHERE is_main = true AND benefactor_id = ?`, address.BenefactorID).Scan(&count) - if err != nil && err != sql.ErrNoRows { - return entity.Address{}, richerror.New(op).WithErr(err). - WithMessage("error querying for existing main address").WithKind(richerror.KindUnexpected) - } - - // Set is_main field based on the count - address.IsMain = count == 0 // Insert the new address - res, err := d.conn.Conn().ExecContext(ctx, `INSERT INTO addresses (postal_code, address, lat, lon, is_main, city_id, province_id, benefactor_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, - address.PostalCode, address.Address, address.Lat, address.Lon, address.IsMain, address.CityID, provinceID, address.BenefactorID) + res, err := d.conn.Conn().ExecContext(ctx, `INSERT INTO addresses (postal_code, address, lat, lon, name, city_id, province_id, benefactor_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, + address.PostalCode, address.Address, address.Lat, address.Lon, address.Name, address.CityID, provinceID, address.BenefactorID) if err != nil { return entity.Address{}, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected) diff --git a/repository/mysql/address/get.go b/repository/mysql/address/get.go index c6e9931..e4cccfb 100644 --- a/repository/mysql/address/get.go +++ b/repository/mysql/address/get.go @@ -40,7 +40,7 @@ func scanAddress(scanner mysql.Scanner) (entity.Address, error) { var address entity.Address err := scanner.Scan(&address.ID, &address.PostalCode, &address.Address, &address.Lat, &address.Lon, - &address.IsMain, &address.CityID, &address.ProvinceID, &address.BenefactorID, + &address.Name, &address.CityID, &address.ProvinceID, &address.BenefactorID, &createdAt, &updatedAt) return address, err diff --git a/repository/mysql/migration/1705399370_create_addresses_table.sql b/repository/mysql/migration/1705399370_create_addresses_table.sql index 729b869..8ec48ea 100644 --- a/repository/mysql/migration/1705399370_create_addresses_table.sql +++ b/repository/mysql/migration/1705399370_create_addresses_table.sql @@ -5,7 +5,7 @@ CREATE TABLE `addresses` ( `address` TEXT NOT NULL, `lat` FLOAT, `lon` FLOAT, - `is_main` BOOL NOT NULL DEFAULT FALSE, + `name` VARCHAR(191) NOT NULL, `city_id` INT NOT NULL, `province_id` INT NOT NULL, `benefactor_id` INT NOT NULL, diff --git a/service/benefactor/address/add.go b/service/benefactor/address/add.go index e77db5b..8fb6a8c 100644 --- a/service/benefactor/address/add.go +++ b/service/benefactor/address/add.go @@ -14,6 +14,7 @@ func (s Service) Add(ctx context.Context, req param.BenefactorAddAddressRequest) address, err := s.repo.CreateBenefactorAddress(ctx, entity.Address{ PostalCode: req.PostalCode, Address: req.Address, + Name: req.Name, Lat: req.Lat, Lon: req.Lon, CityID: req.CityID, diff --git a/validator/benefactor/address/add_address.go b/validator/benefactor/address/add_address.go index 56a5315..7014fbf 100644 --- a/validator/benefactor/address/add_address.go +++ b/validator/benefactor/address/add_address.go @@ -15,21 +15,18 @@ func (v Validator) ValidateAddAddress(req param.BenefactorAddAddressRequest) (ma validation.Field(&req.Address, validation.Required), - validation.Field(&req.BenefactorID, - validation.Required, + validation.Field(&req.BenefactorID, validation.Required, validation.By(v.doesBenefactorExist)), - validation.Field(&req.Lon, - validation.Required), + validation.Field(&req.Lon, validation.Required), - validation.Field(&req.Lat, - validation.Required), + validation.Field(&req.Lat, validation.Required), - validation.Field(&req.PostalCode, - validation.Required, - ), - validation.Field(&req.CityID, - validation.Required, + validation.Field(&req.Name, validation.Required), + + validation.Field(&req.PostalCode, validation.Required), + + validation.Field(&req.CityID, validation.Required, validation.By(v.doesCityExist)), ); err != nil {