mirror of
https://forgejo.merr.is/annika/isl-api.git
synced 2025-12-11 23:13:27 -05:00
TODO: Change "addMultiple" to not require a map. That only exists so I could import easily from my old project.
180 lines
5 KiB
Go
180 lines
5 KiB
Go
package Controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"isl-api/Services"
|
|
"isl-api/Types"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
)
|
|
|
|
type PowerItemController struct {
|
|
powerItemService *Services.PowerItemService
|
|
}
|
|
|
|
func NewPowerItemController(router *httprouter.Router, powerItemService *Services.PowerItemService) *PowerItemController {
|
|
controller := &PowerItemController{
|
|
powerItemService: powerItemService,
|
|
}
|
|
controller.setPowerItemEndpoints(router, "/powerItem")
|
|
return controller
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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))
|
|
}
|