A simple API to user management based on CRUD operations.
With this API you can create, read, update and delete users from a MySQL database.
The following packages dependencies is need to run the server:
We're using MySQL Database to keep it most simple as possible
More details about models can see in
src/models
folder
- id
- password
- name
- phone
- createdAt
- updatedAt
Before start server you need to set up your local database access, to do it you need to change the .env
file located at root of the project.
Run the following lines on your terminal
# Install packages dependences
yarn install
# Start server
yarn run server
After installing the dependencies and start your server, the API will be running at port 3000
To change API settings you can access .env
file at root project directory and make your needed changes, save that changes, and restart the server.
Returns json data about all users
-
URL
/users
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
None
-
Success Response:
- Code: 200 OK
Content:{ users : [{ "id": 6, "name": "Jhon", "phone": "+55 83 98833-55511", "email": "[email protected]", "password": " abcde123", "createdAt": "2021-03-22T14:05:43.000Z", "updatedAt": "2021-03-23T03:07:04.000Z" }, ...] }
- Code: 200 OK
-
Error Response:
- Code: 500 Internal Server Error
Content:{ error: "Could not fetch users" }
- Code: 500 Internal Server Error
-
Sample Call:
$.ajax({ url: "/users", type : "GET", success : function(r) { console.log(r); } });
Returns json data about a single user.
-
URL
/users/:id
-
Method:
GET
-
URL Params
Required:
id=[integer]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{ "user": { "id": 8, "name": "asfasfas", "phone": "fasfasfas", "email": "fasfas [email protected]", "password": " fasfasfasfasfasfas", "createdAt": "2021-03-23T03:49:38.000Z", "updatedAt": "2021-03-23T03:49:56.000Z" } }
- Code: 200
-
Error Response:
- Code: 400 INVALID PARAMS
Content:{ error: "invalid params" }
OR
- Code: 404 NOT FOUND
Content:{ error: "Could not find user" }
OR
- Code: 500 INTERNAL SERVER ERROR
Content:{ error: "Could not fetch user" }
- Code: 400 INVALID PARAMS
-
Sample Call:
$.ajax({ url: "/users/1", dataType: "json", type : "GET", success : function(r) { console.log(r); } });
Create a user and store in database.
-
URL
/users
-
Method:
POST
-
URL Params
Required:
None
-
Data Params
(JSON)
email, name, phone, password -
Success Response:
- Code: 200
Content:{ message:"User created" }
- Code: 200
-
Error Responses:
- Code: 400 INVALID PARAMS
Content:{ error: "invalid params" }
OR
- Code: 500 INTERNAL SERVER ERROR
Content:{ error: "Could not create user" }
OR
- Code: 406 NOT ACCEPTABLE
Content:{ error: "Email already exists" }
- Code: 400 INVALID PARAMS
-
Sample Call:
$.ajax({ url: "/users", dataType: "json", type : "POST", data: {email: email, password: password, name: name, phone: phone} success : function(r) { console.log(r); } });
Update user information identified by ID
-
URL
/users/:id
-
Method:
PUT
-
URL Params
Required:
id=[integer]
-
Data Params
(JSON)
email, name, phone, password -
Success Response:
- Code: 200
Content:{ message: "User details updated" }
- Code: 200
-
Error Response:
- Code: 400 INVALID PARAMS
Content:{ error: "invalid params" }
OR
- Code: 500 INTERNAL SERVER ERROR
Content:{ error: "Could not update user" }
OR
- Code: 404 NOT FOUND
Content:{ error: "Could not find user" }
OR
- Code: 406 NOT ACCEPTABLE
Content:{ error: "Email already exists" }
- Code: 400 INVALID PARAMS
-
Sample Call:
$.ajax({ url: "/users/1", dataType: "json", type : "PUT", success : function(r) { console.log(r); } });
delete user info from database identified by ID
-
URL
/users/:id
-
Method:
DELETE
-
URL Params
Required:
id=[integer]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{ message: "User deleted" }
- Code: 200
-
Error Response:
- Code: 400 INVALID PARAMS
Content:{ error: "invalid params" }
OR
- Code: 500 INTERNAL SERVER ERROR
Content:{ error: "Could not delete user" }
OR
- Code: 404 NOT FOUND
Content:{ error: "Could not find user" }
- Code: 400 INVALID PARAMS
-
Sample Call:
$.ajax({ url: "/users/1", dataType: "json", type : "DELETE", success : function(r) { console.log(r); } });