-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from BlazeIsClone/develop
update: add domain mission
- Loading branch information
Showing
11 changed files
with
232 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
internal/database/mysql/migrations/000001_create_missions_table.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |