-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
106 lines (92 loc) · 2.55 KB
/
server.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
package main
import (
"net/http"
"strconv"
"example.com/freshers-bootcamp/controller"
"example.com/freshers-bootcamp/middleware"
"example.com/freshers-bootcamp/repository"
"example.com/freshers-bootcamp/service"
"github.com/gin-gonic/gin"
)
var (
noteRepository repository.NoteRepository = repository.NewConnection()
noteService service.NoteService = service.New(noteRepository)
noteContrroller controller.NoteController = controller.New(noteService)
)
func main() {
defer noteRepository.CloseDB()
router := gin.New()
router.Use(gin.Recovery(), gin.Logger(), middleware.AuthouriseRoute())
router.GET("/heart-beat", func(ctx *gin.Context) {
ctx.JSON(200, gin.H{
"message": "OK!!",
})
})
v1 := router.Group("/v1/notes")
{
v1.GET("", func(c *gin.Context) {
if notes, err := noteContrroller.GetAllNotes(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Unable to Fetch All records",
})
} else {
c.JSON(http.StatusOK, notes)
}
})
v1.GET("/:id", func(c *gin.Context) {
if note, err := noteContrroller.GetSingleNote(c); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Unable to fetch record",
})
} else {
c.JSON(http.StatusOK, note)
}
})
v1.POST("/create", func(c *gin.Context) {
if id, err := noteContrroller.CreateNote(c); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Either Duplicate NoteName OR Try Again with other Note",
})
} else {
c.JSON(http.StatusOK, gin.H{
"message": "Note Successfully Created",
"id": strconv.FormatUint(id, 10),
})
}
})
v1.PATCH("/:id", func(c *gin.Context) {
if err := noteContrroller.UpdateSingleNote(c); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Not Updated. Try Again!",
})
} else {
c.JSON(http.StatusOK, gin.H{
"message": "Note Successfully Updated",
})
}
})
v1.DELETE("", func(c *gin.Context) {
if err := noteContrroller.DeleteAllNotes(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Not Deleted. Try Again!",
})
} else {
c.JSON(http.StatusOK, gin.H{
"message": "Successfully Deleted All notes",
})
}
})
v1.DELETE("/:id", func(c *gin.Context) {
if err := noteContrroller.DeleteSingleNote(c); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Not Deleted. Try Again!",
})
} else {
c.JSON(http.StatusOK, gin.H{
"message": "Successfully Deleted the note",
})
}
})
}
router.Run(":8080")
}