package
0.3.5
Repository: https://github.com/raphaelpereira/go-res.git
Documentation: pkg.go.dev

# README

Search Query Example

Tags: Models, Collections, Linked resources, Queries, Pagination, Store, Call methods, Resource parameters

Description

A customer management system, where you can search and filter customers by name and country. The search results are live and updated as customers are edited, added, or deleted by multiple users simultaneously.

Prerequisite

  • Download and install Go
  • Install NATS Server and Resgate (done with 3 docker commands).

Install and run

git clone https://github.com/jirenius/go-res
cd go-res/examples/05-search-query
go run .

Open the client

http://localhost:8085

Things to try out

Live query

  • Open the client in two separate tabs.
  • Make a query (eg. Set Filter to B, and Country to Germany) in one tab.
  • In a separate tab, try to:
    • create a New customer matching the query.
    • edit a customer so that it starts to match the query.
    • edit a customer so that it no longer matches the query.
    • delete a customer that matches the query.
  • In the tab with the query, try to:
    • edit the name of a customer so that it affects its sort order.
    • edit the country of a customer so that it no longer matches the query.

Persistence

  • Open the client and make some changes.
  • Restart the service and the client.
  • Observe that all changes are persisted (using Badger DB).

API

RequestResourceDescription
getsearch.customers?from=0&limit=5&name=A&country=SwedenQuery collection of customer references. All query parameters are optional.
callsearch.customers.newCustomerAdds a new customer.
getsearch.customer.<ID>Models representing customers.
callsearch.customer.<ID>.setSets the customers' name, email, and country properties.
callsearch.customer.<ID>.deleteDeletes a customer.
getsearch.countriesCollection of available countries.

REST API

Resources can be retrieved using ordinary HTTP GET requests, and methods can be called using HTTP POST requests.

Get customer query collection (all parameters are optional)

GET http://localhost:8080/api/search/customers?from=0&limit=5&name=A&country=Sweden

Add new customer

POST http://localhost:8080/api/search/customers/newCustomer

Body

{ "name": "John Doe", "email": "[email protected]", "country": "France" }

Get customer

GET http://localhost:8080/api/search/customer/<ID>

Update customer properties

POST http://localhost:8080/api/search/customer/<ID>/set

Body

{ "name": "Jane Doe", "country": "United Kingdom" }

Delete customer

POST http://localhost:8080/api/search/customer/<ID>/delete

No body

Get country collection

GET http://localhost:8080/api/search/countries

# Functions

CountriesContains searches for a country and returns true if it is found in Countries.
NewCustomerStore creates a new CustomerStore.

# Variables

Countries is a sorted list of country names.
CountriesHandler is a handler that serves the static Countries collection.

# Structs

Customer represents a customer model.
CustomerHandler is a handler for customer requests.
CustomerQueryHandler is a handler for customer query collection requests.
CustomerStore holds all the customers and provides a way to make queries for customers matching certain filters.