Coder Social home page Coder Social logo

contacts-demo's Introduction

Contacts

A sample REST app that implements a CRUD API

About

This is a demo project for educational purposes. Implement an application built over Cowboy server through Plug adapters, and Ecto framework like wrappers around the data store. In this case the data store is a Postgres database.

We use Cowboy because is the defacto and most used HTTP server for Erlang/OTP. It is optimized for low latency and low memory usage, and because it uses Ranch for managing connections, Cowboy can easily be embedded in any other application.

Plug provides a specification for web application components and adapters for web servers, Cowboy in this case. If we have to change the web server, we can do it with with a minimum of effort just changing the adapter.

Ecto is a simple wrapper for data store, with a minimum configuration we can start to persist data into the data store.

API

GET /contacts        		List contacts of the database 
POST /contacts       		Create a contact   
GET /contacts/:email 		Fetch contact with email
PUT /contacts/:email   		Update a contact with the JSON on the payload
DELETE /contacts/:email    	Delete a contact with email

For filter purposes GET /contacts accept query params:

  • {field}={value}
  • _ord = asc | desc
  • _sort={field}
Method URI Status Code
GET /contacts 200
POST /contacts 200 400
GET /contacts/:email 200 404
PUT /contacts/:email 200 400 404
DELETE /contacts/:email 204 404

Samples

curl --header "Content-Type: application/json" --request GET http://localhost:4000/contacts?name=Jhon&_ord=desc&_sort=surname

curl --header "Content-Type: application/json" --request POST --data '{"name":"Jane","surname":"Doe", "phone_number":"+541231231", "email":"[email protected]" }' http://localhost:4000/contacts

curl --header "Content-Type: application/json" --request GET http://localhost:4000/contacts/[email protected]

curl --header "Content-Type: application/json" --request PUT --data '{"name":"Jane"}' http://localhost:4000/contacts/[email protected]

curl --header "Content-Type: application/json"  --request DELETE   http://localhost:4000/contacts/[email protected]

Sample Json

{
  "surname": "Doe",
  "phone_number": "+541231231",
  "name": "Jane",
  "email": "[email protected]"
}
    

Run on Docker

docker-compose up -d --build

This start the app on port 4000 with a postgres database on port 5566

Run Test

docker-compose up -d postgres #start postgres on docker
mix test

Docs

HexDoc avaliable on doc/index.html

contacts-demo's People

Watchers

Alejo Rios avatar

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.