Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
invinoveritas
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
gbe
invinoveritas
Commits
07db2275
Commit
07db2275
authored
3 years ago
by
gbe
Browse files
Options
Downloads
Patches
Plain Diff
Store ISO3 country codes
parent
0fca37a0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
handler-details.go
+10
-3
10 additions, 3 deletions
handler-details.go
templates/details.tpl
+5
-5
5 additions, 5 deletions
templates/details.tpl
templates/index.tpl
+7
-3
7 additions, 3 deletions
templates/index.tpl
vino.go
+48
-0
48 additions, 0 deletions
vino.go
with
70 additions
and
11 deletions
handler-details.go
+
10
−
3
View file @
07db2275
...
...
@@ -84,6 +84,12 @@ func (h Handler) detailsPost(w http.ResponseWriter, r *http.Request) {
return
}
v
,
err
:=
LoadVino
(
r
.
Context
(),
h
.
db
,
id
)
if
err
!=
nil
{
// Only log error here, just create a new entry.
log
.
Println
(
"can't load vino with id"
,
id
,
":"
,
err
)
}
name
:=
r
.
FormValue
(
"name"
)
if
name
==
""
{
httpError
(
w
,
"name can't be empty"
,
nil
,
http
.
StatusBadRequest
)
...
...
@@ -96,14 +102,15 @@ func (h Handler) detailsPost(w http.ResponseWriter, r *http.Request) {
return
}
v
,
err
:=
LoadV
in
o
(
r
.
Context
(),
h
.
db
,
id
)
country
,
err
:=
ISO3CountryFromStr
in
g
(
r
.
FormValue
(
"country"
)
)
if
err
!=
nil
{
// Only log error here, just create a new entry.
log
.
Println
(
"can't load vino with id"
,
id
,
":"
,
err
)
httpError
(
w
,
"can't parse country"
,
nil
,
http
.
StatusBadRequest
)
return
}
v
.
Name
=
name
v
.
Rating
=
rating
v
.
Country
=
country
comment
:=
strings
.
TrimSpace
(
r
.
FormValue
(
"comment"
))
if
comment
!=
""
{
...
...
This diff is collapsed.
Click to expand it.
templates/details.tpl
+
5
−
5
View file @
07db2275
...
...
@@ -15,11 +15,6 @@
<input
type=
"hidden"
name=
"id"
value=
"{{ .Wine.ID }}"
>
<fieldset>
<div
class=
"pure-control-group"
>
<label
for=
"form-id"
>
ID
</label>
<input
type=
"text"
id=
"form-id"
value=
"{{ .Wine.ID }}"
disabled
>
</div>
<div
class=
"pure-control-group"
>
<label
for=
"form-name"
>
Name
</label>
<input
id=
"form-name"
type=
"text"
name=
"name"
value=
"{{ .Wine.Name }}"
placeholder=
"Name"
>
...
...
@@ -30,6 +25,11 @@
<input
id=
"form-rating"
type=
"number"
name=
"rating"
value=
"{{ .Wine.Rating }}"
placeholder=
"Rating"
>
</div>
<div
class=
"pure-control-group"
>
<label
for=
"form-country"
>
Country (ISO3)
</label>
<input
id=
"form-country"
type=
"text"
name=
"country"
value=
"{{ .Wine.Country }}"
placeholder=
"XXX"
pattern=
"[A-Z]
{
3
}
"
>
</div>
<div
class=
"pure-control-group"
>
<label
for=
"form-new-picture"
>
New picture
</label>
<input
id=
"form-new-picture"
type=
"file"
name=
"picture"
accept=
"image/png, image/jpeg"
>
...
...
This diff is collapsed.
Click to expand it.
templates/index.tpl
+
7
−
3
View file @
07db2275
...
...
@@ -8,13 +8,17 @@
<div
class=
"content withfooter pure-u-1"
>
<div
class=
"maxwidth box"
>
<table
class=
"pure-table"
>
<thead><tr><th>
#
</th><th>
Rating
</th><th
class=
"fill"
>
Name
</th></tr></thead>
<thead><tr>
<th
class=
"fill"
>
Name
</th>
<th>
Rating
</th>
<th>
Country
</th>
</tr></thead>
<tbody>
{{ range .Wines }}
<tr>
<td><a
href=
"/details?id={{ .ID }}"
>
{{ .ID }}
</a></td>
<td>
{{ .Rating }}
</td>
<td><a
href=
"/details?id={{ .ID }}"
>
{{ .Name }}
</a></td>
<td>
{{ .Rating }}
</td>
<td>
{{ .Country }}
</td>
</tr>
{{ end }}
</tbody>
...
...
This diff is collapsed.
Click to expand it.
vino.go
+
48
−
0
View file @
07db2275
...
...
@@ -28,10 +28,46 @@ type Comment struct {
Content
string
}
type
ISO3CountryCode
[
3
]
byte
var
UnknownCountry
=
ISO3CountryCode
{
'X'
,
'X'
,
'X'
}
func
ISO3CountryFromString
(
s
string
)
(
ISO3CountryCode
,
error
)
{
if
len
(
s
)
==
0
{
return
UnknownCountry
,
nil
}
if
len
(
s
)
!=
3
{
return
UnknownCountry
,
errors
.
New
(
"unexpected length"
)
}
return
ISO3CountryCode
{
s
[
0
],
s
[
1
],
s
[
2
]},
nil
}
func
(
i
ISO3CountryCode
)
String
()
string
{
return
fmt
.
Sprintf
(
"%c%c%c"
,
i
[
0
],
i
[
1
],
i
[
2
])
}
func
(
i
*
ISO3CountryCode
)
UnmarshalBinary
(
d
[]
byte
)
error
{
if
len
(
d
)
==
0
{
*
i
=
[
3
]
byte
{
'X'
,
'X'
,
'X'
}
return
nil
}
if
len
(
d
)
!=
3
{
return
errors
.
New
(
"invalid length"
)
}
*
i
=
[
3
]
byte
{
d
[
0
],
d
[
1
],
d
[
2
]}
return
nil
}
type
Vino
struct
{
ID
uuid
.
UUID
Name
string
Rating
int
Country
ISO3CountryCode
Picture
image
.
Image
HasPicture
bool
// Set to true if there's picture data for this vino
Comments
[]
Comment
...
...
@@ -74,6 +110,11 @@ func loadVino(tx *bolt.Tx, id uuid.UUID) (Vino, error) {
return
v
,
err
}
err
=
v
.
Country
.
UnmarshalBinary
(
data
.
Get
([]
byte
(
"country"
)))
if
err
!=
nil
{
return
v
,
err
}
comments
:=
data
.
Bucket
([]
byte
(
"comments"
))
if
comments
!=
nil
{
err
:=
comments
.
ForEach
(
func
(
k
,
txt
[]
byte
)
error
{
...
...
@@ -283,6 +324,13 @@ func (v *Vino) Store(ctx context.Context, db *bolt.DB) error {
}
}
if
v
.
Country
!=
UnknownCountry
{
err
=
wine
.
Put
([]
byte
(
"country"
),
v
.
Country
[
:
])
if
err
!=
nil
{
return
err
}
}
return
nil
})
}
...
...
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