janevalencia / nextjs-ssr-comments

Interactive Comment Sections developed using NextJS/TailwindCSS/Express-MongoDB by Jane Valencia
https://fem-comments-hucu-janevalencia.vercel.app/
2 stars 0 forks source link

Setup backend api routes for User model #48

Closed janevalencia closed 2 years ago

janevalencia commented 2 years ago

Pull-requests are developed for the following dev-tickets

Description

This pull-request setup the backend API routes to interact with User model.

Route:

Request available for api/users

Request available for api/users/[username]

Testing

POST: api/users

  1. Test passed ✅ Successfully create a new User.
    {
    "message": "Successfully created a new user.",
    "result": {
        "username": "amyrobson",
        "firstname": "Amy",
        "lastname": "Robson",
        "imageURL": "/assets/images/image-amyrobson.png",
        "_id": "63121c48f8da84e272c0133a",
        "createdAt": "2022-09-02T15:07:52.145Z",
        "updatedAt": "2022-09-02T15:07:52.145Z",
        "__v": 0
    }
    }
  2. Test passed ✅ Unable to create a new User with the same username.
    {
    "error": {
        "index": 0,
        "code": 11000,
        "keyPattern": {
            "username": 1
        },
        "keyValue": {
            "username": "juliusomo"
        }
    }
    }
  3. Test passed ✅ property lastname is optional, so missing this property should still create a new User.
    {
    "message": "Successfully created a new user.",
    "result": {
        "username": "maxblagun",
        "firstname": "Max B.",
        "imageURL": "/assets/images/image-maxblagun.png",
        "_id": "63121ceaf8da84e272c0133c",
        "createdAt": "2022-09-02T15:10:34.016Z",
        "updatedAt": "2022-09-02T15:10:34.016Z",
        "__v": 0
    }
    }
  4. Test passed ✅ Missing required properties such as firstname should not create a new User and throw validation error.
    {
    "error": {
        "errors": {
            "firstname": {
                "name": "ValidatorError",
                "message": "Path `firstname` is required.",
                "properties": {
                    "message": "Path `firstname` is required.",
                    "type": "required",
                    "path": "firstname"
                },
                "kind": "required",
                "path": "firstname"
            }
        },
        "_message": "User validation failed",
        "name": "ValidationError",
        "message": "User validation failed: firstname: Path `firstname` is required."
    }
    }

GET: api/users

Test passed ✅ Fetching all users

[
    {
        "_id": "6312198af8da84e272c01336",
        "username": "juliusomo",
        "firstname": "Julius",
        "lastname": "Omo",
        "imageURL": "/assets/images/image-juliusomo.png",
        "createdAt": "2022-09-02T14:56:10.454Z",
        "updatedAt": "2022-09-02T14:56:10.454Z",
        "__v": 0
    },
    {
        "_id": "63121c48f8da84e272c0133a",
        "username": "amyrobson",
        "firstname": "Amy",
        "lastname": "Robson",
        "imageURL": "/assets/images/image-amyrobson.png",
        "createdAt": "2022-09-02T15:07:52.145Z",
        "updatedAt": "2022-09-02T15:07:52.145Z",
        "__v": 0
    },
    {
        "_id": "63121ceaf8da84e272c0133c",
        "username": "maxblagun",
        "firstname": "Max B.",
        "imageURL": "/assets/images/image-maxblagun.png",
        "createdAt": "2022-09-02T15:10:34.016Z",
        "updatedAt": "2022-09-02T15:10:34.016Z",
        "__v": 0
    },
    {
        "_id": "63121fa0f8da84e272c0133f",
        "username": "ramsesmiron",
        "firstname": "Ramses",
        "lastname": "Miron",
        "imageURL": "/assets/images/image-ramsesmiron.png",
        "createdAt": "2022-09-02T15:22:08.730Z",
        "updatedAt": "2022-09-02T15:22:08.730Z",
        "__v": 0
    }
]

GET: api/users/[username]

Test passed ✅ Fetch a single user information provided the username on request.query param Run on postman: GET http://localhost:3000/api/users/juliusomo

{
    "_id": "6312198af8da84e272c01336",
    "username": "juliusomo",
    "firstname": "Julius",
    "lastname": "Omo",
    "imageURL": "/assets/images/image-juliusomo.png",
    "createdAt": "2022-09-02T14:56:10.454Z",
    "updatedAt": "2022-09-02T14:56:10.454Z",
    "__v": 0
}

Route api/users cannot accept PUT, DELETE request methods

Test passed ✅ Calling PUT: http://localhost:3000/api/users will throw response error

{
    "error": "No response found, verify request method."
}