forked from ebhomengo/niki
1
0
Fork 0

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",
"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"
},

View File

@ -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"
},

View File

@ -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:

View File

@ -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

View File

@ -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 {

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)
}
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)

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -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 {