-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobacoba.go
116 lines (98 loc) · 2.33 KB
/
cobacoba.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
package main
import (
"fmt"
"math/rand"
"time"
"net/http"
"strconv"
// "log"
"github.com/labstack/echo/v4"
// _ "github.com/go-sql-driver/mysql"
)
// var db *gorm.DB
// var err error
type User struct {
Id int `json:"id"`
Nickname string `json:"nickname"`
Emails string `json:"email"`
// Roles int `json:"role"`
}
type TipeRole struct {
Roles[]Role `json:"role"`
}
type Users []User
var users = Users{User{generateId,"Ari Wijaya","@gmail.com"/*,generateRole*/}}
func generateId() int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(10000)
}
// func generateRole() int {
//
//
// }
//lanjut disini
func listUser(c echo.Context) error {
// User ID from path `users/:id`
return c.JSON(http.StatusOK, users)
}
func newUser(c echo.Context) error {
user := User{}
err := c.Bind(&user)
if err != nil {
return echo.NewHTTPError(http.StatusUnprocessableEntity)
}
user.Id = generateId()
users = append(users, user)
return c.JSON(http.StatusCreated, users)
}
func detailUser(c echo.Context) error {
id, _ := strconv.Atot(c.Param("id"))
for _, user := range users {
if user.Id == id {
return c.JSON(http.StatusOK, user)
}
}
return c.JSON(http.StatusBadRequest, nil)
}
func updateUser(c echo.Context) error {
id, _ := strconv.Atot(c.Param("id"))
for i, _ := range users {
if users[i].Id == id {
users[i].Online = false
return c.JSON(http.StatusOK, users)
}
}
return c.JSON(http.StatusBadRequest, nil)
}
func deleteUser(c echo.Context) error {
id, _ := strconv.Atot(c.Param("id"))
for i, _ := range users {
if users[i].Id == id {
users = append(users[:i],users[i+1]...)
return c.JSON(http.StatusOK, users)
}
}
return c.JSON(http.StatusBadRequest, nil)
}
func main() {
fmt.Println("Running...")
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, welcome to echo!")
})
// db, err := gorm.Open("mysql", "root:Gebangsari1@/go_rest_api_crud?charset=utf8&parseTime=True")
// if err != nil {
// log.Println("Connection failed", err)
// } else {
// log.Println("Connection enstabilshed")
// }
//
// db.AutoMigrate(&Product{})
// handleRequests()
e.POST("/user", newUser)
e.GET("/user/", listUser)
e.GET("/user/:id", detailUser)
e.PUT("/user/:id", updateUser)
e.DELETE("/user/:id", deleteUser)
e.Logger.Fatal(e.Start(":1323"))
}