Example #1
0
func Create(c *gin.Context) {
	var form CreatePetForm

	if err := c.Bind(&form); err == nil {
		pet := &models.Pet{Name: form.Name, Age: form.Age}
		db.DB.Create(pet)
		logger.WithField("pet", pet).Infof("pet created.")

		c.JSON(http.StatusOK, gin.H{"pet": pet})
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err})
	}
}
Example #2
0
func Create(c *gin.Context) {
	var form OwnerForm

	if err := c.Bind(&form); err == nil {
		owner := &models.Owner{FirstName: form.FirstName, LastName: form.LastName}
		db.DB.Create(owner)
		logger.WithField("owner", owner).Infof("owner saved.")

		c.JSON(http.StatusOK, gin.H{"owner": owner})
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err})
	}
}
Example #3
0
func Create(c *gin.Context) {
	var form CreateTaskForm

	if err := c.Bind(&form); err == nil {
		task := &models.Task{Title: form.Title, Description: form.Description}
		db.DB.Create(task)
		logger.WithField("task", task).Infof("task created.")

		c.JSON(http.StatusOK, gin.H{"task": task})
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err})
	}
}
Example #4
0
func Update(c *gin.Context) {
	var form UpdateTaskForm

	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {

	}

	if err := c.Bind(&form); err == nil {
		task := &models.Task{Title: form.Title, Description: form.Description}
		db.DB.First(task, id).Update(task)
		logger.WithField("task", task).Infof("task saved.")

		c.JSON(http.StatusOK, gin.H{"task": task})
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err})
	}
}
Example #5
0
func Update(c *gin.Context) {
	var form UpdatePetForm

	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {

	}

	if err := c.Bind(&form); err == nil {
		pet := &models.Pet{OwnerId: form.OwnerId}
		db.DB.First(pet, id).Update(pet)
		logger.WithField("pet", pet).Infof("pet saved.")

		c.JSON(http.StatusOK, gin.H{"pet": pet})
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err})
	}
}