Skip to content

Commit

Permalink
create item options model
Browse files Browse the repository at this point in the history
Signed-off-by: Ismael Ibuan <[email protected]>
  • Loading branch information
iibuan committed Sep 13, 2024
1 parent bc7ce6b commit 3d49bff
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 164 deletions.
55 changes: 0 additions & 55 deletions src/goapp/controller/item/implement.go

This file was deleted.

File renamed without changes.
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)
}
11 changes: 11 additions & 0 deletions src/goapp/model/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ type Item struct {
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"`
Expand Down
10 changes: 0 additions & 10 deletions src/goapp/repository/item/interface.go

This file was deleted.

10 changes: 10 additions & 0 deletions src/goapp/repository/item/item-repository-interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package item

import (
"main/model"
)

type ItemRepository interface {
GetItemsBy(itemOptions model.ItemOptions) ([]model.Item, error)
GetTotalItemsBy(itemOptions model.ItemOptions) (int, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ func NewItemRepository(db repository.Database) ItemRepository {
}
}

func (r *itemRepository) GetItemsBy(itemType model.ItemType, itemStatus model.ItemStatus, requestType, organization, user, search string, offset, filter int) ([]model.Item, error) {
func (r *itemRepository) GetItemsBy(itemOptions model.ItemOptions) ([]model.Item, error) {
var params []interface{}

if itemType != model.AllType {
params = append(params, sql.Named("ItemType", itemType))
params = append(params, sql.Named("User", user))
if model.ItemType(itemOptions.ItemType) != model.AllType {
params = append(params, sql.Named("ItemType", itemOptions.ItemType))
params = append(params, sql.Named("User", itemOptions.User))
}

if requestType != "" {
params = append(params, sql.Named("RequestType", requestType))
if itemOptions.RequestType != "" {
params = append(params, sql.Named("RequestType", itemOptions.RequestType))
}

if organization != "" {
params = append(params, sql.Named("Organization", organization))
if itemOptions.Organization != "" {
params = append(params, sql.Named("Organization", itemOptions.Organization))
}

params = append(params, sql.Named("IsApproved", itemStatus))
params = append(params, sql.Named("Search", search))
params = append(params, sql.Named("Offset", offset))
params = append(params, sql.Named("Filter", filter))
params = append(params, sql.Named("IsApproved", itemOptions.ItemStatus))
params = append(params, sql.Named("Search", itemOptions.Search))
params = append(params, sql.Named("Offset", itemOptions.Offset))
params = append(params, sql.Named("Filter", itemOptions.Filter))

resList, err := r.Query("PR_Items_Select", params...)
if err != nil {
Expand Down Expand Up @@ -117,24 +117,24 @@ func (r *itemRepository) GetItemsBy(itemType model.ItemType, itemStatus model.It
return items, nil
}

func (r *itemRepository) GetTotalItemsBy(itemType model.ItemType, itemStatus model.ItemStatus, requestType, organization, user, search string) (int, error) {
func (r *itemRepository) GetTotalItemsBy(itemOptions model.ItemOptions) (int, error) {
var params []interface{}

if itemType != model.AllType {
params = append(params, sql.Named("ItemType", itemType))
params = append(params, sql.Named("User", user))
if model.ItemType(itemOptions.ItemType) != model.AllType {
params = append(params, sql.Named("ItemType", itemOptions.ItemType))
params = append(params, sql.Named("User", itemOptions.User))
}

if requestType != "" {
params = append(params, sql.Named("RequestType", requestType))
if itemOptions.RequestType != "" {
params = append(params, sql.Named("RequestType", itemOptions.RequestType))
}

if organization != "" {
params = append(params, sql.Named("Organization", organization))
if itemOptions.Organization != "" {
params = append(params, sql.Named("Organization", itemOptions.Organization))
}

params = append(params, sql.Named("IsApproved", itemStatus))
params = append(params, sql.Named("Search", search))
params = append(params, sql.Named("IsApproved", itemOptions.ItemStatus))
params = append(params, sql.Named("Search", itemOptions.Search))

rowTotal, err := r.Query("PR_Items_Total", params...)
if err != nil {
Expand Down
67 changes: 0 additions & 67 deletions src/goapp/service/item/implement.go

This file was deleted.

10 changes: 0 additions & 10 deletions src/goapp/service/item/interface.go

This file was deleted.

9 changes: 9 additions & 0 deletions src/goapp/service/item/item-service-interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package item

import (
"main/model"
)

type ItemService interface {
GetAll(itemOptions model.ItemOptions) (model.Response, error)
}
Loading

0 comments on commit 3d49bff

Please sign in to comment.