Skip to content

Commit

Permalink
Merge pull request #97 from Avanade/92-refactor
Browse files Browse the repository at this point in the history
92 refactor
  • Loading branch information
jerricotandelacruz authored Sep 13, 2024
2 parents 30330d4 + 3d49bff commit 0bfdab4
Show file tree
Hide file tree
Showing 21 changed files with 655 additions and 205 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker-build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ jobs:
username: ${{ secrets.CONTAINER_REGISTRY_SERVER_USERNAME }}
password: ${{ secrets.CONTAINER_REGISTRY_SERVER_PASSWORD }}
- run: |
touch .env
docker build . -t ${{ secrets.CONTAINER_REGISTRY_SERVER }}/${{vars.APPSERVICE_NAME}}:${{ github.sha }} -t ${{ secrets.CONTAINER_REGISTRY_SERVER }}/${{vars.APPSERVICE_NAME}}:latest
docker push ${{ secrets.CONTAINER_REGISTRY_SERVER }}/${{vars.APPSERVICE_NAME}}
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")
}
7 changes: 7 additions & 0 deletions src/goapp/controller/item/item-controller-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)
}
110 changes: 110 additions & 0 deletions src/goapp/controller/item/item-controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package item

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

"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
var itemOptions model.ItemOptions

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"])
itemOptions.User = user

vars := mux.Vars(r)

itemOptions.ItemType, err = strconv.ParseInt(vars["type"], 10, 8)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

itemOptions.ItemStatus, err = strconv.ParseInt(vars["status"], 10, 8)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

params := r.URL.Query()

if params.Has("offset") {
itemOptions.Offset, err = strconv.Atoi(params["offset"][0])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
itemOptions.Offset = 0
}

if params.Has("filter") {
itemOptions.Filter, err = strconv.Atoi(params["filter"][0])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
itemOptions.Filter = 10
}

if params.Has("search") {
itemOptions.Search = params["search"][0]
} else {
itemOptions.Search = ""
}

if params.Has("requestType") {
itemOptions.RequestType = params["requestType"][0]
} else {
itemOptions.RequestType = ""
}

if params.Has("organization") {
itemOptions.Organization = params["organization"][0]
} else {
itemOptions.Organization = ""
}

result, err := c.itemService.GetAll(itemOptions)
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)
}
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
56 changes: 56 additions & 0 deletions src/goapp/model/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 ItemOptions struct {
ItemType int64
ItemStatus int64
Offset int
Filter int
Search string
RequestType string
Organization string
User string
}

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 0bfdab4

Please sign in to comment.