-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectHandler.go
More file actions
79 lines (72 loc) · 1.73 KB
/
projectHandler.go
File metadata and controls
79 lines (72 loc) · 1.73 KB
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 handlers
import (
"github.com/Arjuna-Ragil/Localbase/Internal/core/services"
"github.com/gin-gonic/gin"
)
type ProjectHandler struct {
ProjectService *services.ProjectService
}
func NewProjectHandler(projectService *services.ProjectService) *ProjectHandler {
return &ProjectHandler{ProjectService: projectService}
}
func (ph *ProjectHandler) CreateProjectHandler(c *gin.Context) {
var input services.CreateInput
userRole, _ := c.Get("userRole")
if userRole != "admin" {
c.JSON(401, gin.H{
"message": "You are not authorized to perform this action",
"data": nil,
})
return
}
err := c.ShouldBindJSON(&input)
if err != nil {
c.JSON(400, gin.H{
"message": "Invalid input",
"data": err.Error(),
})
return
}
project, err := ph.ProjectService.CreateProject(&input)
if err != nil {
c.JSON(500, gin.H{
"message": "failed to create project",
"data": err.Error(),
})
return
}
c.JSON(200, gin.H{
"message": "successfully created project",
"data": project,
})
}
func (ph *ProjectHandler) GetAllProjectHandler(c *gin.Context) {
userRole := c.GetString("userRole")
projects, err := ph.ProjectService.GetProjects(userRole)
if err != nil {
c.JSON(500, gin.H{
"message": "failed to get projects",
"data": err.Error(),
})
return
}
c.JSON(200, gin.H{
"message": "got all projects",
"data": projects,
})
}
func (ph *ProjectHandler) GetProjectHandler(c *gin.Context) {
projectId := c.Param("projectid")
project, err := ph.ProjectService.GetProjectById(projectId)
if err != nil {
c.JSON(500, gin.H{
"message": "failed to get project",
"data": err.Error(),
})
return
}
c.JSON(200, gin.H{
"message": "success in fetching project",
"data": project,
})
}