Skip to content

Commit

Permalink
Added Filters to GetAll Todos, Removed Todos comments, added port config
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsinghvi committed Nov 21, 2023
1 parent ca57961 commit 24e5fc9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
6 changes: 6 additions & 0 deletions controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func GetAllTodos(c *gin.Context) {
var todos []models.Todo
var searchString = c.Query("search")
var pageString = c.Query("page")
var completedString = c.Query("completed")
pag.ParseString(pageString)

querry := database.Connection.Model(&todos).Order("created_at DESC").Where("deleted = ?", false).Where("user_id = ?", userId)
Expand All @@ -30,6 +31,11 @@ func GetAllTodos(c *gin.Context) {
querry = querry.Where(fmt.Sprintf("text like '%%%s%%'", searchString))
}

if completedString != "" {
var completed = completedString == "true" || completedString == "True"
querry = querry.Where("completed = ?", completed)
}

if pag.TotalRecords, err = querry.Count(); err != nil {
utils.InternalServerError(c, "Error while getting all todos, Reason:", err)
return
Expand Down
1 change: 0 additions & 1 deletion controllers_old/controllers_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var TODOS = []models.Todo{

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)
Expand Down
1 change: 0 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func createTablesAndIndexes(tableName string, model interface{}, indexFields str
func CreateTables() {
createTablesAndIndexes("todos", &models.Todo{}, "completed, created_at, deleted")
createTablesAndIndexes("access_tokens", &models.AccessToken{}, "created_at, token, expiry, user_id, deleted")
// TODO: fails
createTablesAndIndexes("access_logs", &models.AccessLog{}, "token_id, path, method, response_time, status_code, server_hostname, created_at, bill_id, billed, deleted")
createTablesAndIndexes("users", &models.User{}, "email, created_at, deleted")
createTablesAndIndexes("bills", &models.Bill{}, "sattled, user_id, created_at, deleted")
Expand Down
38 changes: 19 additions & 19 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
version: '3'
services:
app:
container_name: go-todo-api
build: .
ports:
- 8080:8080
restart: on-failure
depends_on:
- postgres
networks:
- fullstack
environment:
- DB_HOST=postgres
- DB_DRIVER=${DB_DRIVER}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_NAME=${DB_NAME}
- DB_PORT=${DB_PORT}
- POSTGRES_URL=${POSTGRES_URL}
- PORT=${PORT}
# app:
# container_name: go-todo-api
# build: .
# ports:
# - 8080:8080
# restart: on-failure
# depends_on:
# - postgres
# networks:
# - fullstack
# environment:
# - DB_HOST=postgres
# - DB_DRIVER=${DB_DRIVER}
# - DB_USER=${DB_USER}
# - DB_PASSWORD=${DB_PASSWORD}
# - DB_NAME=${DB_NAME}
# - DB_PORT=${DB_PORT}
# - POSTGRES_URL=${POSTGRES_URL}
# - PORT=${PORT}

postgres:
image: postgres:latest
Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"harshsinghvi/golang-postgres-kubernetes/controllers"
"harshsinghvi/golang-postgres-kubernetes/controllers_old"
"harshsinghvi/golang-postgres-kubernetes/database"
"harshsinghvi/golang-postgres-kubernetes/middlewares"
"harshsinghvi/golang-postgres-kubernetes/models/roles"
"harshsinghvi/golang-postgres-kubernetes/utils"
"log"
"net/http"
)
Expand All @@ -30,6 +32,7 @@ func init() {
}
}
func main() {
PORT := utils.GetEnv("PORT", "8080")

database.Connect()
database.CreateTables()
Expand Down Expand Up @@ -71,10 +74,6 @@ func main() {
v2.GET("/user/bill", middlewares.AIO(roles.Roles{roles.Any}, middlewares.Config{"billing-disable": true}), controllers.GetBills)
v2.DELETE("/user/token/:token-id", middlewares.AIO(roles.Roles{roles.Write}), controllers.DeleteToken)

// TODO Soft delete
// Delete Token
// delete user

// Business Logic
v2.GET("/todo/", middlewares.AIO(roles.Roles{roles.Admin, roles.Read}), controllers.GetAllTodos)
v2.GET("/todo/:id", middlewares.AIO(roles.Roles{roles.Admin, roles.Read, roles.ReadOne}), controllers.GetSingleTodo)
Expand All @@ -86,5 +85,6 @@ func main() {

router.GET("/health", healthHandler)
router.GET("/readiness", readinessHandler)
router.Run(":8080")

router.Run(fmt.Sprintf(":%s", PORT))
}
2 changes: 1 addition & 1 deletion models/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type AccessToken struct {

type AccessLog struct {
ID string `json:"id"`
TokenID string `json:"token_id"` // TODO: Change this to TokenID
TokenID string `json:"token_id"`
Path string `json:"path"`
ClientIP string `json:"client_ip"`
Method string `json:"method"`
Expand Down

0 comments on commit 24e5fc9

Please sign in to comment.