initial commit
This commit is contained in:
77
internal/api/api.go
Normal file
77
internal/api/api.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.brettb.xyz/goinv/server/internal/storage"
|
||||
"git.brettb.xyz/goinv/server/internal/types"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
const API_VERSION_MAJOR = 0
|
||||
const API_VERSION_MINOR = 0
|
||||
const API_VERSION_PATCH = 1
|
||||
|
||||
var API_VERSION = types.APIVersion{
|
||||
Major: API_VERSION_MAJOR,
|
||||
Minor: API_VERSION_MINOR,
|
||||
Patch: API_VERSION_PATCH,
|
||||
}
|
||||
|
||||
type APIFunc func(w http.ResponseWriter, r *http.Request) error
|
||||
|
||||
type APIServer struct {
|
||||
listenAddr string
|
||||
db *storage.DataStore
|
||||
}
|
||||
|
||||
func NewAPIServer(database *storage.DataStore, listenAddr string) *APIServer {
|
||||
return &APIServer{
|
||||
listenAddr: listenAddr,
|
||||
db: database,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *APIServer) Run() {
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(render.SetContentType(render.ContentTypeJSON))
|
||||
|
||||
s.registerRoutes(r)
|
||||
|
||||
log.Printf("API Server listening on %s", s.listenAddr)
|
||||
|
||||
err := http.ListenAndServe(s.listenAddr, r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *APIServer) registerRoutes(r *chi.Mux) {
|
||||
r.Get("/", makeHandler(s.handleIndex))
|
||||
|
||||
r.Route("/assets", s.setupAssetRoutes())
|
||||
r.Route("/shelves", s.setupShelfRoutes())
|
||||
}
|
||||
|
||||
func (s *APIServer) handleIndex(w http.ResponseWriter, r *http.Request) error {
|
||||
return writeJSON(w, http.StatusOK, types.IndexResponse{Version: API_VERSION})
|
||||
}
|
||||
|
||||
func makeHandler(f APIFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := f(w, r); err != nil {
|
||||
render.Render(w, r, errBadRequest(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, s int, v any) error {
|
||||
w.WriteHeader(s)
|
||||
return json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
124
internal/api/assets.go
Normal file
124
internal/api/assets.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.brettb.xyz/goinv/server/internal/types"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
func (s *APIServer) setupAssetRoutes() func(r chi.Router) {
|
||||
return func(r chi.Router) {
|
||||
r.Get("/", makeHandler(s.getAssets))
|
||||
r.Post("/", makeHandler(s.createAsset))
|
||||
|
||||
r.Route("/{assetID}", func(r chi.Router) {
|
||||
r.Use(s.AssetCtx)
|
||||
r.Get("/", makeHandler(s.getAsset))
|
||||
r.Delete("/", makeHandler(s.deleteAsset))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *APIServer) AssetCtx(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assetIdStr := chi.URLParam(r, "assetID")
|
||||
assetId, err := strconv.Atoi(assetIdStr)
|
||||
if err != nil {
|
||||
render.Render(w, r, errNotFound)
|
||||
return
|
||||
}
|
||||
asset, err := s.db.GetAssetByID(uint64(assetId))
|
||||
if err != nil {
|
||||
render.Render(w, r, errNotFound)
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), "asset", asset)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) getAssets(w http.ResponseWriter, r *http.Request) error {
|
||||
assets, err := s.db.GetAssets(0, 50) // TODO: Proper Pagination
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
total, err := s.db.TotalAssets()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.MultipleAssetsResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
Assets: assets,
|
||||
Total: total,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) getAsset(w http.ResponseWriter, r *http.Request) error {
|
||||
ctx := r.Context()
|
||||
asset, ok := ctx.Value("asset").(*types.Asset)
|
||||
if !ok {
|
||||
return render.Render(w, r, errUnprocessable)
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.AssetResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
Asset: asset,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) deleteAsset(w http.ResponseWriter, r *http.Request) error {
|
||||
ctx := r.Context()
|
||||
asset, ok := ctx.Value("asset").(*types.Asset)
|
||||
if !ok {
|
||||
return render.Render(w, r, errUnprocessable)
|
||||
}
|
||||
|
||||
if ok, _ := s.db.DeleteAssetByID(asset.ID); !ok {
|
||||
return render.Render(w, r, errUnprocessable)
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.AssetResponse{Response: &types.Response{HTTPStatusCode: http.StatusOK}, Asset: asset})
|
||||
}
|
||||
|
||||
func (s *APIServer) createAsset(w http.ResponseWriter, r *http.Request) error {
|
||||
data := &types.CreateAssetRequest{}
|
||||
if err := render.Bind(r, data); err != nil {
|
||||
log.Printf("ERR: %v\n", err)
|
||||
return render.Render(w, r, errBadRequest(err))
|
||||
}
|
||||
|
||||
asset := &types.Asset{
|
||||
Name: data.Name,
|
||||
Quantity: data.Quantity,
|
||||
Length: data.Length,
|
||||
Manufacturer: data.Manufacturer,
|
||||
ModelName: data.ModelName,
|
||||
Price: data.Price,
|
||||
Comments: data.Comments,
|
||||
ShelfLocationID: data.ShelfLocationID,
|
||||
CategoryID: data.CategoryID,
|
||||
}
|
||||
|
||||
err := s.db.CreateAsset(asset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.AssetResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
Asset: asset,
|
||||
})
|
||||
}
|
||||
96
internal/api/shelves.go
Normal file
96
internal/api/shelves.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.brettb.xyz/goinv/server/internal/types"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
func (s *APIServer) setupShelfRoutes() func(chi.Router) {
|
||||
return func(r chi.Router) {
|
||||
r.Get("/", makeHandler(s.getShelves))
|
||||
//r.Post("/", makeHandler(s.createShelf))
|
||||
|
||||
r.Route("/{shelfID}", func(r chi.Router) {
|
||||
r.Use(s.ShelfCtx)
|
||||
r.Get("/", makeHandler(s.getShelf))
|
||||
r.Delete("/", makeHandler(s.deleteShelf))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *APIServer) ShelfCtx(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
shelfIdStr := chi.URLParam(r, "shelfID")
|
||||
shelfId, err := strconv.ParseUint(shelfIdStr, 10, 64)
|
||||
if err != nil {
|
||||
render.Render(w, r, errNotFound)
|
||||
return
|
||||
}
|
||||
shelf, err := s.db.GetShelfByID(shelfId)
|
||||
if err != nil {
|
||||
render.Render(w, r, errNotFound)
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), "shelf", shelf)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) getShelves(w http.ResponseWriter, r *http.Request) error {
|
||||
shelves, err := s.db.GetShelves(0, 10)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
total, err := s.db.TotalShelves()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.MultipleShelfResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
ShelfLocations: shelves,
|
||||
Total: total,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) getShelf(w http.ResponseWriter, r *http.Request) error {
|
||||
ctx := r.Context()
|
||||
shelf, ok := ctx.Value("shelf").(*types.ShelfLocation)
|
||||
if !ok {
|
||||
return render.Render(w, r, errUnprocessable)
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.ShelfResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
ShelfLocation: shelf,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) deleteShelf(w http.ResponseWriter, r *http.Request) error {
|
||||
ctx := r.Context()
|
||||
shelf, ok := ctx.Value("shelf").(*types.ShelfLocation)
|
||||
if !ok {
|
||||
return render.Render(w, r, errUnprocessable)
|
||||
}
|
||||
|
||||
if ok, _ := s.db.DeleteShelfByID(shelf.ID); !ok {
|
||||
return render.Render(w, r, errUnprocessable)
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.ShelfResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
ShelfLocation: shelf,
|
||||
})
|
||||
}
|
||||
52
internal/api/types.go
Normal file
52
internal/api/types.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.brettb.xyz/goinv/server/internal/types"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
var errNotFound = &types.APIError{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusNotFound,
|
||||
},
|
||||
Messages: []string{"resource not found"},
|
||||
}
|
||||
|
||||
var errUnprocessable = &types.APIError{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusUnprocessableEntity,
|
||||
},
|
||||
Messages: []string{"unable to process"},
|
||||
}
|
||||
|
||||
func errBadRequest(err error) render.Renderer {
|
||||
return &types.APIError{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusBadRequest,
|
||||
},
|
||||
Err: err,
|
||||
Messages: []string{"bad request"},
|
||||
}
|
||||
}
|
||||
|
||||
func errRender(err error) render.Renderer {
|
||||
return &types.APIError{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusUnprocessableEntity,
|
||||
},
|
||||
Err: err,
|
||||
Messages: []string{"error rendering response"},
|
||||
}
|
||||
}
|
||||
|
||||
func errUnauthorized(err error) render.Renderer {
|
||||
return &types.APIError{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusUnauthorized,
|
||||
},
|
||||
Err: err,
|
||||
Messages: []string{"unauthorized"},
|
||||
}
|
||||
}
|
||||
131
internal/storage/datastore.go
Normal file
131
internal/storage/datastore.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.brettb.xyz/goinv/server/internal/types"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type DataStore struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewDataStorePG(host, user, password, dbname, sslmode string) (*DataStore, error) {
|
||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=%s", host, user, password, dbname, sslmode)
|
||||
|
||||
log.Printf("Connecting to %s@%s/%s\n", host, user, dbname)
|
||||
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Printf("Auto-Migrating models")
|
||||
|
||||
if err := db.AutoMigrate(
|
||||
&types.Asset{},
|
||||
&types.Building{},
|
||||
&types.Category{},
|
||||
&types.ShelfLocation{},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DataStore{
|
||||
db: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) CreateAsset(asset *types.Asset) error {
|
||||
result := s.db.Create(asset)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func (s *DataStore) CreateBuilding(building *types.Building) error {
|
||||
result := s.db.Create(building)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func (s *DataStore) CreateCategory(category *types.Category) error {
|
||||
result := s.db.Create(category)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func (s *DataStore) CreateShelfLocation(shelf *types.ShelfLocation) error {
|
||||
result := s.db.Create(shelf)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func (s *DataStore) GetAssetByID(id uint64) (*types.Asset, error) {
|
||||
var result types.Asset
|
||||
tx := s.db.Model(&types.Asset{}).Where("id = ?", id).First(&result)
|
||||
if tx.Error != nil {
|
||||
return nil, fmt.Errorf("asset %d not found", id)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) GetAssets(offset, limit uint64) ([]*types.Asset, error) {
|
||||
var assets []*types.Asset
|
||||
s.db.Joins("Category").Joins("ShelfLocation").Order("id asc").Offset(int(offset)).Limit(int(limit)).Find(&assets)
|
||||
if len(assets) == 0 {
|
||||
return nil, fmt.Errorf("no assets found")
|
||||
}
|
||||
return assets, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) TotalAssets() (int64, error) {
|
||||
var count int64
|
||||
if tx := s.db.Find(&types.Asset{}).Count(&count); tx.Error != nil {
|
||||
return 0, tx.Error
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) DeleteAssetByID(id uint64) (bool, error) {
|
||||
tx := s.db.Delete(&types.Asset{}, id)
|
||||
if tx.Error != nil {
|
||||
return false, fmt.Errorf("unable to delete: %s", tx.Error.Error())
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) GetShelfByID(id uint64) (*types.ShelfLocation, error) {
|
||||
var result types.ShelfLocation
|
||||
tx := s.db.Model(&types.ShelfLocation{}).Where("id = ?", id).First(&result)
|
||||
if tx.Error != nil {
|
||||
return nil, fmt.Errorf("shelf %d not found", id)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) GetShelves(offset, limit int) ([]*types.ShelfLocation, error) {
|
||||
var shelves []*types.ShelfLocation
|
||||
s.db.Offset(offset).Limit(limit).Find(&shelves)
|
||||
if len(shelves) == 0 {
|
||||
return nil, fmt.Errorf("no shelves found")
|
||||
}
|
||||
return shelves, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) TotalShelves() (int64, error) {
|
||||
var count int64
|
||||
if tx := s.db.Find(&types.ShelfLocation{}).Count(&count); tx.Error != nil {
|
||||
return 0, tx.Error
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) DeleteShelfByID(id uint64) (bool, error) {
|
||||
tx := s.db.Delete(&types.ShelfLocation{}, id)
|
||||
if tx.Error != nil {
|
||||
return false, fmt.Errorf("unable to delete: %s", tx.Error.Error())
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
44
internal/types/api.go
Normal file
44
internal/types/api.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
HTTPStatusCode int `json:"status"`
|
||||
}
|
||||
|
||||
func (r *Response) Render(w http.ResponseWriter, req *http.Request) error {
|
||||
render.Status(req, r.HTTPStatusCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
type APIVersion struct {
|
||||
Major int `json:"major"`
|
||||
Minor int `json:"minor"`
|
||||
Patch int `json:"patch"`
|
||||
}
|
||||
|
||||
type IndexResponse struct {
|
||||
*Response
|
||||
Version APIVersion `json:"version"`
|
||||
}
|
||||
|
||||
type APIError struct {
|
||||
*Response
|
||||
Err error `json:"-"`
|
||||
|
||||
Messages []string `json:"messages"`
|
||||
}
|
||||
|
||||
func NewAPIError(status int, messages ...string) *APIError {
|
||||
return &APIError{
|
||||
Response: &Response{
|
||||
HTTPStatusCode: status,
|
||||
},
|
||||
Err: nil,
|
||||
Messages: messages,
|
||||
}
|
||||
}
|
||||
63
internal/types/assets.go
Normal file
63
internal/types/assets.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
Base Model
|
||||
*/
|
||||
|
||||
type Asset struct {
|
||||
ID uint64 `gorm:"primarykey" json:"id"`
|
||||
Name string `json:"name"`
|
||||
Quantity int `json:"quantity"`
|
||||
Length string `json:"length,omitempty"`
|
||||
Manufacturer string `json:"manufacturer,omitempty"`
|
||||
ModelName string `json:"model_name,omitempty"`
|
||||
Price float64 `json:"price,omitempty"`
|
||||
Comments string `json:"comments,omitempty"`
|
||||
ShelfLocationID *uint64 `json:"-"`
|
||||
ShelfLocation *ShelfLocation `json:"shelf_location,omitempty"`
|
||||
CategoryID *uint64 `json:"-"`
|
||||
Category *Category `json:"category,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
Requests
|
||||
*/
|
||||
|
||||
type CreateAssetRequest struct {
|
||||
Name string `json:"name"`
|
||||
Quantity int `json:"quantity"`
|
||||
Length string `json:"length,omitempty"`
|
||||
Manufacturer string `json:"manufacturer,omitempty"`
|
||||
ModelName string `json:"model_name,omitempty"`
|
||||
Price float64 `json:"price,omitempty"`
|
||||
Comments string `json:"comments,omitempty"`
|
||||
ShelfLocationID *uint64 `json:"shelf_location_id,omitempty"`
|
||||
CategoryID *uint64 `json:"category_id,omitempty"`
|
||||
}
|
||||
|
||||
func (c CreateAssetRequest) Bind(r *http.Request) error { return nil }
|
||||
|
||||
/*
|
||||
Responses
|
||||
*/
|
||||
|
||||
type AssetResponse struct {
|
||||
*Response
|
||||
Asset *Asset `json:"asset"`
|
||||
}
|
||||
|
||||
type MultipleAssetsResponse struct {
|
||||
*Response
|
||||
Assets []*Asset `json:"assets"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
28
internal/types/buildings.go
Normal file
28
internal/types/buildings.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
Base Model
|
||||
*/
|
||||
|
||||
type Building struct {
|
||||
ID uint64 `gorm:"primarykey" json:"id"`
|
||||
Name string `json:"name"`
|
||||
ShelfLocations []ShelfLocation `json:"shelves"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
Requests
|
||||
*/
|
||||
|
||||
/*
|
||||
Responses
|
||||
*/
|
||||
28
internal/types/categories.go
Normal file
28
internal/types/categories.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
Base Model
|
||||
*/
|
||||
|
||||
type Category struct {
|
||||
ID uint64 `gorm:"primarykey" json:"id"`
|
||||
Name string `json:"name"`
|
||||
Assets []Asset `json:"assets,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
Requests
|
||||
*/
|
||||
|
||||
/*
|
||||
Responses
|
||||
*/
|
||||
43
internal/types/shelves.go
Normal file
43
internal/types/shelves.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
Base Model
|
||||
*/
|
||||
|
||||
type ShelfLocation struct {
|
||||
ID uint64 `gorm:"primarykey" json:"id"`
|
||||
Name string `json:"name"`
|
||||
RoomNumber string `json:"room_number,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
BuildingID uint64 `json:"-"`
|
||||
Building Building `json:"building"`
|
||||
Assets []Asset `json:"assets,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
}
|
||||
|
||||
/*
|
||||
Requests
|
||||
*/
|
||||
|
||||
/*
|
||||
Responses
|
||||
*/
|
||||
|
||||
type ShelfResponse struct {
|
||||
*Response
|
||||
ShelfLocation *ShelfLocation `json:"shelf"`
|
||||
}
|
||||
|
||||
type MultipleShelfResponse struct {
|
||||
*Response
|
||||
ShelfLocations []*ShelfLocation `json:"shelves"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
Reference in New Issue
Block a user