-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpegawai.controller.go
119 lines (92 loc) · 2.46 KB
/
pegawai.controller.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
package controllers
import (
"database/sql"
"fmt"
"github.com/labstack/echo/v4"
_ "github.com/lib/pq"
"go-echo-api/config"
"go-echo-api/models"
"net/http"
"strconv"
)
type Pegawai struct {
Id int `json:"id"`
Nama string `json:"nama"`
Alamat string `json:"alamat"`
Telepon string `json:"telepon"`
}
type Response struct {
Status int `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func FetchListPegawaiController(c echo.Context) error {
conf := config.GetConfig()
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
conf.DB_HOST, conf.DB_PORT, conf.DB_USERNAME, conf.DB_PASSWORD, conf.DB_NAME)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
query := "SELECT * FROM pegawai ORDER BY id ASC"
rows, err := db.Query(query)
defer rows.Close()
var arrobj []Pegawai
var obj Pegawai
for rows.Next() {
err := rows.Scan(&obj.Id, &obj.Nama, &obj.Alamat, &obj.Telepon)
if err != nil {
panic(err)
}
arrobj = append(arrobj, obj)
}
res := &Response{
Status: http.StatusOK,
Message: "Success",
Data: arrobj,
}
return c.JSON(http.StatusOK, res)
}
func FetchCreatePegawaiController(c echo.Context) error {
nama := c.FormValue("nama")
alamat := c.FormValue("alamat")
telepon := c.FormValue("telepon")
result, err := models.FetchCreatePegawaiModel(nama, alamat, telepon)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
func FetchUpdatePegawaiController(c echo.Context) error {
id := c.FormValue("id")
nama := c.FormValue("nama")
alamat := c.FormValue("alamat")
telepon := c.FormValue("telepon")
conv_id, err := strconv.Atoi(id)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
result, err := models.FetchUpdatePegawaiModel(conv_id, nama, alamat, telepon)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
func FetchDeletePegawaiController(c echo.Context) error {
id := c.FormValue("id")
conv_id, err := strconv.Atoi(id)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
result, err := models.FetchDeletePegawaiModel(conv_id)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}