Started with items
This commit is contained in:
41
handlers/items.go
Normal file
41
handlers/items.go
Normal file
@@ -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)
|
||||
}
|
||||
20
handlers/locations.go
Normal file
20
handlers/locations.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
34
handlers/project_items.go
Normal file
34
handlers/project_items.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
21
handlers/projects.go
Normal file
21
handlers/projects.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
34
handlers/stock.go
Normal file
34
handlers/stock.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user