Пример #1
0
func (tr *X10DeviceResource) CreateDevice(c *gin.Context) {
	var device api.X10Device

	if err := c.Bind(&device); err != nil {
		c.JSON(400, api.NewError("problem decoding body"))
		return
	}
	// device.Status = api.TodoStatus
	device.Created = int32(time.Now().Unix())

	tr.db.Save(&device)

	c.JSON(201, device)
}
Пример #2
0
func (tr *X10DeviceResource) UpdateDevice(c *gin.Context) {
	id, err := tr.getId(c)
	if err != nil {
		c.JSON(400, api.NewError("problem decoding id sent"))
		return
	}

	var device api.X10Device
	if err = c.Bind(&device); err != nil {
		c.JSON(400, api.NewError("problem decoding json"))
		return
	}
	device.Id = int32(id)

	var existing api.X10Device

	if tr.db.First(&existing, id).RecordNotFound() {
		c.JSON(404, api.NewError("not found"))
	} else {
		tr.db.Save(&device)
		c.JSON(200, device)
	}

}