mirror of
https://forgejo.merr.is/annika/isl-api.git
synced 2025-12-11 11:12:06 -05:00
176 lines
4.6 KiB
Go
176 lines
4.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"forgejo.merr.is/annika/isl-api/entities"
|
|
"forgejo.merr.is/annika/isl-api/services"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/gofrs/uuid"
|
|
"github.com/jackc/pgtype"
|
|
)
|
|
|
|
type PowerItemController struct {
|
|
powerItemService *services.PowerItemService
|
|
}
|
|
|
|
func NewPowerItemController(powerItemService *services.PowerItemService) *PowerItemController {
|
|
controller := &PowerItemController{
|
|
powerItemService: powerItemService,
|
|
}
|
|
return controller
|
|
}
|
|
|
|
func (p *PowerItemController) Add(w http.ResponseWriter, r *http.Request) {
|
|
var newItem entities.PowerItem
|
|
err := json.NewDecoder(r.Body).Decode(&newItem)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if newItem.ID.Status == pgtype.Null || newItem.ID.Status == pgtype.Undefined {
|
|
newID, err := uuid.DefaultGenerator.NewV4()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
newItem.ID.Set(newID)
|
|
}
|
|
|
|
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]entities.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]entities.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) {
|
|
typeCode, err := strconv.Atoi(chi.URLParam(r, "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) {
|
|
typeCode, err := strconv.Atoi(chi.URLParam(r, "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]entities.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))
|
|
}
|