Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment 1
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aksel Baardsen
Assignment 1
Commits
c13fefdc
Commit
c13fefdc
authored
Oct 3, 2019
by
Aksel Baardsen
Browse files
Options
Downloads
Patches
Plain Diff
getting species by countries are now unique, not sure if is a part of the assignment
parent
bac7e75b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
models/countries.go
+27
-64
27 additions, 64 deletions
models/countries.go
with
27 additions
and
64 deletions
models/countries.go
+
27
−
64
View file @
c13fefdc
...
...
@@ -33,87 +33,51 @@ type Results struct {
}
func
GetCountryByCode
(
code
,
limit
string
)
Country
{
var
x
Country
url
:=
countryApi
+
code
cBody
:=
getString
(
url
)
err
:=
json
.
Unmarshal
([]
byte
(
cBody
),
&
x
)
// gets country info
var
country
Country
urlCountry
:=
countryApi
+
code
cBody
:=
getString
(
urlCountry
)
err
:=
json
.
Unmarshal
([]
byte
(
cBody
),
&
country
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
/*
resp, err:= http.Get(url)
// gets species in that country
var
species
Response
urlSpecies
:=
occurrenceApi
+
"country="
+
code
+
"&limit="
+
limit
rBody
:=
getString
(
urlSpecies
)
err
=
json
.
Unmarshal
([]
byte
(
rBody
),
&
species
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
err = json.Unmarshal([]byte(bodyString), &x)
if err != nil {
log.Fatal(err)
}
//log.Info(bodyString)
// adds specieskeys&specienames to the country struct
// and checks if the countrycode is correct
for
i
,
_
:=
range
species
.
Results
{
//i := 0; i < len(species.Results); i++ {
if
country
.
Code
==
species
.
Results
[
i
]
.
CountryCode
{
if
species
.
Results
[
i
]
.
SpeciesKey
!=
0
||
contains
(
country
.
SpeciesKey
,
species
.
Results
[
i
]
.
SpeciesKey
)
{
country
.
Species
=
append
(
country
.
Species
,
species
.
Results
[
i
]
.
Species
)
country
.
SpeciesKey
=
append
(
country
.
SpeciesKey
,
species
.
Results
[
i
]
.
SpeciesKey
)
}
*/
// #########################################################################################
var
y
Response
url2
:=
occurrenceApi
+
"country="
+
code
if
len
(
limit
)
>
0
{
url2
+=
"&limit="
+
limit
}
else
{
url2
+=
"&limit=5"
}
/*
resp2, err := http.Get(url2)
if err != nil {
log.Fatal(err)
}
defer resp2.Body.Close()
if resp2.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp2.Body)
if err != nil {
log.Fatal(err)
}
return
country
bodyString := string(bodyBytes)
err = json.Unmarshal([]byte(bodyString), &y)
if err != nil {
log.Fatal(err)
}
//log.Info(bodyString)
}
*/
rBody
:=
getString
(
url2
)
err
=
json
.
Unmarshal
([]
byte
(
rBody
),
&
y
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
func
contains
(
s
[]
int
,
e
int
)
bool
{
for
_
,
a
:=
range
s
{
if
a
==
e
{
return
true
}
//adds specieskeys&specienames to the country struct
// NEEDS CHECK IF SPECIES COUNTRY IS CORRECT !!
for
i
:=
0
;
i
<
len
(
y
.
Results
);
i
++
{
if
x
.
Code
==
y
.
Results
[
i
]
.
CountryCode
{
x
.
Species
=
append
(
x
.
Species
,
y
.
Results
[
i
]
.
Species
)
x
.
SpeciesKey
=
append
(
x
.
SpeciesKey
,
y
.
Results
[
i
]
.
SpeciesKey
)
}
return
false
}
return
x
}
//returns json ready for parsing as string
func
getString
(
url
string
)
string
{
resp
,
err
:=
http
.
Get
(
url
)
...
...
@@ -134,6 +98,5 @@ func getString(url string) string {
return
"err0r"
}
return
bodyString
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment