Code Monkey home page Code Monkey logo

persons-api's Introduction

Contribute

$ git clone https://github.com/wcrichter/persons-api.git
$ cd persons-api
$ npm install
$ npm start

Supported HTTP Requests

  • GET
  • POST
  • DELETE
  • PUT

Endpoints

GET /persons/:id

Returns a JSON object representing a person for a given person id.

Parameters

  • id - The primary key of the person to retrieve.

Example Call

GET /persons/[email protected]

Example Response (Success):

{
  "_id": "[email protected]",
  "_rev": "3-3a94a40198e67b8d67909e6aa156ec70",
  "firstName": "David",
  "lastName": "Puddy",
  "email": "[email protected]",
  "type": "person"
}

Example Response (Error):

{
  "error": "not_found",
  "reason": "missing",
  "name": "not_found",
  "status": 404,
  "message": "missing"
}

POST /persons

Adds a JSON object representing a person with specified keys and values.

Parameters

  • doc - The document of the person to add to the data.

Example Call

POST /persons

Body:

{
  "_id": "[email protected]",
  "firstName": "Frank",
  "lastName": "Costanza",
  "email": "[email protected]",
  "type": "person"
}

Example Response (Success):

{
  "ok": true,
  "id": "[email protected]",
  "rev": "1-b052be6014e8625f93649d10c5d090e4"
}

Example Response (Error):

{
  "error": "conflict",
  "reason": "Document update conflict.",
  "name": "conflict",
  "status": 409,
  "message": "Document update conflict."
}

DELETE /persons/:id

Delete a JSON object representing a person with a given id.

Parameters

  • id - The id of the person to delete from the database.

Example Call

DELETE /persons/[email protected]

Body:

{
  "_id": "[email protected]",
  "firstName": "Frank",
  "lastName": "Costanza",
  "email": "[email protected]",
  "type": "person"
}

Example Response (Success):

{
  "ok": true,
  "id": "[email protected]",
  "rev": "2-34783470fb39a24b6581849403caa7b5"
}

Example Response (Error):

{
  "name": "not_found",
  "status": 404,
  "message": "deleted",
  "reason": "deleted",
  "error": "not_found"
}

POST /persons

Adds a JSON object representing a person with specified keys and values.

Parameters

  • doc - The document of the person to add to the data.

Example Call

POST /persons

Body:

{
  "_id": "[email protected]",
  "firstName": "Frank",
  "lastName": "Costanza",
  "email": "[email protected]",
  "type": "person"
}

Example Response (Success):

{
  "ok": true,
  "id": "[email protected]",
  "rev": "1-b052be6014e8625f93649d10c5d090e4"
}

Example Response (Error):

{
  "error": "conflict",
  "reason": "Document update conflict.",
  "name": "conflict",
  "status": 409,
  "message": "Document update conflict."
}

POST /persons/:id

Update a JSON object (representing a person) with a new JSON object with a given id.

Parameters

  • id - The id of the person to update from the database.

Example Call

DELETE /persons/[email protected]

Body:

{
	"_id": "[email protected]",
	"_rev": "3-ab7eef2f9976fd1a6c37743f6ac3fa59",
	"firstName": "Elaine",
	"lastName": "Bennis",
	"email": "[email protected]",
	"type": "person"
}

This new object body updates the email value from [email protected] to [email protected]

Example Response (Success):

{
  "ok": true,
  "id": "[email protected]",
  "rev": "4-918291f0b62f9941a55c1178ddb44d09"
}

Example Response (Error):

{
  "name": "conflict",
  "status": 409,
  "message": "Document update conflict.",
  "reason": "Document update conflict.",
  "error": "conflict"
}

GET /persons

Returns a JSON object containing all 'person' documents from the database.

Parameters

  • id - The id of the person to update from the database.

Example Call

GET /persons

Example Response (Success):

[
  {
    "_id": "[email protected]",
    "_rev": "1-3b4b0fa77d27c252f20fb36dc9ecae5c",
    "firstName": "Bob",
    "lastName": "Saccamano",
    "email": "[email protected]",
    "type": "person"
  },
  {
    "_id": "[email protected]",
    "_rev": "5-56a069d8a3d0a6f36ebe09641a377b0a",
    "firstName": "Chris",
    "lastName": "Richter",
    "email": "[email protected]",
    "type": "person"
  },
  {
    "_id": "[email protected]",
    "_rev": "3-3a94a40198e67b8d67909e6aa156ec70",
    "firstName": "David",
    "lastName": "Puddy",
    "email": "[email protected]",
    "type": "person"
  },
  {
    "_id": "[email protected]",
    "_rev": "4-918291f0b62f9941a55c1178ddb44d09",
    "firstName": "Elaine",
    "lastName": "Bennis",
    "email": "[email protected]",
    "type": "person"
  },

]

Example Response (Error):

Cannot GET /people

persons-api's People

Contributors

wcrichter avatar

Watchers

 avatar  avatar

persons-api's Issues

Maybe don't mutate in prepNewPerson?

The prepNewPerson function,

function prepNewPerson(doc) {
    doc._id = 'person' + '_' + doc.firstName.toLowerCase() + '_' + doc.lastName.toLowerCase() + '_' + doc.email.toLowerCase()
    doc.type = 'person'
    return doc
}

probably doesn't need to mutate the doc that is passed in. It might be safer to create a new object with the changes that is then returned so that the original object doesn't get touched.

One way to do this would be with the ES2015/ES6 Object.assign:

function prepNewPerson(doc) {
  return Object.assign({}, doc, {
    _id: 'person' + '_' + doc.firstName.toLowerCase() + '_' + doc.lastName.toLowerCase() + '_' + doc.email.toLowerCase(),
    type: 'person'
  })
}

or with ramda's merge (which returns a new object):

function prepNewPerson(doc) {
  return merge(doc, {
    _id: 'person' + '_' + doc.firstName.toLowerCase() + '_' + doc.lastName.toLowerCase() + '_' + doc.email.toLowerCase(),
    type: 'person'
  })
}

and while on the subject, I bet using ramda you could really bring this function down in simplicity and make it safer. For example, what happens when lastName isn't provided via doc? toLowerCase() will throw an error since you can't call a function on something that isn't defined.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.