generated from Avanade/avanade-template
-
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.
Merge pull request #97 from Avanade/92-refactor
92 refactor
- Loading branch information
Showing
21 changed files
with
655 additions
and
205 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
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,11 @@ | ||
package config | ||
|
||
type Key string | ||
|
||
type Config struct { | ||
DatabaseConnectionString string | ||
} | ||
|
||
type ConfigManager interface { | ||
GetDatabaseConnectionString() string | ||
} |
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,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") | ||
} |
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,7 @@ | ||
package item | ||
|
||
import "net/http" | ||
|
||
type ItemController interface { | ||
GetItems(w http.ResponseWriter, r *http.Request) | ||
} |
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,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) | ||
} |
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,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() | ||
) |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package models | ||
package model | ||
|
||
type TypRequestApproval struct { | ||
ApplicationId string `json:"applicationId"` | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package models | ||
package model | ||
|
||
type TypPageData struct { | ||
Header interface{} | ||
|
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,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 | ||
) |
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
Oops, something went wrong.