Skip to content

Commit

Permalink
Added Project controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
pacholoamit committed Jun 26, 2022
1 parent cf049ed commit 02480ca
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
51 changes: 46 additions & 5 deletions pkg/controllers/project.controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,71 @@ package controllers

import (
"net/http"
"strconv"

"github.com/labstack/echo/v4"
"github.com/pacholoamit/GO-TASK-MGR/pkg/models"
"github.com/pacholoamit/GO-TASK-MGR/pkg/services"
)

type project struct{}

var Project project

func (project) GetAllProjects(c echo.Context) error {
return c.JSON(http.StatusOK, "All Projects")
ap, err := services.Project.GetAllProjects()
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
return c.JSON(http.StatusOK, ap)
}

func (project) CreateProject(c echo.Context) error {
return c.JSON(http.StatusOK, "Creating Project")
pr := new(models.Project)
if err := c.Bind(pr); err != nil {
return err
}

cp, err := services.Project.CreateProject(pr)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
return c.JSON(http.StatusCreated, cp)
}

func (project) GetProject(c echo.Context) error {
return c.JSON(http.StatusOK, "Getting single project")
p := c.Param("id")
id, _ := strconv.Atoi(p)
gp, err := services.Project.GetProject(id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err)
}
return c.JSON(http.StatusOK, gp)
}

func (project) UpdateProject(c echo.Context) error {
return c.JSON(http.StatusOK, "Updating a project")
p := c.Param("id")
pr := new(models.Project)
id, _ := strconv.Atoi(p)

if err := c.Bind(pr); err != nil {
return err
}

up, err := services.Project.UpdateProject(id, pr)

if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err)
}
return c.JSON(http.StatusOK, up)
}

func (project) DeleteProject(c echo.Context) error {
return c.JSON(http.StatusOK, "Deleting a Project")
p := c.Param("id")
id, _ := strconv.Atoi(p)
dp, err := services.Project.DeleteProject(id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err)
}
return c.JSON(http.StatusOK, dp)
}
5 changes: 5 additions & 0 deletions pkg/services/project.services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package services

type project struct{}

var Project project

0 comments on commit 02480ca

Please sign in to comment.