diff --git a/handlers/items.go b/handlers/items.go new file mode 100644 index 0000000..089c651 --- /dev/null +++ b/handlers/items.go @@ -0,0 +1,41 @@ +package handlers + +import ( + "MiauInv/models" + "MiauInv/storage" + "encoding/json" + "net/http" +) + +func GetItems(w http.ResponseWriter, r *http.Request) { + + items, err := storage.GetItems() + + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + json.NewEncoder(w).Encode(items) +} + +func CreateItem(w http.ResponseWriter, r *http.Request) { + + var item models.Item + + err := json.NewDecoder(r.Body).Decode(&item) + + if err != nil { + http.Error(w, err.Error(), 400) + return + } + + err = storage.AddItem(item) + + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + w.WriteHeader(http.StatusCreated) +} diff --git a/handlers/locations.go b/handlers/locations.go new file mode 100644 index 0000000..8fa235e --- /dev/null +++ b/handlers/locations.go @@ -0,0 +1,20 @@ +package handlers + +import ( + "MiauInv/storage" + "net/http" +) + +func CreateLocation(w http.ResponseWriter, r *http.Request) { + name := r.FormValue("name") + + _, err := storage.DB.Exec( + "INSERT INTO locations(name) VALUES(?)", + name, + ) + + if err != nil { + http.Error(w, err.Error(), 500) + return + } +} diff --git a/handlers/project_items.go b/handlers/project_items.go new file mode 100644 index 0000000..9cf41aa --- /dev/null +++ b/handlers/project_items.go @@ -0,0 +1,34 @@ +package handlers + +import ( + "MiauInv/storage" + "encoding/json" + "net/http" +) + +type ProjectItemRequest struct { + ItemID int `json:"item_id"` + ProjectID int `json:"project_id"` + Quantity int `json:"quantity"` +} + +func AllocateToProject(w http.ResponseWriter, r *http.Request) { + + var req ProjectItemRequest + + json.NewDecoder(r.Body).Decode(&req) + + _, err := storage.DB.Exec(` + INSERT INTO project_items(item_id,project_id,quantity) + VALUES(?,?,?) + `, + req.ItemID, + req.ProjectID, + req.Quantity, + ) + + if err != nil { + http.Error(w, err.Error(), 500) + return + } +} diff --git a/handlers/projects.go b/handlers/projects.go new file mode 100644 index 0000000..9d52602 --- /dev/null +++ b/handlers/projects.go @@ -0,0 +1,21 @@ +package handlers + +import ( + "MiauInv/storage" + "net/http" +) + +func CreateProject(w http.ResponseWriter, r *http.Request) { + + name := r.FormValue("name") + + _, err := storage.DB.Exec( + "INSERT INTO projects(name) VALUES(?)", + name, + ) + + if err != nil { + http.Error(w, err.Error(), 500) + return + } +} diff --git a/handlers/stock.go b/handlers/stock.go new file mode 100644 index 0000000..4826dbb --- /dev/null +++ b/handlers/stock.go @@ -0,0 +1,34 @@ +package handlers + +import ( + "MiauInv/storage" + "encoding/json" + "net/http" +) + +type StockRequest struct { + ItemID int `json:"item_id"` + LocationID int `json:"location_id"` + Quantity int `json:"quantity"` +} + +func AddStock(w http.ResponseWriter, r *http.Request) { + + var req StockRequest + + json.NewDecoder(r.Body).Decode(&req) + + _, err := storage.DB.Exec(` + INSERT INTO stock(item_id,location_id,quantity) + VALUES(?,?,?) + `, + req.ItemID, + req.LocationID, + req.Quantity, + ) + + if err != nil { + http.Error(w, err.Error(), 500) + return + } +} diff --git a/models/inventory.go b/models/inventory.go new file mode 100644 index 0000000..cdc4a73 --- /dev/null +++ b/models/inventory.go @@ -0,0 +1,34 @@ +package models + +type Item struct { + ID int `json:"id"` + Name string `json:"name"` + Category string `json:"category"` + Description string `json:"description"` + TotalQuantity int `json:"total_quantity"` +} + +type Location struct { + ID int `json:"id"` + Name string `json:"name"` +} + +type Project struct { + ID int `json:"id"` + Name string `json:"name"` + Description string `json:"description"` +} + +type Stock struct { + ID int `json:"id"` + ItemID int `json:"item_id"` + LocationID int `json:"location_id"` + Quantity int `json:"quantity"` +} + +type ProjectItem struct { + ID int `json:"id"` + ItemID int `json:"item_id"` + ProjectID int `json:"project_id"` + Quantity int `json:"quantity"` +} diff --git a/server/server.go b/server/server.go index 19aa083..7817e60 100644 --- a/server/server.go +++ b/server/server.go @@ -69,6 +69,13 @@ func (this *Server) Run() { mux.HandleFunc("/api/refresh", handlers.RefreshToken) mux.HandleFunc("/api/logout", handlers.Logout) + mux.HandleFunc("/api/items", handlers.GetItems) + mux.HandleFunc("/api/items/create", handlers.CreateItem) + mux.HandleFunc("/api/locations/create", handlers.CreateLocation) + mux.HandleFunc("/api/projects/create", handlers.CreateProject) + mux.HandleFunc("/api/stock/add", handlers.AddStock) + mux.HandleFunc("/api/project-items/add", handlers.AllocateToProject) + // Login required // Admin-only diff --git a/storage/inventory.go b/storage/inventory.go new file mode 100644 index 0000000..5d27990 --- /dev/null +++ b/storage/inventory.go @@ -0,0 +1,52 @@ +package storage + +import "MiauInv/models" + +func AddItem(item models.Item) error { + _, err := DB.Exec( + "INSERT INTO items(name, category, description, total_quantity) VALUES(?,?,?,?)", + item.Name, + item.Category, + item.Description, + item.TotalQuantity, + ) + + return err +} + +func GetItems() ([]models.Item, error) { + + rows, err := DB.Query(` + SELECT id,name,category,description,total_quantity + FROM items + `) + + if err != nil { + return nil, err + } + + defer rows.Close() + + var items []models.Item + + for rows.Next() { + + var item models.Item + + err = rows.Scan( + &item.ID, + &item.Name, + &item.Category, + &item.Description, + &item.TotalQuantity, + ) + + if err != nil { + return nil, err + } + + items = append(items, item) + } + + return items, nil +} diff --git a/storage/storage.go b/storage/storage.go index 684a21a..724c1c7 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -41,30 +41,42 @@ func InitDB(filepath string) error { FOREIGN KEY(user_id) REFERENCES users(id) ); - CREATE TABLE IF NOT EXISTS items ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - category TEXT, - total_quantity INTEGER NOT NULL DEFAULT 0 - ); - - CREATE TABLE IF NOT EXISTS locations ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL - ); - - CREATE TABLE IF NOT EXISTS projects ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL - ); - - CREATE TABLE IF NOT EXISTS item_allocations ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - item_id INTEGER NOT NULL, - location_id INTEGER, - project_id INTEGER, - quantity INTEGER NOT NULL - ); + CREATE TABLE IF NOT EXISTS items ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + category TEXT, + description TEXT, + total_quantity INTEGER NOT NULL DEFAULT 0 + ); + + CREATE TABLE IF NOT EXISTS locations ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL UNIQUE + ); + + CREATE TABLE IF NOT EXISTS projects ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL UNIQUE, + description TEXT + ); + + CREATE TABLE IF NOT EXISTS stock ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + item_id INTEGER NOT NULL, + location_id INTEGER NOT NULL, + quantity INTEGER NOT NULL, + FOREIGN KEY(item_id) REFERENCES items(id), + FOREIGN KEY(location_id) REFERENCES locations(id) + ); + + CREATE TABLE IF NOT EXISTS project_items ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + item_id INTEGER NOT NULL, + project_id INTEGER NOT NULL, + quantity INTEGER NOT NULL, + FOREIGN KEY(item_id) REFERENCES items(id), + FOREIGN KEY(project_id) REFERENCES projects(id) + ); ` _, err = DB.Exec(schema)