-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Routes and directory structure
- Loading branch information
1 parent
fcba606
commit c840eb2
Showing
7 changed files
with
356 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "./main.go" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
// "github.com/go-pg/pg/v9" | ||
guuid "github.com/google/uuid" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"harshsinghvi/golang-postgres-kubernetes/database" | ||
"harshsinghvi/golang-postgres-kubernetes/models" | ||
) | ||
|
||
// var (connection *pg.DB )// = *database.GetDatabase() | ||
|
||
// init() { | ||
// connection = = *database.GetDatabase() | ||
// } | ||
|
||
// connection := *database.GetDatabase() | ||
|
||
func GetAllTodos(c *gin.Context) { | ||
|
||
var todos []models.Todo | ||
err := database.Connection.Model(&todos).Select() | ||
if err != nil { | ||
log.Printf("Error while getting all todos, Reason: %v\n", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"status": http.StatusInternalServerError, | ||
"message": "Something went wrong", | ||
}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{ | ||
"status": http.StatusOK, | ||
"message": "All Todos", | ||
"data": todos, | ||
}) | ||
} | ||
|
||
func GetSingleTodo(c *gin.Context) { | ||
todoId := c.Param("id") | ||
todo := &models.Todo{ID: todoId} | ||
err := database.Connection.Select(todo) | ||
if err != nil { | ||
log.Printf("Error while getting a single todo, Reason: %v\n", err) | ||
c.JSON(http.StatusNotFound, gin.H{ | ||
"status": http.StatusNotFound, | ||
"message": "Todo not found", | ||
}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{ | ||
"status": http.StatusOK, | ||
"message": "Single Todo", | ||
"data": todo, | ||
}) | ||
} | ||
|
||
func CreateTodo(c *gin.Context) { | ||
var todo models.Todo | ||
c.BindJSON(&todo) | ||
|
||
text := todo.Text | ||
id := guuid.New().String() | ||
|
||
insertError := database.Connection.Insert(&models.Todo{ | ||
ID: id, | ||
Text: text, | ||
Completed: false, | ||
CreatedAt: time.Now(), | ||
UpdatedAt: time.Now(), | ||
}) | ||
|
||
if insertError != nil { | ||
log.Printf("Error while inserting new todo into db, Reason: %v\n", insertError) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"status": http.StatusInternalServerError, | ||
"message": "Something went wrong", | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, gin.H{ | ||
"status": http.StatusCreated, | ||
"message": "Todo created Successfully", | ||
}) | ||
} | ||
|
||
func EditTodo(c *gin.Context) { | ||
todoId := c.Param("id") | ||
var todo models.Todo | ||
c.BindJSON(&todo) | ||
completed := todo.Completed | ||
_, err := database.Connection.Model(&models.Todo{}).Set("completed = ?", completed).Where("id = ?", todoId).Update() | ||
if err != nil { | ||
log.Printf("Error, Reason: %v\n", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"status": 500, | ||
"message": "Something went wrong", | ||
}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{ | ||
"status": 200, | ||
"message": "Todo Edited Successfully", | ||
}) | ||
} | ||
|
||
func DeleteTodo(c *gin.Context) { | ||
todoId := c.Param("id") | ||
todo := &models.Todo{ID: todoId} | ||
err := database.Connection.Delete(todo) | ||
if err != nil { | ||
log.Printf("Error while deleting a single todo, Reason: %v\n", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"status": http.StatusInternalServerError, | ||
"message": "Something went wrong", | ||
}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{ | ||
"status": http.StatusOK, | ||
"message": "Todo deleted successfully", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package controllers_old | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"harshsinghvi/golang-postgres-kubernetes/models" | ||
"net/http" | ||
) | ||
|
||
var TODOS = []models.Todo{ | ||
{ID: "1", Text: "Task 1", Completed: false}, | ||
{ID: "2", Text: "Task 2", Completed: false}, | ||
{ID: "3", Text: "Task 3", Completed: false}, | ||
} | ||
|
||
func GetTodos(c *gin.Context) { | ||
id := c.Query("id") | ||
// completed := c.Query("completed") == "true" // TODO: implement this filter | ||
|
||
if id == "" { | ||
c.IndentedJSON(http.StatusOK, TODOS) | ||
return | ||
} | ||
|
||
for _, a := range TODOS { | ||
if a.ID == id { | ||
c.IndentedJSON(http.StatusOK, a) | ||
return | ||
} | ||
} | ||
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "not found"}) | ||
} | ||
|
||
func PostTodos(c *gin.Context) { | ||
var newTodo models.Todo | ||
|
||
// Call BindJSON to bind the received JSON to | ||
if err := c.BindJSON(&newTodo); err != nil { | ||
return | ||
} | ||
|
||
if newTodo.Text == "" { | ||
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no text"}) | ||
return | ||
} | ||
|
||
for _, a := range TODOS { | ||
if a.ID == newTodo.ID { | ||
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Duplicate ID"}) | ||
return | ||
} | ||
} | ||
// Add the new album to the slice. | ||
TODOS = append(TODOS, newTodo) | ||
c.IndentedJSON(http.StatusCreated, newTodo) | ||
} | ||
|
||
func UpdateTodos(c *gin.Context) { | ||
id := c.Param("id") | ||
var updateTodo models.Todo | ||
|
||
// Call BindJSON to bind the received JSON to | ||
if err := c.BindJSON(&updateTodo); err != nil { | ||
return | ||
} | ||
|
||
if updateTodo.ID == "" { | ||
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no ID"}) | ||
return | ||
} | ||
|
||
for index, a := range TODOS { | ||
if a.ID == id { | ||
if updateTodo.Text != "" { | ||
TODOS[index].Text = updateTodo.Text | ||
} | ||
TODOS[index].Completed = updateTodo.Completed | ||
c.IndentedJSON(http.StatusOK, TODOS[index]) | ||
return | ||
} | ||
} | ||
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "invalid ID"}) | ||
} | ||
|
||
func DeleteTodos(c *gin.Context) { | ||
id := c.Param("id") | ||
|
||
for index, a := range TODOS { | ||
if a.ID == id { | ||
TODOS = append(TODOS[:index], TODOS[index+1:]...) | ||
c.IndentedJSON(http.StatusOK, TODOS) | ||
return | ||
} | ||
} | ||
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no ID"}) | ||
} |
Oops, something went wrong.