fix(niki): remove is_main from address and add

ame to address
This commit is contained in:
Erfan Mohammadi 2024-05-30 16:27:33 +03:30
parent bdf197e84b
commit ed894c7520
10 changed files with 35 additions and 47 deletions

View File

@ -692,10 +692,6 @@ const docTemplate = `{
"type": "string", "type": "string",
"example": "tehran" "example": "tehran"
}, },
"benefactor_id": {
"type": "integer",
"example": 1
},
"city_id": { "city_id": {
"type": "integer", "type": "integer",
"example": 1 "example": 1
@ -708,6 +704,10 @@ const docTemplate = `{
"type": "number", "type": "number",
"example": 22.22 "example": 22.22
}, },
"name": {
"type": "string",
"example": "home"
},
"postal_code": { "postal_code": {
"type": "string", "type": "string",
"example": "1234567890" "example": "1234567890"
@ -1194,15 +1194,15 @@ const docTemplate = `{
"id": { "id": {
"type": "integer" "type": "integer"
}, },
"isMain": {
"type": "boolean"
},
"lat": { "lat": {
"type": "number" "type": "number"
}, },
"lon": { "lon": {
"type": "number" "type": "number"
}, },
"name": {
"type": "string"
},
"postalCode": { "postalCode": {
"type": "string" "type": "string"
}, },

View File

@ -681,10 +681,6 @@
"type": "string", "type": "string",
"example": "tehran" "example": "tehran"
}, },
"benefactor_id": {
"type": "integer",
"example": 1
},
"city_id": { "city_id": {
"type": "integer", "type": "integer",
"example": 1 "example": 1
@ -697,6 +693,10 @@
"type": "number", "type": "number",
"example": 22.22 "example": 22.22
}, },
"name": {
"type": "string",
"example": "home"
},
"postal_code": { "postal_code": {
"type": "string", "type": "string",
"example": "1234567890" "example": "1234567890"
@ -1183,15 +1183,15 @@
"id": { "id": {
"type": "integer" "type": "integer"
}, },
"isMain": {
"type": "boolean"
},
"lat": { "lat": {
"type": "number" "type": "number"
}, },
"lon": { "lon": {
"type": "number" "type": "number"
}, },
"name": {
"type": "string"
},
"postalCode": { "postalCode": {
"type": "string" "type": "string"
}, },

View File

@ -4,9 +4,6 @@ definitions:
address: address:
example: tehran example: tehran
type: string type: string
benefactor_id:
example: 1
type: integer
city_id: city_id:
example: 1 example: 1
type: integer type: integer
@ -16,6 +13,9 @@ definitions:
lon: lon:
example: 22.22 example: 22.22
type: number type: number
name:
example: home
type: string
postal_code: postal_code:
example: "1234567890" example: "1234567890"
type: string type: string
@ -331,12 +331,12 @@ definitions:
type: integer type: integer
id: id:
type: integer type: integer
isMain:
type: boolean
lat: lat:
type: number type: number
lon: lon:
type: number type: number
name:
type: string
postalCode: postalCode:
type: string type: string
provinceID: provinceID:

View File

@ -4,9 +4,9 @@ type Address struct {
ID uint ID uint
PostalCode string PostalCode string
Address string Address string
Name string
Lat float64 Lat float64
Lon float64 Lon float64
IsMain bool
CityID uint CityID uint
ProvinceID uint ProvinceID uint
BenefactorID uint BenefactorID uint

View File

@ -8,8 +8,8 @@ type BenefactorAddAddressRequest struct {
Lat float64 `json:"lat" example:"22.23"` Lat float64 `json:"lat" example:"22.23"`
Lon float64 `json:"lon" example:"22.22"` Lon float64 `json:"lon" example:"22.22"`
CityID uint `json:"city_id" example:"1"` CityID uint `json:"city_id" example:"1"`
ProvinceID uint `json:"-"` BenefactorID uint `json:"-"`
BenefactorID uint `json:"benefactor_id" example:"1"` Name string `json:"name" example:"home"`
} }
type BenefactorAddAddressResponse struct { type BenefactorAddAddressResponse struct {

View File

@ -20,20 +20,10 @@ func (d *DB) CreateBenefactorAddress(ctx context.Context, address entity.Address
WithMessage("error querying for existing main address").WithKind(richerror.KindUnexpected) WithMessage("error querying for existing main address").WithKind(richerror.KindUnexpected)
} }
address.ProvinceID = provinceID 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 // 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 (?, ?, ?, ?, ?, ?, ?, ?)`, 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.IsMain, address.CityID, provinceID, address.BenefactorID) address.PostalCode, address.Address, address.Lat, address.Lon, address.Name, address.CityID, provinceID, address.BenefactorID)
if err != nil { if err != nil {
return entity.Address{}, richerror.New(op).WithErr(err). return entity.Address{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected) WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)

View File

@ -40,7 +40,7 @@ func scanAddress(scanner mysql.Scanner) (entity.Address, error) {
var address entity.Address var address entity.Address
err := scanner.Scan(&address.ID, &address.PostalCode, &address.Address, &address.Lat, &address.Lon, 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) &createdAt, &updatedAt)
return address, err return address, err

View File

@ -5,7 +5,7 @@ CREATE TABLE `addresses` (
`address` TEXT NOT NULL, `address` TEXT NOT NULL,
`lat` FLOAT, `lat` FLOAT,
`lon` FLOAT, `lon` FLOAT,
`is_main` BOOL NOT NULL DEFAULT FALSE, `name` VARCHAR(191) NOT NULL,
`city_id` INT NOT NULL, `city_id` INT NOT NULL,
`province_id` INT NOT NULL, `province_id` INT NOT NULL,
`benefactor_id` INT NOT NULL, `benefactor_id` INT NOT NULL,

View File

@ -14,6 +14,7 @@ func (s Service) Add(ctx context.Context, req param.BenefactorAddAddressRequest)
address, err := s.repo.CreateBenefactorAddress(ctx, entity.Address{ address, err := s.repo.CreateBenefactorAddress(ctx, entity.Address{
PostalCode: req.PostalCode, PostalCode: req.PostalCode,
Address: req.Address, Address: req.Address,
Name: req.Name,
Lat: req.Lat, Lat: req.Lat,
Lon: req.Lon, Lon: req.Lon,
CityID: req.CityID, CityID: req.CityID,

View File

@ -15,21 +15,18 @@ func (v Validator) ValidateAddAddress(req param.BenefactorAddAddressRequest) (ma
validation.Field(&req.Address, validation.Required), validation.Field(&req.Address, validation.Required),
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID, validation.Required,
validation.Required,
validation.By(v.doesBenefactorExist)), validation.By(v.doesBenefactorExist)),
validation.Field(&req.Lon, validation.Field(&req.Lon, validation.Required),
validation.Required),
validation.Field(&req.Lat, validation.Field(&req.Lat, validation.Required),
validation.Required),
validation.Field(&req.PostalCode, validation.Field(&req.Name, validation.Required),
validation.Required,
), validation.Field(&req.PostalCode, validation.Required),
validation.Field(&req.CityID,
validation.Required, validation.Field(&req.CityID, validation.Required,
validation.By(v.doesCityExist)), validation.By(v.doesCityExist)),
); err != nil { ); err != nil {