-
Notifications
You must be signed in to change notification settings - Fork 5
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
[WIP] 7 - Implementar HTTP DELETE/plants/{id} #21
base: master
Are you sure you want to change the base?
Changes from 6 commits
5e7b556
645ed68
97c783d
593472b
d07e58d
99e3a7f
0ce1f8d
082855b
328cae1
773ce03
aa9ab08
84abe21
7027956
eb862da
30d909e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package elastic | ||
|
||
import ( | ||
es "github.com/elastic/go-elasticsearch/v8" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var esInstance *es.Client | ||
|
||
// GetClient returns a client connected to the running elasticseach cluster | ||
func GetClient() (*es.Client, error) { | ||
if esInstance != nil { | ||
return esInstance, nil | ||
} | ||
|
||
cfg := es.Config{ | ||
Addresses: []string{ | ||
"http://elasticsearch:9200", | ||
"http://elasticsearch2:9200", | ||
"http://elasticsearch3:9200", | ||
}, | ||
} | ||
esClient, err := es.NewClient(cfg) | ||
if err != nil { | ||
log.WithFields(log.Fields{ | ||
"config": cfg, | ||
"error": err, | ||
}).Error("Could not obtain an Elasticsearch client") | ||
|
||
return nil, err | ||
} | ||
|
||
esInstance = esClient | ||
|
||
return esInstance, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package elastic | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
|
||
"github.com/gdgtoledo/linneo/elastic" | ||
"github.com/gdgtoledo/linneo/plants" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func (id string) getQuery() map[string]interface{} { | ||
query := map[string]interface{}{"query": { | ||
match: { | ||
id: id, | ||
}, | ||
}} | ||
|
||
return query | ||
} | ||
|
||
// Delete a plant by id | ||
func Delete(id string) (plants.DeleteByIDResult, error) { | ||
result := plants.DeleteByIDResult{} | ||
|
||
esClient, err := elastic.GetClient() | ||
|
||
if err != nil { | ||
return result, err | ||
} | ||
|
||
var buf bytes.Buffer | ||
|
||
if err := json.NewEncoder(&buf).Encode(query); err != nil { | ||
log.WithFields(log.Fields{ | ||
"error": err, | ||
}).Error("Error encoding Elasticsearch query") | ||
|
||
return result, err | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package plants | ||
|
||
import dao "github.com/gdgtoledo/linneo/elastic/dao" | ||
|
||
// Delete by id | ||
func Delete(id string) (DeleteQueryByIDResult, error) { | ||
result, err := dao.Delete(id) | ||
return result, err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package plants | ||
|
||
// Model for an item | ||
type Model struct { | ||
ID string | ||
Name string | ||
Species string | ||
Genere string | ||
Geo string | ||
} | ||
|
||
// SearchQueryByIndexName struct | ||
type SearchQueryByIndexName struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. El index es algo que se pone en el cliente, yo lo llamaría There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eliminarias ese IndexName string del struct entonces? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sí, así es reutilizable |
||
IndexName string | ||
Query map[string]interface{} | ||
} | ||
|
||
// SearchQueryByIndexNameResult wraps a search result | ||
type SearchQueryByIndexNameResult map[string]interface{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. El index es algo que se pone en el cliente, yo lo llamaría |
||
|
||
// DeleteByIDResult wraps a delete item result | ||
type DeleteQueryByID interface { | ||
getQuery() map[string]interface{} | ||
} | ||
|
||
// DeleteByIDResult wraps a delete item result | ||
type DeleteQueryByIDResult map[string]interface{} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package plants | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lo mismo que para el Delete, lo dejaría en el main.go, o al menos en un fichero dentro del paquete raíz |
||
|
||
import ( | ||
dao "github.com/gdgtoledo/linneo/elastic/dao" | ||
) | ||
|
||
// Search by query with an indexName | ||
func Search(searchQueryByIndexName SearchQueryByIndexName) (SearchQueryByIndexNameResult, error) { | ||
response, err := dao.Search(searchQueryByIndexName) | ||
return response, err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yo dejaría ésto como un método en el main.go, para no tener un paquete con sólo un método público, o al menos en un fichero dentro del paquete raíz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bueno, yo también he tenido esa duda, al final, desde mi opinión inexperta, lo hice así para tener un dominio que no dependa de ninguna implementación, en este caso, nuestro dominio son las planticas y su gestión, en el main tenemos el problema que tenemos un buen salpicado de marisco de implementación del servidor, con gin, elastic logs tracking and monitoring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
la idea era tener un paquete plants de nuestra entidad de dominio y aquí empiezan mis dudas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
según veo y a efectos practicos, un cliente lo unico que debería pedir es un search('plants'),
ese
plants
se lo daría el contexto de GinThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crearía un
api.go
en el raíz, y allí todos estos metodicos