6 "github.com/gin-gonic/gin"
7 "github.com/iotbzh/xds-agent/lib/apiv1"
8 common "github.com/iotbzh/xds-common/golib"
11 // getProjects returns all projects configuration
12 func (s *APIService) getProjects(c *gin.Context) {
13 c.JSON(http.StatusOK, s.projects.GetProjectArr())
16 // getProject returns a specific project configuration
17 func (s *APIService) getProject(c *gin.Context) {
18 id, err := s.projects.ResolveID(c.Param("id"))
20 common.APIError(c, err.Error())
23 prj := s.projects.Get(id)
25 common.APIError(c, "Invalid id")
29 c.JSON(http.StatusOK, (*prj).GetProject())
32 // addProject adds a new project to server config
33 func (s *APIService) addProject(c *gin.Context) {
34 var cfgArg apiv1.ProjectConfig
35 if c.BindJSON(&cfgArg) != nil {
36 common.APIError(c, "Invalid arguments")
40 s.Log.Debugln("Add project config: ", cfgArg)
42 newFld, err := s.projects.Add(cfgArg, s.sessions.GetID(c))
44 common.APIError(c, err.Error())
48 c.JSON(http.StatusOK, newFld)
51 // syncProject force synchronization of project files
52 func (s *APIService) syncProject(c *gin.Context) {
53 id, err := s.projects.ResolveID(c.Param("id"))
55 common.APIError(c, err.Error())
59 s.Log.Debugln("Sync project id: ", id)
61 err = s.projects.ForceSync(id)
63 common.APIError(c, err.Error())
67 c.JSON(http.StatusOK, nil)
70 // delProject deletes project from server config
71 func (s *APIService) delProject(c *gin.Context) {
72 id, err := s.projects.ResolveID(c.Param("id"))
74 common.APIError(c, err.Error())
78 s.Log.Debugln("Delete project id ", id)
80 delEntry, err := s.projects.Delete(id, s.sessions.GetID(c))
82 common.APIError(c, err.Error())
85 c.JSON(http.StatusOK, delEntry)
88 // updateProject Update some field of a specific project
89 func (s *APIService) updateProject(c *gin.Context) {
90 id, err := s.projects.ResolveID(c.Param("id"))
92 common.APIError(c, err.Error())
96 var cfgArg apiv1.ProjectConfig
97 if c.BindJSON(&cfgArg) != nil {
98 common.APIError(c, "Invalid arguments")
102 s.Log.Debugln("Update project id ", id)
104 upPrj, err := s.projects.Update(id, cfgArg, s.sessions.GetID(c))
106 common.APIError(c, err.Error())
109 c.JSON(http.StatusOK, upPrj)