Skip to content

Commit

Permalink
Merge pull request #19 from BlazeIsClone/develop
Browse files Browse the repository at this point in the history
update: add domain mission
  • Loading branch information
BlazeIsClone authored Oct 22, 2024
2 parents 85b37be + ffbf34c commit 2660bf2
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 97 deletions.
8 changes: 4 additions & 4 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"os"

"github.com/blazeisclone/spaceops-mission-ctrl/domain/organization"
"github.com/blazeisclone/spaceops-mission-ctrl/domain/mission"
"github.com/blazeisclone/spaceops-mission-ctrl/instrumenting"
mysqlDB "github.com/blazeisclone/spaceops-mission-ctrl/internal/database/mysql"

Expand All @@ -25,9 +25,6 @@ func main() {

router := http.NewServeMux()

organization.Routes(router)
instrumenting.Routes(router)

port := os.Getenv("PORT")

server := http.Server{
Expand All @@ -47,5 +44,8 @@ func main() {
fmt.Println("db.Closed")
}()

instrumenting.Routes(router)
mission.Routes(router, db)

server.ListenAndServe()
}
9 changes: 0 additions & 9 deletions domain/item.go

This file was deleted.

11 changes: 11 additions & 0 deletions domain/mission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package domain

import "time"

type Mission struct {
ID int
Name string
Description string
CreatedAt time.Time
UpdatedAt time.Time
}
18 changes: 18 additions & 0 deletions domain/mission/endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mission

import (
"database/sql"
"net/http"
"os"
)

func Routes(router *http.ServeMux, db *sql.DB) {
handler := NewHandler(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)
}
121 changes: 121 additions & 0 deletions domain/mission/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package mission

import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/blazeisclone/spaceops-mission-ctrl/domain"
)

type Handler struct {
repo MissionRepository
}

func NewHandler(db *sql.DB) *Handler {
return &Handler{
repo: NewMySQLMissionRepository(db),
}
}

func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
var payload struct {
Name string `json:"name"`
Description string `json:"description"`
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "Invalid input", http.StatusBadRequest)
return
}

mission := &domain.Mission{
Name: payload.Name,
Description: payload.Description,
}

if err := h.repo.Create(mission); err != nil {
http.Error(w, fmt.Sprintf("Error creating mission: %v", err), http.StatusInternalServerError)
return
}

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

func (h *Handler) FindByID(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid mission ID", http.StatusBadRequest)
return
}

mission, err := h.repo.FindByID(id)
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) UpdateByID(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid mission ID", http.StatusBadRequest)
return
}

var payload struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "Invalid input", http.StatusBadRequest)
return
}

mission := &domain.Mission{
ID: payload.ID,
Name: payload.Name,
Description: payload.Description,
}

err = h.repo.UpdateByID(id, mission)
if err != nil {
http.Error(w, fmt.Sprintf("Error updating mission: %v", err), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Mission updated successfully")
}

func (h *Handler) DeleteByID(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid mission ID", http.StatusBadRequest)
return
}

err = h.repo.DeleteByID(id)
if err != nil {
http.Error(w, fmt.Sprintf("Error deleting mission: %v", err), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Mission deleted successfully")
}
67 changes: 67 additions & 0 deletions domain/mission/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package mission

import (
"database/sql"
"fmt"

"github.com/blazeisclone/spaceops-mission-ctrl/domain"
)

type MySQLMissionRepository struct {
db *sql.DB
}

func NewMySQLMissionRepository(db *sql.DB) *MySQLMissionRepository {
return &MySQLMissionRepository{db: db}
}

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)
if err != nil {
return fmt.Errorf("error creating mission: %w", err)
}

id, err := result.LastInsertId()
if err != nil {
return fmt.Errorf("error getting last insert id: %w", err)
}

mission.ID = int(id)
return nil
}

func (r *MySQLMissionRepository) FindByID(id int) (*domain.Mission, error) {
query := "SELECT id, name, description, created_at, updated_at FROM missions WHERE id = ?"
row := r.db.QueryRow(query, id)

var mission domain.Mission
var createdAt, updatedAt []uint8
err := row.Scan(&mission.ID, &mission.Name, &mission.Description, &createdAt, &updatedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, fmt.Errorf("error finding mission: %w", err)
}

return &mission, nil
}

func (r *MySQLMissionRepository) UpdateByID(id int, mission *domain.Mission) error {
query := "UPDATE missions SET name = ?, description = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?"
_, err := r.db.Exec(query, mission.Name, mission.Description, id)
if err != nil {
return fmt.Errorf("error updating mission: %w", err)
}
return nil
}

func (r *MySQLMissionRepository) DeleteByID(id int) error {
query := "DELETE FROM missions WHERE id = ?"
_, err := r.db.Exec(query, id)
if err != nil {
return fmt.Errorf("error deleting mission: %w", err)
}
return nil
}
10 changes: 10 additions & 0 deletions domain/mission/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mission

import "github.com/blazeisclone/spaceops-mission-ctrl/domain"

type MissionRepository interface {
Create(mission *domain.Mission) error
FindByID(id int) (*domain.Mission, error)
UpdateByID(id int, mission *domain.Mission) error
DeleteByID(id int) error
}
17 changes: 0 additions & 17 deletions domain/organization/endpoint.go

This file was deleted.

23 changes: 0 additions & 23 deletions domain/organization/handler.go

This file was deleted.

44 changes: 0 additions & 44 deletions domain/organization/product.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TABLE missions (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

0 comments on commit 2660bf2

Please sign in to comment.