gontracts

package module
v0.0.0-...-b84815f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2019 License: MIT Imports: 16 Imported by: 0

README

Simple contract microservice

The microservice able to handle companies, contracts, and purchases

Go Report Card GoDoc Build Status Coverage Status

Usage

Download service via

go get -t github.com/ilyakaznacheev/gontracts/...

and then open directory GOPATH/src/github.com/ilyakaznacheev. Or you can manually create the same dir and fetch the repo with Git.

To run service you have to run MySQL server via Docker

docker-compose up

and then start the server

go run cmd/gontracts/gontracts.go

The server will be started on localhost:8000

Requirements

If you download the package manually following requirements must be met:

  • github.com/go-sql-driver/mysql
  • github.com/gorilla/mux

API

For full API description see swagger.yml

Service has following web API:

  • /company/<id:int> GET: get company data by id
  • /company POST: create new company
  • /company PUT: create or update company
  • /company/<id:int> DELETE: delete company by id
  • /company GET: get list of all companies
  • /contract/<id:int> GET: get contract data by id
  • /contract POST: create new contract
  • /contract PUT: create or update contract
  • /contract/<id:int> DELETE: delete contract by id
  • /contract GET: get list of all contracts
  • /contract/<id:int>/purchase GET: get purchase history of contract
  • /purchase POST: create new purchase document
  • /get-token GET: generates new Bearer auth token
Authorization

API uses JSON Web Encryption (JWE) for authorizations.

Use GET request to /get-token to get a new token. Other paths require authorization.

Examples

Get company data

Request

GET:localhost:8000/company/1

Response

{
    "ID": 1,
    "name": "Megacom",
    "regcode": "MGC111"
}
Create a company

Request

POST:localhost:8000/company

{
	"name":"Supercom",
	"regcode":"SRC222"
}

Response

{
	"ID": 2
}
Update company info

Request

PUT:localhost:8000/company

{
	"name":"Supercom",
	"regcode":"SRC333"
}

Response

{
	"name":"Supercom",
	"regcode":"SRC333"
}
Delete company

Request

DELETE:localhost:8000/company/3

Response

OK

Add new contract

Request

POST:localhost:8000/contract

{
	"sellerID":1,
	"clientID":2,
	"validFrom":"2000-01-01T00:00:00Z",
	"validTo":"2001-01-01T00:00:00Z",
	"amount":150
}

Response

{
	"ID": 5
}
Add new purchase document

Request

POST:localhost:8000/purchase

{
	"contractID":1,
	"datetime": "2000-10-01T00:00:00Z",
	"amount": 3
}

Response

{
	"ID": 4
}
Get purchase history of contract

Request

GET:localhost:8000/contract/1/purchase

Response

[
	{
		"ID": 1,
		"contractID": 1,
		"datetime": "2000-10-01T00:00:00Z",
		"amount": 3
	},
	{
		"ID": 2,
		"contractID": 1,
		"datetime": "2000-10-01T00:00:00Z",
		"amount": 3
	},
	{
		"ID": 3,
		"contractID": 1,
		"datetime": "2000-10-01T00:00:00Z",
		"amount": 3
	},
	{
		"ID": 4,
		"contractID": 1,
		"datetime": "2000-10-01T00:00:00Z",
		"amount": 3
	}
]

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrContractNotFound contract doesn't exist in DB
	ErrContractNotFound = errors.New("contract doesn't exist")
	// ErrSellerNotExist seller company doesn't exist in DB
	ErrSellerNotExist = errors.New("seller company doesn't exist")
	// ErrClientNotExist client company doesn't exist in DB
	ErrClientNotExist = errors.New("client company doesn't exist")
	// ErrDateNotValid purchase date is outside the contract date range
	ErrDateNotValid = errors.New("purchase date is outside the contract date range")
	// ErrNotEnoughMoney not enough money for the purchase
	ErrNotEnoughMoney = errors.New("not enough money for the purchase")
)

Functions

This section is empty.

Types

type AuthHandler

type AuthHandler struct {
	jwtmiddleware.JWTMiddleware
	// contains filtered or unexported fields
}

AuthHandler authentication handler

func NewAuthHandler

func NewAuthHandler(key []byte) *AuthHandler

NewAuthHandler creates new authentication handler

func (*AuthHandler) GenerateToken

func (a *AuthHandler) GenerateToken(w http.ResponseWriter, r *http.Request)

GenerateToken returns new authentication token

func (*AuthHandler) HandlerFunc

func (a *AuthHandler) HandlerFunc(f func(w http.ResponseWriter, r *http.Request)) http.Handler

HandlerFunc is a function handler adepter that wraps handler function into auth handler object

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is a request handler

func NewHandler

func NewHandler(company model.CompanyModel, contract model.ContractModel, purchase model.PurchaseModel) *Handler

NewHandler returns new request handler

func (*Handler) CreateCompany

func (h *Handler) CreateCompany(w http.ResponseWriter, r *http.Request)

CreateCompany creates new company

func (*Handler) CreateContract

func (h *Handler) CreateContract(w http.ResponseWriter, r *http.Request)

CreateContract creates new contract

func (*Handler) DeleteCompany

func (h *Handler) DeleteCompany(w http.ResponseWriter, r *http.Request)

DeleteCompany removes company

func (*Handler) DeleteContract

func (h *Handler) DeleteContract(w http.ResponseWriter, r *http.Request)

DeleteContract removes contract

func (*Handler) GetCompany

func (h *Handler) GetCompany(w http.ResponseWriter, r *http.Request)

GetCompany returns company info

func (*Handler) GetCompanyList

func (h *Handler) GetCompanyList(w http.ResponseWriter, r *http.Request)

GetCompanyList returns list of companies

func (*Handler) GetContract

func (h *Handler) GetContract(w http.ResponseWriter, r *http.Request)

GetContract returns contract data

func (*Handler) GetContractList

func (h *Handler) GetContractList(w http.ResponseWriter, r *http.Request)

GetContractList returns contract list

func (*Handler) GetPurchaseHistory

func (h *Handler) GetPurchaseHistory(w http.ResponseWriter, r *http.Request)

GetPurchaseHistory returns purcase history of contract

func (*Handler) Purchase

func (h *Handler) Purchase(w http.ResponseWriter, r *http.Request)

Purchase creates new purchase document

func (*Handler) UpdateCompany

func (h *Handler) UpdateCompany(w http.ResponseWriter, r *http.Request)

UpdateCompany updates company data

func (*Handler) UpdateContract

func (h *Handler) UpdateContract(w http.ResponseWriter, r *http.Request)

UpdateContract updates contract

type ResponseID

type ResponseID struct {
	ID int
}

ResponseID represents id in response

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is an main application server

func (*Server) Start

func (s *Server) Start() error

Start runs the server

func (*Server) Stop

func (s *Server) Stop()

Stop shuts the server down

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL