Skip to content

Commit

Permalink
fully tested controller & route for GetAllTask route
Browse files Browse the repository at this point in the history
  • Loading branch information
pacholoamit committed Jun 24, 2022
1 parent 0020dd5 commit faa69a6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.db
7 changes: 2 additions & 5 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package main

import (
"net/http"

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

func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
routes.TaskRoutes(e)
e.Logger.Fatal(e.Start(":1323"))
}
25 changes: 24 additions & 1 deletion pkg/controllers/task.controllers.go
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
package controllers
package controllers

import (
"net/http"

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


var NewTask models.Task

func GetAllTasks(c echo.Context) error {
newTasks:=models.GetAllTasks()
return c.JSON(http.StatusOK, newTasks)
}

func CreateTask(c echo.Context) error {
t := new(models.Task)
if err := c.Bind(t); err != nil {
return err
}
return c.JSON(http.StatusOK, t)
}
32 changes: 31 additions & 1 deletion pkg/models/tasks.models.go
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
package models
package models

import (
"github.com/pacholoamit/GO-TASK-MGR/pkg/config"
"gorm.io/gorm"
)

var db *gorm.DB

type Task struct{
gorm.Model
Title string `json:"title" form:"title" query:"title" `
Description string `json:"description" form:"description" query:"description"`
}

func init() {
config.Connect()
db = config.GetDB()
db.AutoMigrate(&Task{})
}

func CreateTask(t *Task) *Task {
db.Create(&t)
return t
}

func GetAllTasks() []Task {
var Tasks []Task
db.Find(&Tasks)
return Tasks
}
10 changes: 5 additions & 5 deletions pkg/routes/tasks.routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (


var TaskRoutes = func(router *echo.Echo) {
router.GET("/tasks/", controllers.GetAllTasks)
router.POST("/task/", controllers.CreateTask)
router.GET("/task/:id",controllers.GetTask)
router.PUT("/task/:id", controllers.UpdateTask)
router.DELETE("/task/:id", controllers.DeleteTask)
router.GET("/tasks", controllers.GetAllTasks)
// router.POST("/task", controllers.CreateTask)
// router.GET("/task/:id",controllers.GetTask)
// router.PUT("/task/:id", controllers.UpdateTask)
// router.DELETE("/task/:id", controllers.DeleteTask)
}

0 comments on commit faa69a6

Please sign in to comment.