-
Notifications
You must be signed in to change notification settings - Fork 0
/
extras.go
79 lines (61 loc) · 1.97 KB
/
extras.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package controllers
import (
"net/http"
"strconv"
"github.com/anggakharisma/spice-republic/api/db"
"github.com/anggakharisma/spice-republic/api/models"
"github.com/gin-gonic/gin"
)
type ExtraRequest struct {
Name string `json:"name" binding:"required"`
MeasuremntTypeID uint `json:"measurement_type_id" binding:"required"`
}
type UpdateExtraInput struct {
Name string `json:"name" binding:"required"`
}
func FindExtras(c *gin.Context) {
var extras []models.Extra
db.DB.Find(&extras)
c.JSON(http.StatusOK, gin.H{"data": extras})
}
func FindExtra(c *gin.Context) {
var extra models.Extra
if err := db.DB.Where("id = ?", c.Param("id")).First(&extra); err.Error != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error})
return
}
c.JSON(http.StatusOK, gin.H{"data": extra})
}
func CreateExtra(c *gin.Context) {
var extraRequest ExtraRequest
if err := c.BindJSON(&extraRequest); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
extra := models.Extra{Name: extraRequest.Name, MeasurementTypeId: extraRequest.MeasuremntTypeID}
db.DB.Create(&extra)
c.JSON(http.StatusOK, gin.H{"data": "extra " + strconv.Itoa(int(extra.ID)) + " created"})
}
func UpdateExtra(c *gin.Context) {
var extra models.Extra
if err := db.DB.Where("id = ?", c.Param("id")).First(&extra).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var updateExtraInput UpdateExtraInput
if err := c.ShouldBindJSON(&updateExtraInput); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
db.DB.Model(&extra).Updates(updateExtraInput)
c.JSON(http.StatusOK, gin.H{"data": "data updated"})
}
func DeleteExtra(c *gin.Context) {
var extra models.Extra
if err := db.DB.Where("id = ?", c.Param("id")).First(&extra).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
db.DB.Delete(&extra)
c.JSON(http.StatusOK, gin.H{"data": true})
}