Skip to content

Commit

Permalink
my request refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Ismael Ibuan <[email protected]>
  • Loading branch information
iibuan committed Oct 7, 2024
1 parent 00a18ae commit c5607be
Showing 26 changed files with 337 additions and 130 deletions.
3 changes: 2 additions & 1 deletion .bicep/webapp/parameters.json
Original file line number Diff line number Diff line change
@@ -41,7 +41,8 @@
"EMAIL_CLIENT_SECRET" : "",
"EMAIL_USER_ID" : "",
"LINK_FOOTERS": "",
"ORGANIZATION_NAME": ""
"ORGANIZATION_NAME": "",
"COMMUNITY_PORTAL_APP_ID": ""
}
}
}
1 change: 1 addition & 0 deletions .github/workflows/setup-appservice-resource.yml
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ jobs:
parameters.appServiceSettings.value.DOCKER_REGISTRY_SERVER_USERNAME : ${{ secrets.CONTAINER_REGISTRY_SERVER_USERNAME }}
parameters.appServiceSettings.value.DOCKER_REGISTRY_SERVER_PASSWORD : ${{ secrets.CONTAINER_REGISTRY_SERVER_PASSWORD }}
parameters.appServiceSettings.value.APPROVALSYSTEMDB_CONNECTION_STRING : ${{ secrets.DATABASE_CONNECTION_STRING }}
parameters.appServiceSettings.value.COMMUNITY_PORTAL_APP_ID : ${{ vars.COMMUNITY_PORTAL_APP_ID }}

- name: Deploy App Service Plan and Web App
uses: azure/arm-deploy@v1
3 changes: 2 additions & 1 deletion src/goapp/.env.template
Original file line number Diff line number Diff line change
@@ -16,4 +16,5 @@ EMAIL_CLIENT_SECRET=<Client Secret>
EMAIL_ENABLED=<Email enabled. default: false>
EMAIL_USER_ID=<Email user id>
LINK_FOOTERS=""
ORGANIZATION_NAME=""
ORGANIZATION_NAME=""
COMMUNITY_PORTAL_APP_ID=""
3 changes: 3 additions & 0 deletions src/goapp/config/config.go
Original file line number Diff line number Diff line change
@@ -17,4 +17,7 @@ type ConfigManager interface {
GetTenantID() string
GetClientID() string
GetClientSecret() string
GetLinkFooters() string
GetOrganizationName() string
GetCommunityPortalAppId() string
}
12 changes: 12 additions & 0 deletions src/goapp/config/env-config.go
Original file line number Diff line number Diff line change
@@ -66,3 +66,15 @@ func (ecm *envConfigManager) GetClientID() string {
func (ecm *envConfigManager) GetClientSecret() string {
return os.Getenv("CLIENT_SECRET")
}

func (ecm *envConfigManager) GetLinkFooters() string {
return os.Getenv("LINK_FOOTERS")
}

func (ecm *envConfigManager) GetOrganizationName() string {
return os.Getenv("ORGANIZATION_NAME")
}

func (ecm *envConfigManager) GetCommunityPortalAppId() string {
return os.Getenv("COMMUNITY_PORTAL_APP_ID")
}
10 changes: 10 additions & 0 deletions src/goapp/controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"main/config"
cApplicationModule "main/controller/app-module"
cItem "main/controller/item"
cUser "main/controller/user"
@@ -9,6 +10,7 @@ import (

type Controller struct {
Item cItem.ItemController
ItemPage cItem.ItemPageController
ApplicationModule cApplicationModule.ApplicationModuleController
User cUser.UserController
}
@@ -42,3 +44,11 @@ func NewUserController(svc *service.Service) ControllerOptionFunc {
c.User = cUser.NewUserController(svc)
}
}

// PAGES

func NewItemPageController(svc *service.Service, conf config.ConfigManager) ControllerOptionFunc {
return func(c *Controller) {
c.ItemPage = cItem.NewItemPageController(svc, conf)
}
}
4 changes: 4 additions & 0 deletions src/goapp/controller/item/item-controller-interface.go
Original file line number Diff line number Diff line change
@@ -8,3 +8,7 @@ type ItemController interface {
ProcessResponse(w http.ResponseWriter, r *http.Request)
ReassignItem(w http.ResponseWriter, r *http.Request)
}

type ItemPageController interface {
MyRequests(w http.ResponseWriter, r *http.Request)
}
62 changes: 62 additions & 0 deletions src/goapp/controller/item/item-page-controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package item

import (
"encoding/json"
"fmt"
"main/config"
"main/model"
"main/pkg/session"
"main/service"
"net/http"
)

type itemPageController struct {
*service.Service
CommunityPortalAppId string
}

func NewItemPageController(s *service.Service, conf config.ConfigManager) ItemPageController {
return &itemPageController{
Service: s,
CommunityPortalAppId: conf.GetCommunityPortalAppId(),
}
}

func (c *itemPageController) MyRequests(w http.ResponseWriter, r *http.Request) {
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, "Error getting user data", http.StatusInternalServerError)
return
}
user := model.AzureUser{
Name: profile["name"].(string),
Email: profile["preferred_username"].(string),
}

application, err := c.Service.Application.GetApplicationById(c.CommunityPortalAppId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

b, err := json.Marshal(application)
if err != nil {
fmt.Println(err)
return
}

t, d := c.Service.Template.UseTemplate("myrequests", r.URL.Path, user, string(b))

err = t.Execute(w, d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
103 changes: 12 additions & 91 deletions src/goapp/infrastructure/database/database.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import (

type Database struct {
connString string
db *sql.DB
}

func NewDatabase(config config.ConfigManager) Database {
@@ -22,62 +21,54 @@ func NewDatabase(config config.ConfigManager) Database {
}
}

func (d *Database) Connect() error {
func (d *Database) Connect() (*sql.DB, error) {
conn, err := sql.Open("sqlserver", d.connString)
if err != nil {
return err
return nil, err
}

d.db = conn
return nil
}

func (d *Database) Disconnect() error {
if d.db != nil {
return d.db.Close()
}
return nil
return conn, nil
}

func (d *Database) Query(query string, args ...any) (*sql.Rows, error) {
err := d.Connect()
con, err := d.Connect()
if err != nil {
return nil, err
}
defer d.Disconnect()
defer con.Close()

ctx := context.Background()
rows, err := d.db.QueryContext(ctx, query, args...)
rows, err := con.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
return rows, nil
}

func (d *Database) QueryRow(query string, args ...any) (*sql.Row, error) {
err := d.Connect()
con, err := d.Connect()
if err != nil {
return nil, err
}
defer d.Disconnect()
defer con.Close()

ctx := context.Background()
row := d.db.QueryRowContext(ctx, query, args...)
row := con.QueryRowContext(ctx, query, args...)
if row == nil {
err = fmt.Errorf("QueryRowContext returned nil")
}
return row, nil
}

func (d *Database) Execute(query string, args ...any) error {
err := d.Connect()
con, err := d.Connect()
if err != nil {
return err
}
defer d.Disconnect()
defer con.Close()

ctx := context.Background()
_, err = d.db.ExecContext(ctx, query, args...)
_, err = con.ExecContext(ctx, query, args...)
if err != nil {
return err
}
@@ -112,73 +103,3 @@ func (d *Database) RowsToMap(rows *sql.Rows) ([]map[string]interface{}, error) {

return results, nil
}

// OLD
func (d *Database) Close() error {
if d.db != nil {
return d.db.Close()
}
return nil
}

// OLD
func (d *Database) ExecuteStoredProcedure(procedure string, params map[string]interface{}) (sql.Result, error) {
var args []interface{}

for i, v := range params {
args = append(args, sql.Named(i, v))
}

ctx := context.Background()
result, err := d.db.ExecContext(ctx, procedure, args...)

if err != nil {
return nil, err
}

return result, nil
}

// OLD
func (d *Database) ExecuteStoredProcedureWithResult(procedure string, params map[string]interface{}) ([]map[string]interface{}, error) {
var args []interface{}

ctx := context.Background()

for i, v := range params {
args = append(args, sql.Named(i, v))
}

rows, err := d.db.QueryContext(ctx, procedure, args...)
if err != nil {
return nil, err
}

defer rows.Close()

columns, err := rows.Columns()
if err != nil {
return nil, err
}

var results []map[string]interface{}

for rows.Next() {
values := make([]interface{}, len(columns))
pointers := make([]interface{}, len(columns))
for i := range values {
pointers[i] = &values[i]
}
err := rows.Scan(pointers...)
if err != nil {
return nil, err
}
result := make(map[string]interface{})
for i, val := range values {
result[columns[i]] = val
}
results = append(results, result)
}

return results, nil
}
10 changes: 7 additions & 3 deletions src/goapp/init.go
Original file line number Diff line number Diff line change
@@ -15,23 +15,27 @@ var (
db database.Database = database.NewDatabase(conf)

repo = r.NewRepository(
r.NewApplicationModule(db),
r.NewItem(db),
r.NewApprovalRequestApprover(db),
r.NewApplication(&db),
r.NewApplicationModule(&db),
r.NewItem(&db),
r.NewApprovalRequestApprover(&db),
)

svc = s.NewService(
s.NewApplicationService(repo),
s.NewApplicationModuleService(repo),
s.NewItemService(repo, conf),
s.NewEmailService(conf),
s.NewApprovalRequestApproverService(repo),
s.NewMsGraphService(conf),
s.NewTemplateService(conf),
)

ctrl = c.NewController(
c.NewItemController(svc),
c.NewApplicationModuleController(svc),
c.NewUserController(svc),
c.NewItemPageController(svc, conf),
)

httpRouter router.Router = router.NewMuxRouter()
8 changes: 8 additions & 0 deletions src/goapp/model/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package model

type Application struct {
Id string `json:"id"`
Name string `json:"name"`
ExportUrl string `json:"exportUrl"`
OrganizationTypeUrl string `json:"organizationTypeUrl"`
}
16 changes: 8 additions & 8 deletions src/goapp/model/data.go → src/goapp/model/template.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package model

type TypPageData struct {
Header interface{}
Profile interface{}
type MasterPageData struct {
Header Headers
Profile AzureUser
Content interface{}
Footers []Footer
OrganizationName string
@@ -13,20 +13,20 @@ type Footer struct {
Url string
}

type TypHeaders struct {
Menu []TypMenu
ExternalLinks []TypMenu
type Headers struct {
Menu []Menu
ExternalLinks []Menu
Page string
}

type TypMenu struct {
type Menu struct {
Name string
Url string
IconPath string
UrlPath string
}

type TypAzureUser struct {
type AzureUser struct {
Name string `json:"name"`
Email string `json:"preferred_username"`
}
Loading

0 comments on commit c5607be

Please sign in to comment.