api.spaceplanner.app

Spaceplanner API
git clone git://jacobedwards.org/api.spaceplanner.app
Log | Files | Refs

floorplans.go (4350B)


      1 package main
      2 
      3 import (
      4 	"net/http"
      5 
      6 	"github.com/gin-gonic/gin"
      7 	"jacobedwards.org/spaceplanner.app/internal/backend"
      8 )
      9 
     10 type SettableFloorplan struct {
     11 	Name string `json:"name" binding:"required"`
     12 	Address *string `json:"address"`
     13 	Synopsis *string `json:"synopsis"`
     14 }
     15 
     16 type Point struct {
     17 	X *int `json:"x" binding:"required"`
     18 	Y *int `json:"y" binding:"required"`
     19 }
     20 
     21 type FloorplanURI struct {
     22 	User string `uri:"user" binding:"required"`
     23 	Floorplan backend.ObjectID `uri:"floorplan" binding:"required"`
     24 }
     25 
     26 func (e *Env) FurnitureTypes(c *gin.Context) {
     27 	types, err := e.backend.FurnitureTypes(nil)
     28 	if err != nil {
     29 		RespondError(c, 500, "%s: Unable to get types", err.Error())
     30 	} else {
     31 		Respond(c, 200, types)
     32 	}
     33 }
     34 
     35 func (e *Env) PointmapTypes(c *gin.Context) {
     36 	type resp struct {
     37 		Types []string `json:"types"`
     38 	}
     39 
     40 	types, err := e.backend.PointmapTypes()
     41 	if err != nil {
     42 		RespondError(c, 500, "Unable to get pointmap types: %s", err.Error())
     43 	} else {
     44 		Respond(c, 200, resp{ Types: types })
     45 	}
     46 }
     47 
     48 func (e *Env) CreateFloorplan(c *gin.Context) {
     49 	var req SettableFloorplan
     50 
     51 	user := c.Param("user")
     52 	if err := c.ShouldBind(&req); err != nil {
     53 		RespondError(c, 400, "%s", err.Error())
     54 		return
     55 	}
     56 	
     57 	fp, err := e.backend.CreateFloorplan(nil, &backend.Floorplan{User: user, Name: req.Name, Address: req.Address, Synopsis: req.Synopsis})
     58 	if err != nil {
     59 		RespondError(c, 400, "%s", err.Error())
     60 	} else {
     61 		Respond(c, http.StatusOK, *fp)
     62 	}
     63 }
     64 
     65 func (e *Env) UpdateFloorplan(c *gin.Context) {
     66 	var uri FloorplanURI
     67 	var req SettableFloorplan
     68 
     69 	if err := c.ShouldBindUri(&uri); err != nil {
     70 		RespondError(c, 400, err.Error())
     71 		return
     72 	}
     73 	if err := c.ShouldBind(&req); err != nil {
     74 		RespondError(c, 400, "%s", err.Error())
     75 		return
     76 	}
     77 
     78 	fp, err := e.backend.UpdateFloorplan(nil, uri.User, uri.Floorplan,
     79 	    &backend.Floorplan{User: uri.User, Name: req.Name, Address: req.Address, Synopsis: req.Synopsis})
     80 	if err != nil {
     81 		RespondError(c, 500, "Unable to update floorplan")
     82 	} else {
     83 		Respond(c, 200, fp)
     84 	}
     85 }
     86 
     87 func (e *Env) DeleteFloorplan(c *gin.Context) {
     88 	var uri FloorplanURI
     89 
     90 	if err := c.ShouldBindUri(&uri); err != nil {
     91 		RespondError(c, 400, err.Error())
     92 		return
     93 	}
     94 
     95 	fp, err := e.backend.DeleteFloorplan(nil, uri.User, uri.Floorplan)
     96 	if err != nil {
     97 		RespondError(c, 400, "%s", err.Error())
     98 	} else {
     99 		Respond(c, 200, fp)
    100 	}
    101 }
    102 
    103 func (e *Env) GetFloorplans(c *gin.Context) {
    104 	user := c.Param("user")
    105 
    106 	floorplans, err := e.backend.GetFloorplans(nil, user)
    107 	if err != nil {
    108 		RespondError(c, 400, "%s", err.Error())
    109 	} else {
    110 		Respond(c, 200, floorplans)
    111 	}
    112 }
    113 
    114 func (e *Env) GetFloorplan(c *gin.Context) {
    115 	var uri FloorplanURI
    116 
    117 	if err := c.ShouldBindUri(&uri); err != nil {
    118 		RespondError(c, 400, err.Error())
    119 		return
    120 	}
    121 
    122 	fp, err := e.backend.GetFloorplan(nil, uri.User, uri.Floorplan)
    123 	if err != nil {
    124 		RespondError(c, 400, "%s", err.Error())
    125 	} else {
    126 		Respond(c, 200, fp)
    127 	}
    128 }
    129 
    130 func (e *Env) GetFloorplanData(c *gin.Context) {
    131 	var uri FloorplanURI
    132 
    133 	if err := c.ShouldBindUri(&uri); err != nil {
    134 		RespondError(c, 400, err.Error())
    135 		return
    136 	}
    137 
    138 	data, err := e.backend.GetFloorplanData(nil, uri.User, uri.Floorplan)
    139 	if err != nil {
    140 		RespondError(c, 400, "%s", err.Error())
    141 	} else {
    142 		Respond(c, http.StatusOK, data)
    143 	}
    144 }
    145 
    146 func (e *Env) PatchFloorplanData(c *gin.Context) {
    147 	var uri FloorplanURI
    148 	var patch []backend.Patch
    149 
    150 	if err := c.ShouldBindUri(&uri); err != nil {
    151 		RespondError(c, 400, err.Error())
    152 		return
    153 	}
    154 	if err := c.ShouldBind(&patch); err != nil {
    155 		RespondError(c, 400, "%s: Unable to get patch", err.Error())
    156 		return
    157 	}
    158 
    159 	data, err := e.backend.PatchFloorplanData(nil, uri.User, uri.Floorplan, patch)
    160 	if err != nil {
    161 		RespondError(c, 400, "%s: Unable to patch floorplan", err.Error())
    162 	} else {
    163 		Respond(c, http.StatusOK, data)
    164 	}
    165 }
    166 
    167 func (e *Env) ReplaceFloorplanData(c *gin.Context) {
    168 	var uri FloorplanURI
    169 	var data *backend.FloorplanData
    170 
    171 	if err := c.ShouldBindUri(&uri); err != nil {
    172 		RespondError(c, 400, err.Error())
    173 		return
    174 	}
    175 	if err := c.ShouldBind(&data); err != nil {
    176 		RespondError(c, 400, "%s: Couldn't get floorplan data", err.Error())
    177 		return
    178 	}
    179 
    180 	newdata, err := e.backend.ReplaceFloorplanData(nil, uri.User, uri.Floorplan, data)
    181 	if err != nil {
    182 		RespondError(c, 500, "%s: Unable to update floorplan data", err.Error())
    183 		return
    184 	}
    185 	Respond(c, 200, newdata)
    186 }