Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Return an error if trying to add an existing intent
Browse files Browse the repository at this point in the history
  • Loading branch information
hugolgst committed Apr 29, 2020
1 parent 8ab7a1d commit c973f0c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
22 changes: 19 additions & 3 deletions dashboard/intents.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,19 @@ func GetIntents(w http.ResponseWriter, r *http.Request) {

// CreateIntent is the route to create a new intent
func CreateIntent(w http.ResponseWriter, r *http.Request) {
allowedHeaders := "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization,Olivia-Token"
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", allowedHeaders)
w.Header().Set("Access-Control-Expose-Headers", "Authorization")

data := mux.Vars(r)

// Checks if the token present in the headers is the right one
token := r.Header.Get("Olivia-Token")
if !ChecksToken(token) {
json.NewEncoder(w).Encode(Error{
Message: "You don't have the permission to do this.",
Message: util.GetMessage(data["locale"], "no permission"),
})
return
}
Expand All @@ -96,12 +104,20 @@ func CreateIntent(w http.ResponseWriter, r *http.Request) {

if intent.Responses == nil || intent.Patterns == nil {
json.NewEncoder(w).Encode(Error{
Message: "Patterns and responses can't be null",
Message: util.GetMessage(data["locale"], "patterns same"),
})
return
}

data := mux.Vars(r)
// Returns an error if the tags are the same
for _, _intent := range analysis.GetIntents(data["locale"]) {
if _intent.Tag == intent.Tag {
json.NewEncoder(w).Encode(Error{
Message: util.GetMessage(data["locale"], "tags same"),
})
return
}
}

// Adds the intent
AddIntent(data["locale"], intent)
Expand Down
12 changes: 12 additions & 0 deletions res/locales/ca/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
"No teniu permís per fer-ho."
]
},
{
"tag": "patterns same",
"messages": [
"Els patrons i/o les respostes no poden estar buits."
]
},
{
"tag": "tags same",
"messages": [
"Ja existeix un intent amb aquesta etiqueta."
]
},
{
"tag": "spotify tokens",
"messages": [
Expand Down
12 changes: 12 additions & 0 deletions res/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
"You don't have the permission to do this."
]
},
{
"tag": "patterns same",
"messages": [
"The patterns and/or responses can't be empty."
]
},
{
"tag": "tags same",
"messages": [
"An intent already exists with this tag."
]
},
{
"tag": "spotify tokens",
"messages": [
Expand Down
12 changes: 12 additions & 0 deletions res/locales/es/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
"No tienes permiso para hacer esto.."
]
},
{
"tag": "patterns same",
"messages": [
"Los patrones y/o respuestas no pueden estar vacíos."
]
},
{
"tag": "tags same",
"messages": [
"Ya existe un intent con esta etiqueta."
]
},
{
"tag": "spotify tokens",
"messages": [
Expand Down
12 changes: 12 additions & 0 deletions res/locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
"Vous n'avez pas la permission pour faire ceci."
]
},
{
"tag": "patterns same",
"messages": [
"Les modèles et/ou les réponses ne peuvent pas être vides."
]
},
{
"tag": "tags same",
"messages": [
"Un intent existe déjà avec cette étiquette"
]
},
{
"tag": "spotify tokens",
"messages": [
Expand Down

0 comments on commit c973f0c

Please sign in to comment.