/
kontak.go
153 lines (141 loc) · 4.04 KB
/
kontak.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package controller
import (
"errors"
"fmt"
"github.com/Bukulapak/KontakAPI/config"
"github.com/gofiber/fiber/v2"
inimodel "github.com/indrariksa/be_presensi/model"
inimodul "github.com/indrariksa/be_presensi/module"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"net/http"
)
func Home(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"github_repo": "https://github.com/Bukulapak/KontakAPI",
"message": "You are at the root endpoint 😉",
"success": true,
})
}
func GetAll(c *fiber.Ctx) error {
ps := inimodul.GetAllContact(config.Ulbimongoconn, "kontak")
return c.JSON(ps)
}
func GetKontakID(c *fiber.Ctx) error {
id := c.Params("id")
if id == "" {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": http.StatusInternalServerError,
"message": "Wrong parameter",
})
}
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": http.StatusBadRequest,
"message": "Invalid id parameter",
})
}
ps, err := inimodul.GetKontakFromID(objID, config.Ulbimongoconn, "kontak")
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return c.Status(http.StatusNotFound).JSON(fiber.Map{
"status": http.StatusNotFound,
"message": fmt.Sprintf("No data found for id %s", id),
})
}
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": http.StatusInternalServerError,
"message": fmt.Sprintf("Error retrieving data for id %s", id),
})
}
return c.JSON(ps)
}
func InsertData(c *fiber.Ctx) error {
db := config.Ulbimongoconn
var kontak inimodel.Kontak
if err := c.BodyParser(&kontak); err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
}
insertedID, err := inimodul.InsertKontak(db, "kontak",
kontak.NamaKontak,
kontak.NomorHp,
kontak.Alamat,
kontak.Keterangan,
)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"status": http.StatusOK,
"message": "Data berhasil disimpan.",
"inserted_id": insertedID,
})
}
func UpdateData(c *fiber.Ctx) error {
db := config.Ulbimongoconn
id := c.Params("id")
var kontak inimodel.Kontak
if err := c.BodyParser(&kontak); err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
}
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
}
err = inimodul.UpdateKontak(db, "kontak", oid,
kontak.NamaKontak,
kontak.NomorHp,
kontak.Alamat,
kontak.Keterangan,
)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"status": http.StatusOK,
"message": "Data berhasil diupdate.",
})
}
func DeleteKontak(c *fiber.Ctx) error {
id := c.Params("id")
if id == "" {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": http.StatusInternalServerError,
"message": "Wrong parameter",
})
}
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": http.StatusBadRequest,
"message": "Invalid id parameter",
})
}
err = inimodul.DeleteKontakByID(objID, config.Ulbimongoconn, "kontak")
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": http.StatusInternalServerError,
"message": fmt.Sprintf("Error deleting data for id %s", id),
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"status": http.StatusOK,
"message": fmt.Sprintf("Data with id %s deleted successfully", id),
})
}