Skip to content

Commit

Permalink
Merge pull request #25 from voltgizerz/develop
Browse files Browse the repository at this point in the history
Develop to Master 23/06/2024
  • Loading branch information
voltgizerz committed Jun 23, 2024
2 parents e0e1795 + b503293 commit 5e06fa8
Show file tree
Hide file tree
Showing 42 changed files with 1,483 additions and 167 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
logs
./coverage.out
config.production.yml
app.exe
app.exe
tmp
main.exe
coverage.out
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ changelog-gen:

# Target to apply migrations
up:
goose -dir=./database/migrations mysql "root@tcp(localhost:3306)/db_pos?parseTime=true" up
@goose -dir=./database/migrations mysql "root@tcp(localhost:3306)/db_pos?parseTime=true" up

# Target to reset migrations (if needed)
down:
goose -dir=./database/migrations mysql "root@tcp(localhost:3306)/db_pos?parseTime=true" down
@goose -dir=./database/migrations mysql "root@tcp(localhost:3306)/db_pos?parseTime=true" down

status:
goose -dir=./database/migrations mysql "root@tcp(localhost:3306)/db_pos?parseTime=true" status
@goose -dir=./database/migrations mysql "root@tcp(localhost:3306)/db_pos?parseTime=true" status
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# REST API Go for POS
Simple REST APIs with for POS Software
This project provides simple REST APIs for POS (Point of Sale) software.
# Build With
- Go 1.20 or higher
- Fiber
- MySQL
- JWT Auth

# How to Run Project

Follow these steps to run the project:

1. Setup your database envrionment.
1. Setup Your Database Environment

2. Run the application using the following command: `go run ./cmd/app.go`
2. Run Database Migrations `goose -dir=./database/migrations mysql "root@tcp(localhost:3306)/db_pos?parseTime=true" up`

3. Run the Application `go run ./cmd/app.go`
46 changes: 46 additions & 0 deletions air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./main.exe"
cmd = "go build -o main.exe ./cmd"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
27 changes: 21 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"github.com/voltgizerz/POS-restaurant/config"
"github.com/voltgizerz/POS-restaurant/database"
"github.com/voltgizerz/POS-restaurant/internal/app/api"
"github.com/voltgizerz/POS-restaurant/internal/app/api/auth"
"github.com/voltgizerz/POS-restaurant/internal/app/api/handler"
"github.com/voltgizerz/POS-restaurant/internal/app/api/middleware"
"github.com/voltgizerz/POS-restaurant/internal/app/auth"
"github.com/voltgizerz/POS-restaurant/internal/app/interactor"
"github.com/voltgizerz/POS-restaurant/internal/app/repository"
"github.com/voltgizerz/POS-restaurant/internal/app/service"
Expand Down Expand Up @@ -39,31 +40,45 @@ func main() {
// Initialize Auth JWT
authJWT := auth.NewAuthJWT(cfg.API.JWTSecretKey)

// Initialize Middleware
jwtMiddleware := middleware.NewJWTAuthMiddleware(authJWT)

repoOpts := repository.RepositoryOpts{
Database: db,
}

// Initialize Repositories
userRepo := repository.NewUserRepository(repoOpts)

menuRepo := repository.NewMenuRepository(repoOpts)

// Initialize Services
userService := service.NewUserService(interactor.UserService{
AuthService: authJWT,
UserRepository: userRepo,
})

menuService := service.NewMenuService(interactor.MenuService{
MenuRepository: menuRepo,
})

// Initialize Handlers
userHandler := handler.NewUserHandler(interactor.UserHandler{
authHandler := handler.NewAuthHandler(interactor.UserHandler{
UserService: userService,
})

menuHandler := handler.NewMenuHandler(interactor.MenuHandler{
MenuService: menuService,
})

interactoAPI := interactor.APInteractor{
CfgAPI: cfg.API,
UserHandler: userHandler,
AuthHandler: authHandler,
MenuHandler: menuHandler,
}

// Start API server
go startAPIServer(interactoAPI)
go startAPIServer(interactoAPI, jwtMiddleware)

// Wait for termination signal
waitForSignal()
Expand All @@ -84,8 +99,8 @@ func handlePanic() {
}
}

func startAPIServer(interactor interactor.APInteractor) {
httpServer := api.NewServer(interactor)
func startAPIServer(interactor interactor.APInteractor, jwtMiddleware middleware.JWTAuth) {
httpServer := api.NewServer(interactor, jwtMiddleware)
httpServer.Initialize()
}

Expand Down
7 changes: 0 additions & 7 deletions cmd/user_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func InitDatabase(ctx context.Context, cfg config.Database) *DatabaseOpts {
span, ctx := opentracing.StartSpanFromContext(ctx, "database.InitDatabase")
defer span.Finish()

dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s", cfg.Username, cfg.Password, cfg.Host, cfg.Name)
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", cfg.Username, cfg.Password, cfg.Host, cfg.Name)

return &DatabaseOpts{
MasterDB: connectMySQL(ctx, dsn, cfg.MaxOpenConns, cfg.MaxIdleConns),
Expand Down
20 changes: 20 additions & 0 deletions database/migrations/00001_food_menus_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE `food_menus` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(300) DEFAULT NULL,
`thumbnail` text,
`price` float DEFAULT NULL,
`is_active` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_id` bigint(20) DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE `food_menus`
-- +goose StatementEnd
3 changes: 2 additions & 1 deletion database/migrations/20240622125106_add_users_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ CREATE TABLE `users` (
`email` varchar(255) DEFAULT NULL,
`password_hashed` varchar(528) DEFAULT NULL,
`is_active` tinyint(1) NOT NULL,
`role_id` tinyint(1) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- +goose StatementEnd

-- +goose Down
Expand Down
1 change: 1 addition & 0 deletions docs/api/insomnia-collection_POS Restaurant Go Api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_type":"export","__export_format":4,"__export_date":"2024-06-23T07:24:07.426Z","__export_source":"insomnia.desktop.app:v9.2.0","resources":[{"_id":"req_27faf4acf7d94c2f8ee446ce1274b3a5","parentId":"wrk_33553d20fe564d8ab097ea6088332cce","modified":1719125283185,"created":1719125200207,"url":"127.0.0.1:8080/api/v1/auth/login","name":"Login User","description":"","method":"POST","body":{"mimeType":"application/x-www-form-urlencoded","params":[{"id":"pair_ca5c1bbd010143de9ad7090b685485f6","name":"username","value":"testusername","description":""},{"id":"pair_11ebd58f85514c21b6b8ce67947815bf","name":"password","value":"passcode123","description":""}]},"preRequestScript":"","parameters":[],"headers":[{"name":"Content-Type","value":"application/x-www-form-urlencoded"},{"name":"User-Agent","value":"insomnia/9.2.0"}],"authentication":{},"metaSortKey":-1719125200207,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"wrk_33553d20fe564d8ab097ea6088332cce","parentId":null,"modified":1719039673832,"created":1719039673832,"name":"Go API ","description":"","scope":"collection","_type":"workspace"},{"_id":"req_1400b2c944bd4240b7f176437966392e","parentId":"wrk_33553d20fe564d8ab097ea6088332cce","modified":1719125210749,"created":1719124956560,"url":"127.0.0.1:8080/api/v1/auth/register","name":"Register New User","description":"","method":"POST","body":{"mimeType":"application/x-www-form-urlencoded","params":[{"id":"pair_05dc401783214e82a42ad53258104db8","name":"name","value":"name-test","description":""},{"id":"pair_24963cb8d30b425e812788697a19c817","name":"email","value":"test@gmail.com","description":""},{"id":"pair_faff0d81521f4f0d92e01492d4270e35","name":"password","value":"passcode123","description":""},{"id":"pair_4380821141cd42f09f43a31f7fe43e47","name":"confirmpassword","value":"passcode123","description":""},{"id":"pair_bba8b5099ccd4e1394d1384abfe26038","name":"username","value":"testusername","description":""}]},"preRequestScript":"","parameters":[],"headers":[{"name":"Content-Type","value":"application/x-www-form-urlencoded"},{"name":"User-Agent","value":"insomnia/9.2.0"}],"authentication":{},"metaSortKey":-1719124956560,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_e17fb461fdad4260a62874423c3407cb","parentId":"wrk_33553d20fe564d8ab097ea6088332cce","modified":1719127358730,"created":1719036049829,"url":"127.0.0.1:8080/api/v1/menu/1","name":"Delete By Menu Id","description":"","method":"DELETE","body":{"mimeType":"application/x-www-form-urlencoded","params":[]},"preRequestScript":"","parameters":[],"headers":[{"name":"Content-Type","value":"application/x-www-form-urlencoded"},{"name":"User-Agent","value":"insomnia/9.2.0"}],"authentication":{"type":"bearer","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVkX2F0IjoxNzE5MjExNjg1LCJpc19hY3RpdmUiOnRydWUsInJvbGVfaWQiOjEsInVzZXJfaWQiOjEsInVzZXJuYW1lIjoidGVzdHVzZXJuYW1lIn0.bNMip3ymFcTF-jxvVvPbqq7YGYZreIuX2uBVp3ZgV70"},"metaSortKey":-1719036049829,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_e2a1a598078d438aa430a7a174409ef6","parentId":"wrk_33553d20fe564d8ab097ea6088332cce","modified":1719127355300,"created":1719036016759,"url":"127.0.0.1:8080/api/v1/menu/1","name":"Update Menu By Menu Id","description":"","method":"PATCH","body":{"mimeType":"application/x-www-form-urlencoded","params":[{"id":"pair_3be9954a1d1443e7851c76422f49cfc4","name":"name","value":"Ayam Goreng 11","description":""},{"id":"pair_a20ab4671593420b99d57fe4f0e9e6d6","name":"thumbnail","value":"gegel.comcc","description":""},{"id":"pair_70a43e9006fc4823b40a44572325b037","name":"price","value":"1232222","description":""},{"id":"pair_06b82172c08341f3b40a8bbda281aafe","name":"userid","value":"1","description":""},{"id":"pair_c9a14508f96d4c828d12b4ee34906369","name":"isactive","value":"1","description":""}]},"preRequestScript":"","parameters":[],"headers":[{"name":"Content-Type","value":"application/x-www-form-urlencoded"},{"name":"User-Agent","value":"insomnia/9.2.0"}],"authentication":{"type":"bearer","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVkX2F0IjoxNzE5MjExNjg1LCJpc19hY3RpdmUiOnRydWUsInJvbGVfaWQiOjEsInVzZXJfaWQiOjEsInVzZXJuYW1lIjoidGVzdHVzZXJuYW1lIn0.bNMip3ymFcTF-jxvVvPbqq7YGYZreIuX2uBVp3ZgV70"},"metaSortKey":-1719036016759,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_0f26ea45076541b08ccad21f3dab899c","parentId":"wrk_33553d20fe564d8ab097ea6088332cce","modified":1719127330364,"created":1719035726253,"url":"127.0.0.1:8080/api/v1/menu/user/1","name":"Delete Batch By User Id","description":"","method":"DELETE","body":{"mimeType":"application/x-www-form-urlencoded","params":[{"id":"pair_5ad1404a7f0d4bd1be5ea45284009f2a","name":"userid","value":"1","description":""}]},"preRequestScript":"","parameters":[],"headers":[{"name":"Content-Type","value":"application/x-www-form-urlencoded"},{"name":"User-Agent","value":"insomnia/9.2.0"}],"authentication":{"type":"bearer","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVkX2F0IjoxNzE5MjExNjg1LCJpc19hY3RpdmUiOnRydWUsInJvbGVfaWQiOjEsInVzZXJfaWQiOjEsInVzZXJuYW1lIjoidGVzdHVzZXJuYW1lIn0.bNMip3ymFcTF-jxvVvPbqq7YGYZreIuX2uBVp3ZgV70"},"metaSortKey":-1719035726253,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_51334c4bd8a64cd1bf07fad2be756d25","parentId":"wrk_33553d20fe564d8ab097ea6088332cce","modified":1719127341228,"created":1718992253922,"url":"127.0.0.1:8080/api/v1/menu/1","name":"Get Menu","description":"","method":"GET","body":{"mimeType":"application/x-www-form-urlencoded","params":[]},"preRequestScript":"","parameters":[{"id":"pair_966a197268b44f4e93817109444ca73d","name":"","value":"","description":""}],"headers":[{"name":"Content-Type","value":"application/x-www-form-urlencoded"},{"name":"User-Agent","value":"insomnia/9.2.0"}],"authentication":{"type":"bearer","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVkX2F0IjoxNzE5MjExNjg1LCJpc19hY3RpdmUiOnRydWUsInJvbGVfaWQiOjEsInVzZXJfaWQiOjEsInVzZXJuYW1lIjoidGVzdHVzZXJuYW1lIn0.bNMip3ymFcTF-jxvVvPbqq7YGYZreIuX2uBVp3ZgV70"},"metaSortKey":-1718992253922,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_e8a655aef15149dba34a572adfeca736","parentId":"wrk_33553d20fe564d8ab097ea6088332cce","modified":1719127349313,"created":1718969498802,"url":"127.0.0.1:8080/api/v1/menu/","name":"Add Menu","description":"","method":"POST","body":{"mimeType":"application/x-www-form-urlencoded","params":[{"id":"pair_fef0594ecb8340a3b003269f65dcb791","name":"name","value":"Nasi Goreng","description":""},{"id":"pair_b5a602dda9524ec88e2009be7f4b4c75","name":"thumbnail","value":"google.com","description":""},{"id":"pair_ae082c6db13c41fab752cb668f88e188","name":"price","value":"10000","description":""},{"id":"pair_df1950bf66cd474f87c88611ab9ddbf6","name":"userid","value":"1","description":""},{"id":"pair_bb985bd330654094ba96e764a51e65de","name":"isactive","value":"0","description":""}]},"preRequestScript":"","parameters":[],"headers":[{"name":"Content-Type","value":"application/x-www-form-urlencoded"},{"name":"User-Agent","value":"insomnia/9.2.0"}],"authentication":{"type":"bearer","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVkX2F0IjoxNzE5MjExNjg1LCJpc19hY3RpdmUiOnRydWUsInJvbGVfaWQiOjEsInVzZXJfaWQiOjEsInVzZXJuYW1lIjoidGVzdHVzZXJuYW1lIn0.bNMip3ymFcTF-jxvVvPbqq7YGYZreIuX2uBVp3ZgV70"},"metaSortKey":-1718969498802,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"env_f08ebae3ac20411f9717505e52ebcded","parentId":"wrk_33553d20fe564d8ab097ea6088332cce","modified":1717862513529,"created":1717862513529,"name":"Base Environment","data":{},"dataPropertyOrder":null,"color":null,"isPrivate":false,"metaSortKey":1717862513529,"_type":"environment"},{"_id":"jar_ab65d293fddc42468581f9b2495630f7","parentId":"wrk_33553d20fe564d8ab097ea6088332cce","modified":1717862513532,"created":1717862513532,"name":"Default Jar","cookies":[],"_type":"cookie_jar"}]}
Loading

0 comments on commit 5e06fa8

Please sign in to comment.