Skip to content

Commit

Permalink
setting up router and request funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
ananyabatra04 committed Jan 28, 2024
1 parent a201082 commit c1d8a6e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/leodido/go-urn v1.3.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZY
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/leodido/go-urn v1.3.0 h1:jX8FDLfW4ThVXctBNZ+3cIWnCSnrACDV73r76dy0aQQ=
github.com/leodido/go-urn v1.3.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand Down
32 changes: 32 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"time"

_ "github.com/mattn/go-sqlite3"
)

type Task struct {
TaskID int
UserID string
Category string
TaskName string
Description string
StartTime time.Time
EndTime time.Time
IsCompleted bool
IsRecurring bool
IsAllDay bool
}

type TaskPreview struct {
TaskID int
UserID string
Category string
TaskName string
StartTime time.Time
EndTime time.Time
IsCompleted bool
IsRecurring bool
IsAllDay bool
}
69 changes: 52 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import "github.com/gin-gonic/gin"
import "net/http"
import "os"
import "github.com/jmoiron/sqlx"
import _"github.com/mattn/go-sqlite3"

import (
"net/http"
"os"

"github.com/gin-gonic/gin"
"github.com/jmoiron/sqlx"

"fmt"

_ "github.com/mattn/go-sqlite3"
)

func main() {
Expand All @@ -17,27 +19,60 @@ func main() {
"message": "pong",
})
})
// exactly the same as the built-in
// exactly the same as the built-in
schemacreate, erro := os.ReadFile("schema.sql")
if(erro!=nil){
if erro != nil {
fmt.Println("breaky")
}
fmt.Println(string(schemacreate))
db,err := sqlx.Open("sqlite3", ":memory:")
if err != nil{
db, err := sqlx.Open("sqlite3", ":memory:")
if err != nil {
fmt.Println("breaky")
}
// force a connection and test that it worked
swagmoney := db.Ping()

// force a connection and test that it worked
swagmoney := db.Ping()

db.MustExec(string(schemacreate))
if swagmoney != nil{
if swagmoney != nil {
fmt.Println("breaky")
}else{
} else {
fmt.Println("not breaky")
}

fmt.Println("Running at http://localhost:8080")

//Router: takes incoming requests and routes them to functions to handle them
//Building a group of routes starting with this path
v1 := r.Group("/main/blah") //TODO: FIX the route and the uri's below
{
v1.GET("tasks", getAllUserTasks)
v1.GET("task/:id", getTaskById)
v1.POST("tasks", createTask)
v1.PUT("tasks/:id", editTask)
v1.DELETE("tasks/:id", deleteTask)

}

fmt.Println("Running at http://localhost:8080")
r.Run() // listen and serve on 0.0.0.0:8080
}

func createTask(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Called createTask"})
}

func editTask(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Called editTask"})
}

func deleteTask(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Called deleteTask"})
}

func getAllUserTasks(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Called getAllUserTasks"})
}

func getTaskById(c *gin.Context) {
id := c.Param("id")
c.JSON(http.StatusOK, gin.H{"message": "Called GetTaskById on Id:" + id})
}

0 comments on commit c1d8a6e

Please sign in to comment.