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.
refactor: implement clean arch on get items
Signed-off-by: Ismael Ibuan <[email protected]>
- Loading branch information
Showing
20 changed files
with
619 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
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,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) | ||
} |
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,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,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 | ||
) |
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.