Skip to content

Commit

Permalink
Merge pull request #20 from 44r0n/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Aarón committed Aug 20, 2017
2 parents ad871ed + 0c12a67 commit cc5f1db
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 50 deletions.
126 changes: 78 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,38 +97,43 @@ Registers a json given user.
* **Success Response:**

* **Code:** 201 CREATED<br />
**Content:** No content
**Content:**
~~~json
{
"Response": {
"Status": 201,
"Error": 1,
"Description": ""
}
}
~~~

* **Error Response:**

* **Code:** 409 CONFLICT <br />
**Content:**
~~~json
{
"error": {
"status": 409,
"error": "FIELDS_REPEATED",
"description": "One or more fields already exist",
"fields": {
"email": "An account already exists with this email",
"username": "An account already exists with this username"
}
"Response": {
"Status": 409,
"Error": -2,
"Description": "Repeated UserName"
}
}
~~~

* **Sample Call:**
* **Sample Call:**

~~~javascript
$.ajax({
url: "/Register",
dataType: "json",
type : "POST",
201 : function(r) {
console.log(r);
}
});
~~~
~~~javascript
$.ajax({
url: "/Register",
dataType: "json",
type : "POST",
201 : function(r) {
console.log(r);
}
});
~~~
----
##### Login User

Expand Down Expand Up @@ -161,10 +166,10 @@ Registers a json given user.
**Content:**
~~~json
{
"response": {
"status": "OK",
"token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImQxZmY1MDE4LTgyYjgtMTFlNy1hMGQ1LTAyNDJhYzExMDAwMiJ9.vwm1JhqfPhI6Vj-A4BubhaGyrSpGSKYQ246JSPQqd6lZBrkpuDrpYem79baUJPQDXoduw14j6x26KkK8wprzTg",
"error": ""
"Response": {
"Status": 200,
"Token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjJlZjM5NjQ0LTg1YjYtMTFlNy04Y2Q1LTAyNDJhYzExMDAwMiJ9.87Ok320Qdj6JQ_xS5cBngc1VcL2yuPvUnCSfYgv5qrEI1FBbcCHnoUdUtBGn3pCk0qYD_cWljaNnprHLwOPkKQ",
"Error": 1
}
}
~~~
Expand All @@ -175,10 +180,10 @@ Registers a json given user.
**Content:**
~~~json
{
"response": {
"status": "Incorrect user or password",
"token": "",
"error": ""
"Response": {
"Status": 404,
"Error": -8,
"Description": "User not found"
}
}
~~~
Expand Down Expand Up @@ -222,15 +227,31 @@ Validates a given token.

* **Success Response:**

* **Code:** 204 NO CONTENT<br />
* **Code:** 200 NO CONTENT<br />
**Content:**
None
~~~json
{
"Response": {
"Status": 200,
"Error": 1,
"Description": ""
}
}
~~~

* **Error Response:**

* **Code:** 404 NOT FOUND <br />
**Content:**
None
~~~json
{
"Response": {
"Status": 404,
"Error": -7,
"Description": "The token is invalid"
}
}
~~~

* **Sample Call:**
~~~javascript
Expand Down Expand Up @@ -271,36 +292,45 @@ Validates a given token.

* **Success Response:**

* **Code:** 204 NO CONTENT<br />
* **Code:** 200 NO CONTENT<br />
**Content:**
None

* **Error Response:**

* **Code:** 404 NOT FOUND <br />
**Content:**
~~~json
{
"response": {
"status": "Incorrect user or password",
"token": "",
"error": ""
}
}
~~~
~~~json
{
"Response": {
"Status": 200,
"Error": 1,
"Description": ""
}
}
~~~

* **Sample Call:**
~~~javascript
$.ajax({
url: "/Register",
dataType: "json",
type : "POST",
201 : function(r) {
200 : function(r) {
console.log(r);
}
});
~~~

#### Error codes
As seen, in all responses have an error code. Those are for a quick check of the response apart from the http response code. Here are the codes:

| Code | Description |
| :------------- | :------------- |
| 1 | Ok |
| -1 | Unknown error |
| -2 | Repeated User Name |
| -3 | Repeated User Email |
| -4 | Database error |
| -5 | Json Error |
| -6 | No token provided |
| -7 | Invalid token |
| -8 | User Not Found |

## Built With

* [MySQL](https://www.mysql.com/) - The database engine.
Expand Down
2 changes: 1 addition & 1 deletion controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (uc *UserController) Login(w http.ResponseWriter, r *http.Request, p httpro
w.Header().Set("Content-Type", "application/json")
if token != "" {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"Response":{"Status":`+strconv.Itoa(http.StatusOK)+`,"Error":"", "Token":"`+token+`"}}`)
fmt.Fprint(w, `{"Response":{"Status":`+strconv.Itoa(http.StatusOK)+`,"Token":"`+token+`","Error":`+strconv.Itoa(codes.Ok)+`}}`)
return
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func TestLoginOK(t *testing.T) {
status := rr.Code
So(status, ShouldEqual, http.StatusOK)
if !*database {
expected := `{"Response":{"Status":` + strconv.Itoa(http.StatusOK) + `,"Error":"", "Token":"1234abcd"}}`
expected := `{"Response":{"Status":` + strconv.Itoa(http.StatusOK) + `,"Token":"1234abcd","Error":` + strconv.Itoa(codes.Ok) + `}}`
So(rr.Body.String(), ShouldEqual, expected)
}
})
Expand Down

0 comments on commit cc5f1db

Please sign in to comment.