Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: (divided function in seperate files) #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"log"

hawqal "github.com/CapregSoft/Hawqal-go"
"github.com/CapregSoft/Hawqal-go/models"
hawqal "github.com/CapregSoft/Hawqal-go/hawqal"
// "github.com/CapregSoft/Hawqal-go/models"
)

func CheckError(err error) {
Expand All @@ -15,28 +15,28 @@ func CheckError(err error) {
}
func main() {

// countries, err := hawqal.GetCountries()
// CheckError(err)
// fmt.Print(string(countries))
countries, err := hawqal.GetCountries()
CheckError(err)
fmt.Print(string(countries))

// country, err := hawqal.GetCountry("pakistan")
// CheckError(err)
// fmt.Print(string(country))
country, err := hawqal.GetCountry("pakistan")
CheckError(err)
fmt.Print(string(country))

// states, err := hawqal.GetStates()
// CheckError(err)
// fmt.Print(string(states))
states, err := hawqal.GetStates()
CheckError(err)
fmt.Print(string(states))

state, err := hawqal.GetState("Punjab", &models.StateFilter{CountryName: "pakistan", Filter: &models.StateData{StateName: true}})
state, err := hawqal.GetState("punjab")
CheckError(err)
fmt.Print(string(state))

// cities, err := hawqal.GetCities()
// CheckError(err)
// fmt.Print(string(cities))
cities, err := hawqal.GetCities()
CheckError(err)
fmt.Print(string(cities))

// city, err := hawqal.GetCity("Lahore")
// CheckError(err)
// fmt.Print(string(city))
city, err := hawqal.GetCity("Lahore")
CheckError(err)
fmt.Print(string(city))

}
17 changes: 7 additions & 10 deletions filters/filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filter

import (
"log"
"reflect"
"strings"
)
Expand All @@ -21,13 +22,9 @@ func GetTrue(filter interface{}) string {
return strings.Join(fieldName, ", ")
}

// func GetTrue(filter interface{}) string {
// val := reflect.ValueOf(filter).Elem()
// var fieldName []string
// for i := 0; i < val.NumField(); i++ {
// if val.Field(i).Bool() {
// fieldName = append(fieldName, val.Type().Field(i).Name)
// }
// }
// return strings.Join(fieldName, ", ")
// }
// Function to Check Error
func CheckError(err error) {
if err != nil {
log.Fatal(err)
}
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ module github.com/CapregSoft/Hawqal-go
go 1.18

require (
github.com/jmoiron/sqlx v1.3.5
github.com/mattn/go-sqlite3 v1.14.16
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
109 changes: 109 additions & 0 deletions hawqal/city.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package hawqal

import (
"encoding/json"
"fmt"
"strings"

"github.com/CapregSoft/Hawqal-go/db"
filter "github.com/CapregSoft/Hawqal-go/filters"
"github.com/CapregSoft/Hawqal-go/models"
)

func GetCities(choice ...*models.CityFilter) ([]byte, error) {
var trueFields string
cities := make([]*models.Cities, 0)
if choice != nil {
if choice[0].Filter != nil {
userInput := filter.GetValue(choice[0].Filter)
if trueFields = filter.GetTrue(userInput); trueFields == "" {
trueFields = "*"
}
} else {
trueFields = "*"
}
} else {
trueFields = "*"
}
sqlQuery := "SELECT "
sqlQuery = sqlQuery + trueFields + " FROM cities "
if choice != nil {
if choice[0].CountryName != "" && choice[0].StateName != "" {
country_name := strings.Title(choice[0].CountryName)
state_name := strings.Title(choice[0].StateName)
sqlQuery = sqlQuery + fmt.Sprintf(" WHERE country_name = '%v' AND state_name = '%v'", country_name, state_name)
} else if choice[0].CountryName != "" {
country_name := strings.Title(choice[0].CountryName)
sqlQuery = sqlQuery + fmt.Sprintf(" WHERE country_name = '%v'", country_name)
} else if choice[0].StateName != "" {
state_name := strings.Title(choice[0].StateName)
sqlQuery = sqlQuery + fmt.Sprintf(" WHERE state_name = '%v'", state_name)
}
}
conn, err := db.DBConnection()
filter.CheckError(err)
defer conn.Close()
rows, err := conn.Queryx(sqlQuery)
filter.CheckError(err)
for rows.Next() {
var city models.Cities
if err := rows.StructScan(&city); err != nil {
fmt.Println(err)
}
cities = append(cities, &city)
}
defer rows.Close()
cityJson, err := json.MarshalIndent(cities, "", " ")
filter.CheckError(err)
return cityJson, nil
}

func GetCity(CityName string, choice ...*models.CityFilter) ([]byte, error) {
var trueFields string
cities := make([]*models.Cities, 0)
if choice != nil && choice[0].Filter != nil {
userInput := filter.GetValue(choice[0].Filter)
if trueFields = filter.GetTrue(userInput); trueFields == "" {
trueFields = "*"
}
} else {
trueFields = "*"
}
sqlQuery := "SELECT "
sqlQuery = sqlQuery + trueFields + " FROM cities "
if CityName != "" {
CityName = strings.Title(CityName)
sqlQuery = sqlQuery + fmt.Sprintf("WHERE city_name = '%v'", CityName)
} else {
return nil, fmt.Errorf("CityName must be set")
}
if choice != nil {
if choice[0].CountryName != "" && choice[0].StateName != "" {
country_name := strings.Title(choice[0].CountryName)
state_name := strings.Title(choice[0].StateName)
sqlQuery = sqlQuery + fmt.Sprintf(" AND country_name = '%v' AND state_name = '%v'", country_name, state_name)
} else if choice[0].CountryName != "" {
country_name := strings.Title(choice[0].CountryName)
sqlQuery = sqlQuery + fmt.Sprintf(" AND country_name = '%v'", country_name)
} else if choice[0].StateName != "" {
state_name := strings.Title(choice[0].StateName)
sqlQuery = sqlQuery + fmt.Sprintf(" AND state_name = '%v'", state_name)
}
}
conn, err := db.DBConnection()
filter.CheckError(err)
defer conn.Close()
rows, err := conn.Queryx(sqlQuery)
filter.CheckError(err)
for rows.Next() {
var city models.Cities
if err := rows.StructScan(&city); err != nil {
fmt.Println(err)
}
cities = append(cities, &city)
}
defer rows.Close()
cityJson, err := json.MarshalIndent(cities, "", " ")
filter.CheckError(err)
return cityJson, nil
}
80 changes: 80 additions & 0 deletions hawqal/country.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package hawqal

import (
"encoding/json"
"fmt"
"strings"

"github.com/CapregSoft/Hawqal-go/db"
filter "github.com/CapregSoft/Hawqal-go/filters"
"github.com/CapregSoft/Hawqal-go/models"
)

func GetCountries(choice ...*models.CountryFilter) ([]byte, error) {
var trueFields string
if choice != nil {
userInput := filter.GetValue(choice[0])
if trueFields = filter.GetTrue(userInput); trueFields == "" {
trueFields = "*"
}
} else {
trueFields = "*"
}
sqlQuery := "SELECT "
sqlQuery = sqlQuery + trueFields + " FROM countries "
countries := make([]*models.Countries, 0)
conn, err := db.DBConnection()
filter.CheckError(err)
defer conn.Close()
rows, err := conn.Queryx(sqlQuery)
filter.CheckError(err)
for rows.Next() {
var country models.Countries
if err := rows.StructScan(&country); err != nil {
fmt.Println(err)
}
countries = append(countries, &country)
}
defer rows.Close()
CountryJson, err := json.MarshalIndent(countries, "", " ")
filter.CheckError(err)
return CountryJson, nil
}

func GetCountry(CountryName string, choice ...*models.CountryFilter) ([]byte, error) {
var trueFields string
countries := make([]*models.Countries, 0)
if choice != nil {
userInput := filter.GetValue(choice[0])
if trueFields = filter.GetTrue(userInput); trueFields == "" {
trueFields = "*"
}
} else {
trueFields = "*"
}

sqlQuery := "SELECT "
sqlQuery = sqlQuery + trueFields + " FROM countries "
if CountryName != "" {
CountryName = strings.Title(CountryName)
sqlQuery = sqlQuery + fmt.Sprintf(" WHERE country_name = '%v'", CountryName)
} else {
return nil, fmt.Errorf("Country_name must be set")
}
conn, err := db.DBConnection()
filter.CheckError(err)
defer conn.Close()
rows, err := conn.Queryx(sqlQuery)
filter.CheckError(err)
for rows.Next() {
var country models.Countries
if err := rows.StructScan(&country); err != nil {
fmt.Println(err)
}
countries = append(countries, &country)
}
defer rows.Close()
CountryJson, err := json.MarshalIndent(countries, "", " ")
filter.CheckError(err)
return CountryJson, nil
}
Loading