The basket service is a REST API developed in Go.
Customers will be able to purchase existing products. Product creation, update, deletion or user creation etc for the admin use cases are not yet developed. You can import the necessary data to run the customer usecase scenarios to the database from the csv files under the data folder.
You can find the detailed documentation of the project under the docs.
. postgres
. golang-migrate
. mockery
. sqlc
Install packages
$ brew install golang-migrate postgres mockery sqlc
OR
Run scripts/bootstrap.sh
-
Install and run Postgresql
- if you use Mac, you can follow this steps : Mac Setup PostgreSQL
-
Create database that named basket_api
createdb basket_api
-
Run migrations from Makefile
make migrateup
- You can see the details of golang-migrate: DB migration in Go
-
All tables are ready! Now, go to data folder
- import
product.csv
to product table - import
customer.csv
to customer table - import
cart.csv
to cart table
- import
There are several ways to implement CRUD operations on database in golang.
You can read this blog comparing these options.
As a result of these comparisons, I decided to use the sqlc
package. sqlc will generate idiomatic Golang codes, which uses the standard database/sql library.
You can sqlc github page for the details.
To run application you need:
- .config.yml
To run basket-api locally you should create own .config.yml in the root directory.
For this, you can copy the .config_default.yml
and rename to .config.yml
.
Example .config.yml
:
postgres:
url: postgresql://localhost:5432/basket_api?sslmode=disable
http_server:
port: 8080
threshold_for_discount:
amount: 10000
You can set your postgresql url and http_server.port.
threshold_for_discount
, variable is the amount used to calculate whether the customer qualifies for the discounts.
All the following items must be completed to run the application:
- Installed required packages
- Created db (
make createdb
) - Ran migrations (
make migrateup
) - Imported csv file to db tables(product, customer, cart)
- Created own
.config.yml
If these items are OK, basket-api will be ready at the port given in the configuration with this command :
$ go run basket-api server
/cmd
:
Main applications for this project. By taking the application configs from the configuration file, a new server is started and created a new db Pool.
/data
: Contains csv files to be imported into the database.
/db
: Migration files and query files are located here. Sqlc looks in the migration folder here to create schemas and generates code by running the files under the query folder.
/docs
: Design and user documents
/internal
: Actual application code is located here
--------/api
: in here, a router is created using gin, a web framework.
--------/dpsql
: the codes generated by sqlc
are located here.
--------/model
: our domain data and request/response dtos.
--------/persistence
: database operations are performed here. There are DAO files.
--------/route
: http handlers are in here
--------/service
: contains some business logic for each model
--------/util
: contains helpers and discount functions
/mocks
: mock files created by Mockery are located here.
/scripts
: to perform various build, and install
Unit tests use Mockery to create mocks. Mockery provides the ability to easily generate mocks for golang interfaces Mockery generates mocks and stores them in the mocks directory which is committed to source control.
When adding a method to an interface that is mocked in tests you will need to run Mockery so that the mock file is updated.
You can run all test with this command :
$ go test ./...