Added Functionality To PowerItem Endpoint

TODO: Change "addMultiple" to not require a map. That only exists so I could import easily from my old project.
This commit is contained in:
Annika Merris 2024-01-28 13:57:25 -05:00
parent d52d9968ba
commit 1420b14565
7 changed files with 373 additions and 95 deletions

View file

@ -1,73 +1,180 @@
package Controllers
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"isl-api/sql/powerItem"
"isl-api/Services"
"isl-api/Types"
"github.com/jackc/pgx/v4"
"github.com/julienschmidt/httprouter"
)
func SetPowerItemEndpoints(mux *http.ServeMux, prefix string) {
mux.HandleFunc(fmt.Sprintf("/%s", prefix), PowerItemFunc)
mux.HandleFunc(fmt.Sprintf("/%s/all", prefix), PowerItemAll)
type PowerItemController struct {
powerItemService *Services.PowerItemService
}
func PowerItemFunc(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
fmt.Fprintf(w, "Hello")
// handle a GET
case http.MethodPost:
var newItem powerItem.AddNewItemWithIDParams
err := json.NewDecoder(r.Body).Decode(&newItem)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(w, "%+v", newItem)
// handle a POST
case http.MethodOptions:
w.Header().Set("Allow", "GET, POST, OPTIONS")
w.WriteHeader(http.StatusNoContent)
default:
w.Header().Set("Allow", "GET, POST, OPTIONS")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
func NewPowerItemController(router *httprouter.Router, powerItemService *Services.PowerItemService) *PowerItemController {
controller := &PowerItemController{
powerItemService: powerItemService,
}
controller.setPowerItemEndpoints(router, "/powerItem")
return controller
}
func (w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
ctx := context.TODO()
conn, err := pgx.Connect(ctx, "postgres://isl:development@localhost:5432")
if err != nil {
panic(err)
}
q := powerItem.NewQuerier(conn)
rows, err := q.GetAllItems(ctx)
if err != nil {
panic(err)
}
data, err := json.Marshal(rows)
if err != nil {
panic(err)
}
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, string(data))
func (p *PowerItemController) setPowerItemEndpoints(router *httprouter.Router, prefix string) {
router.HandlerFunc("GET", fmt.Sprintf("%v", prefix), p.getAll)
router.HandlerFunc("GET", fmt.Sprintf("%v/asMap", prefix), p.getAllAsMap)
router.HandlerFunc("GET", fmt.Sprintf("%v/byType/:type", prefix), p.getAllByType)
router.HandlerFunc("GET", fmt.Sprintf("%v/byType/:type/asMap", prefix), p.getAllByTypeAsMap)
router.HandlerFunc("POST", fmt.Sprintf("%v", prefix), p.add)
router.HandlerFunc("POST", fmt.Sprintf("%v/multiple", prefix), p.addMultiple)
}
case http.MethodOptions:
w.Header().Set("Allow", "GET, OPTIONS")
w.WriteHeader(http.StatusNoContent)
default:
w.Header().Set("Allow", "GET, OPTIONS")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
func (p *PowerItemController) add(w http.ResponseWriter, r *http.Request) {
var newItem Types.PowerItem
err := json.NewDecoder(r.Body).Decode(&newItem)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result, err := p.powerItemService.Add(newItem)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
returnValue, err := json.Marshal(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(returnValue))
}
func (p *PowerItemController) addMultiple(w http.ResponseWriter, r *http.Request) {
var itemType int32 = 3
var newItems map[string]Types.PowerItem
err := json.NewDecoder(r.Body).Decode(&newItems)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
added, errors := p.powerItemService.AddMultipile(newItems, itemType)
if len(errors) != 0 {
http.Error(w, "Could not add item", http.StatusInternalServerError)
for _, err := range errors {
fmt.Println(err.Error())
}
return
}
returnValue, err := json.Marshal(added)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(returnValue))
}
func (p *PowerItemController) getAll(w http.ResponseWriter, r *http.Request) {
result, err := p.powerItemService.GetAll()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(data))
}
func (p *PowerItemController) getAllAsMap(w http.ResponseWriter, r *http.Request) {
items, err := p.powerItemService.GetAll()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resultMap := make(map[string]Types.PowerItem)
for _, curItem := range items {
uuid := fmt.Sprintf("%x-%x-%x-%x-%x", curItem.ID.Bytes[0:4], curItem.ID.Bytes[4:6], curItem.ID.Bytes[6:8], curItem.ID.Bytes[8:10], curItem.ID.Bytes[10:16])
resultMap[uuid] = curItem
}
data, err := json.Marshal(resultMap)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(data))
}
func (p *PowerItemController) getAllByType(w http.ResponseWriter, r *http.Request) {
params := httprouter.ParamsFromContext(r.Context())
typeCode, err := strconv.Atoi(params.ByName("type"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result, err := p.powerItemService.GetAllByType(typeCode)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(data))
}
func (p *PowerItemController) getAllByTypeAsMap(w http.ResponseWriter, r *http.Request) {
params := httprouter.ParamsFromContext(r.Context())
typeCode, err := strconv.Atoi(params.ByName("type"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
items, err := p.powerItemService.GetAllByType(typeCode)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resultMap := make(map[string]Types.PowerItem)
for _, curItem := range items {
uuid := fmt.Sprintf("%x-%x-%x-%x-%x", curItem.ID.Bytes[0:4], curItem.ID.Bytes[4:6], curItem.ID.Bytes[6:8], curItem.ID.Bytes[8:10], curItem.ID.Bytes[10:16])
resultMap[uuid] = curItem
}
data, err := json.Marshal(resultMap)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(data))
}