Skip to content

Commit

Permalink
refactor: implement clean arch on get items
Browse files Browse the repository at this point in the history
Signed-off-by: Ismael Ibuan <[email protected]>
  • Loading branch information
iibuan committed Sep 12, 2024
1 parent fdda339 commit 95e13dd
Show file tree
Hide file tree
Showing 20 changed files with 619 additions and 205 deletions.
11 changes: 11 additions & 0 deletions src/goapp/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

type Key string

type Config struct {
DatabaseConnectionString string
}

type ConfigManager interface {
GetDatabaseConnectionString() string
}
26 changes: 26 additions & 0 deletions src/goapp/config/env-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package config

import (
"log"
"os"

"github.com/joho/godotenv"
)

type envConfigManager struct {
*Config
}

func NewEnvConfigManager() *envConfigManager {
// Set environment variables
err := godotenv.Load()
if err != nil {
log.Fatal(err.Error())
}

return &envConfigManager{}
}

func (ecm *envConfigManager) GetDatabaseConnectionString() string {
return os.Getenv("APPROVALSYSTEMDB_CONNECTION_STRING")
}
55 changes: 55 additions & 0 deletions src/goapp/controller/item/implement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package item

import (
"encoding/json"
"fmt"
"main/pkg/session"
service "main/service/item"
"net/http"

"github.com/gorilla/mux"
)

type itemController struct {
itemService service.ItemService
}

func NewItemController(itemService service.ItemService) ItemController {
return &itemController{
itemService: itemService,
}
}

// GetItems is a function to get all items
func (c *itemController) GetItems(w http.ResponseWriter, r *http.Request) {
// Get all items
vars := mux.Vars(r)

params := r.URL.Query()

session, err := session.Store.Get(r, "auth-session")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

var profile map[string]interface{}
u := session.Values["profile"]
profile, ok := u.(map[string]interface{})
if !ok {
http.Error(w, "Failed to get user info", http.StatusInternalServerError)
return
}
user := fmt.Sprintf("%s", profile["preferred_username"])

result, err := c.itemService.GetAll(vars, params, user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Return the result
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(result)
}
7 changes: 7 additions & 0 deletions src/goapp/controller/item/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package item

import "net/http"

type ItemController interface {
GetItems(w http.ResponseWriter, r *http.Request)
}
24 changes: 24 additions & 0 deletions src/goapp/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"main/config"
"main/repository"
"main/router"

repositoryItem "main/repository/item"

serviceItem "main/service/item"

controllerItem "main/controller/item"
)

var (
configManager config.ConfigManager = config.NewEnvConfigManager()
database repository.Database = repository.NewDatabase(configManager)

itemRepository repositoryItem.ItemRepository = repositoryItem.NewItemRepository(database)
itemService serviceItem.ItemService = serviceItem.NewItemService(itemRepository)
itemController controllerItem.ItemController = controllerItem.NewItemController(itemService)

httpRouter router.Router = router.NewMuxRouter()
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package model

type TypRequestApproval struct {
ApplicationId string `json:"applicationId"`
Expand Down
2 changes: 1 addition & 1 deletion src/goapp/models/data.go → src/goapp/model/data.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package model

type TypPageData struct {
Header interface{}
Expand Down
45 changes: 45 additions & 0 deletions src/goapp/model/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package model

type Item struct {
Application string `json:"application"`
ApproverRemarks string `json:"approverRemarks"`
Body string `json:"body"`
Created string `json:"created"`
DateResponded string `json:"dateResponded"`
DateSent string `json:"dateSent"`
IsApproved bool `json:"isApproved"`
Module string `json:"module"`
Subject string `json:"subject"`
ApproveText string `json:"approveText"`
RejectText string `json:"rejectText"`
ApproveUrl string `json:"approveUrl"`
RejectUrl string `json:"rejectUrl"`
AllowReassign bool `json:"allowReassign"`
AllowReassignUrl string `json:"allowReassignUrl"`
RespondedBy string `json:"respondedBy"`
Approvers []string `json:"approvers"`
RequestedBy string `json:"requestedBy"`
}

type Response struct {
Data []Item `json:"data"`
Total int `json:"total"`
}

type ItemType int8

const (
RequestItem ItemType = iota
ApprovalItem
AllType
)

type ItemStatus int8

const (
Pending ItemStatus = iota
Approved
Rejected
Closed // Disapproved, Approved
All // Disapproved, Approved, Pending
)
16 changes: 8 additions & 8 deletions src/goapp/pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package template
import (
"fmt"
"html/template"
"main/models"
"main/model"
session "main/pkg/session"
"net/http"
"os"
Expand All @@ -28,21 +28,21 @@ func UseTemplate(w *http.ResponseWriter, r *http.Request, page string, pageData
}

// Data on master page
var menu []models.TypMenu
menu = append(menu, models.TypMenu{Name: "My Requests", Url: "/", IconPath: "/public/icons/projects.svg"})
menu = append(menu, models.TypMenu{Name: "My Approvals", Url: "/myapprovals", IconPath: "/public/icons/approvals.svg"})
masterPageData := models.TypHeaders{Menu: menu, Page: getUrlPath(r.URL.Path)}
var menu []model.TypMenu
menu = append(menu, model.TypMenu{Name: "My Requests", Url: "/", IconPath: "/public/icons/projects.svg"})
menu = append(menu, model.TypMenu{Name: "My Approvals", Url: "/myapprovals", IconPath: "/public/icons/approvals.svg"})
masterPageData := model.TypHeaders{Menu: menu, Page: getUrlPath(r.URL.Path)}

//Footers
var footers []models.Footer
var footers []model.Footer
footerString := os.Getenv("LINK_FOOTERS")
res := strings.Split(footerString, ";")
for _, footer := range res {
f := strings.Split(footer, ">")
footers = append(footers, models.Footer{Text: f[0], Url: f[1]})
footers = append(footers, model.Footer{Text: f[0], Url: f[1]})
}

data := models.TypPageData{
data := model.TypPageData{
Header: masterPageData,
Profile: profile,
Content: pageData,
Expand Down
Loading

0 comments on commit 95e13dd

Please sign in to comment.