Skip to content

Commit fb1b846

Browse files
committed
implement search by ID
1 parent fc0c58f commit fb1b846

File tree

11 files changed

+488
-25
lines changed

11 files changed

+488
-25
lines changed

cmd/communes.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,27 @@ Cobra is a CLI library for Go that empowers applications.
4646
This application is a tool to generate the needed files
4747
to quickly create a Cobra application.`,
4848
Run: func(cmd *cobra.Command, args []string) {
49+
id, _ := cmd.Flags().GetString("id")
4950
outputInJSON, _ := cmd.Flags().GetBool("json")
51+
52+
c := mada.NewCommuneService()
53+
54+
if id != "" {
55+
c.ShowCommune(id, outputInJSON)
56+
return
57+
}
58+
5059
limit, _ := cmd.Flags().GetInt("limit")
51-
c := mada.Commune{}
60+
5261
c.List(outputInJSON, limit)
5362
},
5463
}
5564

5665
func init() {
5766
rootCmd.AddCommand(communesCmd)
5867
communesCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
59-
communesCmd.Flags().Int("limit", 100, "Limit the number of communes")
68+
communesCmd.Flags().IntP("limit", "l", 100, "Limit the number of communes")
69+
communesCmd.Flags().StringP("id", "i", "", "Commune ID")
6070

6171
// Here you will define your flags and configuration settings.
6272

cmd/districts.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,26 @@ Cobra is a CLI library for Go that empowers applications.
4646
This application is a tool to generate the needed files
4747
to quickly create a Cobra application.`,
4848
Run: func(cmd *cobra.Command, args []string) {
49+
id, _ := cmd.Flags().GetString("id")
4950
outputInJSON, _ := cmd.Flags().GetBool("json")
5051
limit, _ := cmd.Flags().GetInt("limit")
51-
d := mada.District{}
52+
53+
d := mada.NewDistrictService()
54+
55+
if id != "" {
56+
d.ShowDistrict(id, outputInJSON)
57+
return
58+
}
59+
5260
d.List(outputInJSON, limit)
5361
},
5462
}
5563

5664
func init() {
5765
rootCmd.AddCommand(districtsCmd)
5866
districtsCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
59-
districtsCmd.Flags().Int("limit", 100, "Limit the number of communes")
67+
districtsCmd.Flags().IntP("limit", "l", 100, "Limit the number of communes")
68+
districtsCmd.Flags().StringP("id", "i", "", "District ID")
6069

6170
// Here you will define your flags and configuration settings.
6271

cmd/fokontany.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,26 @@ Cobra is a CLI library for Go that empowers applications.
4646
This application is a tool to generate the needed files
4747
to quickly create a Cobra application.`,
4848
Run: func(cmd *cobra.Command, args []string) {
49+
id, _ := cmd.Flags().GetString("id")
4950
outputInJSON, _ := cmd.Flags().GetBool("json")
5051
limit, _ := cmd.Flags().GetInt("limit")
51-
f := mada.Fokontany{}
52+
53+
f := mada.NewFokontanyService()
54+
55+
if id != "" {
56+
f.ShowFokontany(id, outputInJSON)
57+
return
58+
}
59+
5260
f.List(outputInJSON, limit)
5361
},
5462
}
5563

5664
func init() {
5765
rootCmd.AddCommand(fokontanyCmd)
5866
fokontanyCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
59-
fokontanyCmd.Flags().Int("limit", 100, "Limit the number of communes")
67+
fokontanyCmd.Flags().IntP("limit", "l", 100, "Limit the number of communes")
68+
fokontanyCmd.Flags().StringP("id", "i", "", "Fokontany ID")
6069

6170
// Here you will define your flags and configuration settings.
6271

cmd/regions.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,26 @@ Cobra is a CLI library for Go that empowers applications.
4646
This application is a tool to generate the needed files
4747
to quickly create a Cobra application.`,
4848
Run: func(cmd *cobra.Command, args []string) {
49+
id, _ := cmd.Flags().GetString("id")
4950
outputInJSON, _ := cmd.Flags().GetBool("json")
5051
limit, _ := cmd.Flags().GetInt("limit")
51-
r := mada.Region{}
52+
53+
r := mada.NewRegionService()
54+
55+
if id != "" {
56+
r.ShowRegion(id, outputInJSON)
57+
return
58+
}
59+
5260
r.List(outputInJSON, limit)
5361
},
5462
}
5563

5664
func init() {
5765
rootCmd.AddCommand(regionsCmd)
5866
regionsCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
59-
regionsCmd.Flags().Int("limit", 100, "Limit the number of communes")
67+
regionsCmd.Flags().IntP("limit", "l", 100, "Limit the number of communes")
68+
regionsCmd.Flags().StringP("id", "i", "", "Region ID")
6069

6170
// Here you will define your flags and configuration settings.
6271

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ require (
2828
github.com/golang/protobuf v1.5.2 // indirect
2929
github.com/golang/snappy v0.0.4 // indirect
3030
github.com/inconshreveable/mousetrap v1.0.0 // indirect
31+
github.com/jmoiron/sqlx v1.3.5 // indirect
3132
github.com/lib/pq v1.10.6 // indirect
3233
github.com/mattn/go-colorable v0.1.12 // indirect
3334
github.com/mattn/go-isatty v0.0.14 // indirect
3435
github.com/mattn/go-runewidth v0.0.13 // indirect
36+
github.com/mattn/go-sqlite3 v1.14.13 // indirect
3537
github.com/mitchellh/go-homedir v1.1.0 // indirect
3638
github.com/mschoch/smat v0.2.0 // indirect
3739
github.com/nats-io/nats.go v1.15.0 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ github.com/everystreet/go-shapefile v1.0.0/go.mod h1:bnFakeByxvu+JTjGvMLo0xCI+XL
7676
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
7777
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
7878
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
79+
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
7980
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
8081
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551 h1:gtexQ/VGyN+VVFRXSFiguSNcXmS6rkKT+X7FdIrTtfo=
8182
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
@@ -98,6 +99,8 @@ github.com/huandu/xstrings v1.3.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq
9899
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
99100
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
100101
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
102+
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
103+
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
101104
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
102105
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
103106
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -107,6 +110,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
107110
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
108111
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
109112
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
113+
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
110114
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
111115
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
112116
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
@@ -121,6 +125,9 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
121125
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
122126
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
123127
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
128+
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
129+
github.com/mattn/go-sqlite3 v1.14.13 h1:1tj15ngiFfcZzii7yd82foL+ks+ouQcj8j/TPq3fk1I=
130+
github.com/mattn/go-sqlite3 v1.14.13/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
124131
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
125132
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
126133
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=

mada/commune.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
package mada
22

33
import (
4+
"database/sql"
45
"encoding/json"
56
"fmt"
67

78
"github.com/blevesearch/bleve/v2"
89
)
910

10-
type Commune struct{}
11+
type Commune struct {
12+
ID string `json:"id"`
13+
Name string `json:"name"`
14+
Region string `json:"region"`
15+
District string `json:"district"`
16+
Country string `json:"country"`
17+
}
18+
19+
type CommuneService struct {
20+
db *sql.DB
21+
}
1122

12-
func (c *Commune) List(outputInJSON bool, limit int) {
23+
func NewCommuneService() *CommuneService {
24+
db, err := OpenSQLiteConnection()
25+
26+
if err != nil {
27+
panic(err)
28+
}
29+
30+
return &CommuneService{db: db}
31+
}
32+
33+
func (c *CommuneService) List(outputInJSON bool, limit int) {
1334
index, err := InitializeBleve()
1435
if err != nil {
1536
panic(err)
@@ -34,3 +55,40 @@ func (c *Commune) List(outputInJSON bool, limit int) {
3455

3556
fmt.Println(string(b))
3657
}
58+
59+
func (c *CommuneService) ShowCommune(id string, outputInJSON bool) {
60+
rows, _ := c.db.Query("SELECT uid, name, region, district, country FROM commune WHERE uid = ?", id)
61+
defer rows.Close()
62+
var uid, name, region, district, country string
63+
rows.Next()
64+
rows.Scan(&uid, &name, &region, &district, &country)
65+
66+
if outputInJSON {
67+
b, _ := json.MarshalIndent(Commune{
68+
ID: uid,
69+
Name: name,
70+
Region: region,
71+
District: district,
72+
Country: country,
73+
}, "", " ")
74+
75+
fmt.Println(string(b))
76+
return
77+
}
78+
79+
fmt.Printf(`
80+
id
81+
%s
82+
name
83+
%s
84+
district
85+
%s
86+
87+
region
88+
%s
89+
type
90+
commune
91+
country
92+
Madagascar
93+
`, uid, name, district, region)
94+
}

mada/district.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
package mada
22

33
import (
4+
"database/sql"
45
"encoding/json"
56
"fmt"
67

78
"github.com/blevesearch/bleve/v2"
89
)
910

10-
type District struct{}
11+
type District struct {
12+
ID string `json:"id"`
13+
Name string `json:"name"`
14+
Region string `json:"region"`
15+
Country string `json:"country"`
16+
}
17+
18+
type DistrictService struct {
19+
db *sql.DB
20+
}
21+
22+
func NewDistrictService() *DistrictService {
23+
db, err := OpenSQLiteConnection()
24+
25+
if err != nil {
26+
panic(err)
27+
}
1128

12-
func (d *District) List(outputInJSON bool, limit int) {
29+
return &DistrictService{db: db}
30+
}
31+
32+
func (d *DistrictService) List(outputInJSON bool, limit int) {
1333
index, err := InitializeBleve()
1434
if err != nil {
1535
panic(err)
@@ -34,3 +54,31 @@ func (d *District) List(outputInJSON bool, limit int) {
3454

3555
fmt.Println(string(b))
3656
}
57+
58+
func (d *DistrictService) ShowDistrict(id string, outputInJSON bool) {
59+
rows, _ := d.db.Query("SELECT uid, name, region FROM district WHERE uid = ?", id)
60+
defer rows.Close()
61+
var uid, name, region string
62+
rows.Next()
63+
rows.Scan(&uid, &name, &region)
64+
65+
if outputInJSON {
66+
b, _ := json.MarshalIndent(District{ID: uid, Name: name, Region: region, Country: "Madagascar"}, "", " ")
67+
fmt.Println(string(b))
68+
return
69+
}
70+
71+
fmt.Printf(`
72+
id
73+
%s
74+
name
75+
%s
76+
77+
region
78+
%s
79+
type
80+
district
81+
country
82+
Madagascar
83+
`, uid, name, region)
84+
}

mada/fokontany.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
package mada
22

33
import (
4+
"database/sql"
45
"encoding/json"
56
"fmt"
67

78
"github.com/blevesearch/bleve/v2"
89
)
910

10-
type Fokontany struct{}
11+
type Fokontany struct {
12+
ID string `json:"id"`
13+
Name string `json:"name"`
14+
Commune string `json:"commune"`
15+
Region string `json:"region"`
16+
District string `json:"district"`
17+
Country string `json:"country"`
18+
}
19+
20+
type FokontanyService struct {
21+
db *sql.DB
22+
}
23+
24+
func NewFokontanyService() *FokontanyService {
25+
db, err := OpenSQLiteConnection()
26+
27+
if err != nil {
28+
panic(err)
29+
}
1130

12-
func (f *Fokontany) List(outputInJSON bool, limit int) {
31+
return &FokontanyService{db: db}
32+
}
33+
34+
func (f *FokontanyService) List(outputInJSON bool, limit int) {
1335
index, err := InitializeBleve()
1436
if err != nil {
1537
panic(err)
@@ -34,3 +56,42 @@ func (f *Fokontany) List(outputInJSON bool, limit int) {
3456

3557
fmt.Println(string(b))
3658
}
59+
60+
func (f *FokontanyService) ShowFokontany(id string, outputInJSON bool) {
61+
rows, _ := f.db.Query("SELECT uid, name, commune, region, district, country FROM fokontany WHERE uid = ?", id)
62+
defer rows.Close()
63+
var uid, name, commune, district, region, country string
64+
rows.Next()
65+
rows.Scan(&uid, &name, &commune, &region, &district, &country)
66+
67+
if outputInJSON {
68+
b, _ := json.MarshalIndent(Fokontany{
69+
ID: uid,
70+
Name: name,
71+
Commune: commune,
72+
Region: region,
73+
District: district,
74+
Country: country,
75+
}, "", " ")
76+
fmt.Println(string(b))
77+
return
78+
}
79+
fmt.Printf(`
80+
id
81+
%s
82+
name
83+
%s
84+
commune
85+
%s
86+
district
87+
%s
88+
region
89+
%s
90+
country
91+
%s
92+
type
93+
fokontany
94+
country
95+
Madagascar
96+
`, uid, name, commune, region, district, country)
97+
}

0 commit comments

Comments
 (0)