Skip to content

Commit

Permalink
Merge pull request #20 from BlazeIsClone/develop
Browse files Browse the repository at this point in the history
add missions index route & improve local dev
  • Loading branch information
BlazeIsClone authored Oct 24, 2024
2 parents 2660bf2 + d11888a commit 7bc8013
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 14 deletions.
41 changes: 41 additions & 0 deletions .air.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
tmp_dir = "tmp"

[build]
# Just plain old shell command. You could use `make` as well.
cmd = "make build"
# Binary file yields from `cmd`.
bin = "build"
# Customize binary.
full_bin = "./build"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Watch these directories if you specified.
include_dir = []
# Exclude files.
exclude_file = []
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop to run old binary when build errors occur.
stop_on_error = true
# This log file places in your tmp_dir.
log = "air_errors.log"

[log]
# Show log time
time = false

[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
# Delete tmp directory on exit
clean_on_exit = true
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@

[![Go](https://img.shields.io/badge/go-00ADD8.svg?style=for-the-badge&logo=go&logoColor=white)](https://go.dev/)

Order management microservice.
Mission Control microservice.

## Development

To get started, clone this repository and follow theese steps.
To get started, clone this repository and follow these steps to run the Go application in your local environment.

Run Docker Containers:
Start all of the Docker containers in the background, you may start in "detached" mode:

```bash
docker-compose up -d
```

Running Database Migrations (use arg action):
The application is executing within a Docker container and is isolated from your local computer. To run various commands against your application use:

```bash
docker-compose exec app {CMD}
```

## Database Migrations

Run all of your outstanding migrations:

```bash
make migrate action=up
```

Roll back the latest migration operation, you may use the rollback Artisan command. This command rolls back the last "batch" of migrations, which may include multiple migration files:

```bash
make migrate action=down
```
12 changes: 10 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
version: '3'

services:
app:
image: cosmtrek/air
working_dir: /app
ports:
- 80:3000
env_file: ".env"
volumes:
- ./:/app
depends_on:
- mysql
mysql:
image: mariadb
environment:
Expand Down
9 changes: 5 additions & 4 deletions domain/mission/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ func Routes(router *http.ServeMux, db *sql.DB) {

basePath := os.Getenv("BASE_PATH")

router.HandleFunc("POST "+basePath+"/missions", handler.Create)
router.HandleFunc("GET "+basePath+"/missions/{id}", handler.FindByID)
router.HandleFunc("PUT "+basePath+"/missions/{id}", handler.UpdateByID)
router.HandleFunc("DELETE "+basePath+"/missions/{id}", handler.DeleteByID)
router.HandleFunc("GET "+basePath+"/missions", handler.Index)
router.HandleFunc("POST "+basePath+"/missions", handler.Store)
router.HandleFunc("GET "+basePath+"/missions/{id}", handler.Show)
router.HandleFunc("PUT "+basePath+"/missions/{id}", handler.Update)
router.HandleFunc("DELETE "+basePath+"/missions/{id}", handler.Destroy)
}
24 changes: 20 additions & 4 deletions domain/mission/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,23 @@ func NewHandler(db *sql.DB) *Handler {
}
}

func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
func (h *Handler) Index(w http.ResponseWriter, r *http.Request) {
mission, err := h.repo.GetAll()
if err != nil {
http.Error(w, fmt.Sprintf("Error finding mission: %v", err), http.StatusInternalServerError)
return
}

if mission == nil {
http.NotFound(w, r)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(mission)
}

func (h *Handler) Store(w http.ResponseWriter, r *http.Request) {
var payload struct {
Name string `json:"name"`
Description string `json:"description"`
Expand All @@ -45,7 +61,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(mission)
}

func (h *Handler) FindByID(w http.ResponseWriter, r *http.Request) {
func (h *Handler) Show(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
Expand All @@ -68,7 +84,7 @@ func (h *Handler) FindByID(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(mission)
}

func (h *Handler) UpdateByID(w http.ResponseWriter, r *http.Request) {
func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
Expand Down Expand Up @@ -102,7 +118,7 @@ func (h *Handler) UpdateByID(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Mission updated successfully")
}

func (h *Handler) DeleteByID(w http.ResponseWriter, r *http.Request) {
func (h *Handler) Destroy(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions domain/mission/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ func NewMySQLMissionRepository(db *sql.DB) *MySQLMissionRepository {
return &MySQLMissionRepository{db: db}
}

func (r *MySQLMissionRepository) GetAll() (*[]domain.Mission, error) {
query := "SELECT id, name, description, created_at, updated_at FROM missions"
rows, err := r.db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()

var missions []domain.Mission

for rows.Next() {
var mission domain.Mission
var createdAt, updatedAt []uint8

err := rows.Scan(&mission.ID, &mission.Name, &mission.Description, &createdAt, &updatedAt)
if err != nil {
return nil, fmt.Errorf("error scanning mission: %w", err)
}

missions = append(missions, mission)
}
if err = rows.Err(); err != nil {
return &missions, err
}

return &missions, nil
}

func (r *MySQLMissionRepository) Create(mission *domain.Mission) error {
query := "INSERT INTO missions (name, description) VALUES (?, ?)"
result, err := r.db.Exec(query, mission.Name, mission.Description)
Expand Down
1 change: 1 addition & 0 deletions domain/mission/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "github.com/blazeisclone/spaceops-mission-ctrl/domain"

type MissionRepository interface {
Create(mission *domain.Mission) error
GetAll() (*[]domain.Mission, error)
FindByID(id int) (*domain.Mission, error)
UpdateByID(id int, mission *domain.Mission) error
DeleteByID(id int) error
Expand Down

0 comments on commit 7bc8013

Please sign in to comment.