Skip to content

Commit

Permalink
feat: add table crud
Browse files Browse the repository at this point in the history
  • Loading branch information
Sardonyx001 committed Mar 1, 2024
1 parent fc25e51 commit 3068ff0
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 2 deletions.
3 changes: 1 addition & 2 deletions backend/models/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package models

type Table struct {
UUIDBaseModel
RestaurantID string
Restaurant Restaurant
RestaurantID string `json:"restaurant_id"`
Name string `json:"name" gorm:"not null"`
Empty bool `json:"empty" gorm:"not null"`
Orders []Order
Expand Down
87 changes: 87 additions & 0 deletions backend/server/handlers/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package handlers

import (
"backend/models"
"backend/services"
"net/http"

"github.com/labstack/echo/v4"
)

type (
TableHandler interface {
GetTables(c echo.Context) error
GetTableById(c echo.Context) error
CreateTable(c echo.Context) error
UpdateTableById(c echo.Context) error
DeleteTableById(c echo.Context) error
}

tableHandler struct {
tableService services.TableService
}
)

func NewTableHandler(tableService services.TableService) TableHandler {
return &tableHandler{tableService}
}

func (h *tableHandler) GetTables(c echo.Context) error {
tables, err := h.tableService.GetTables()
if err != nil {
return c.JSON(http.StatusNotFound, "No tables found")
}

return c.JSON(http.StatusOK, tables)
}

func (h *tableHandler) GetTableById(c echo.Context) error {
tableID := c.Param("table_id")

table, err := h.tableService.GetTableById(tableID)
if err != nil {
return c.JSON(http.StatusNotFound, "No table found")
}

return c.JSON(http.StatusOK, table)
}

func (h *tableHandler) CreateTable(c echo.Context) error {
table := models.Table{}
if err := c.Bind(&table); err != nil {
return c.JSON(http.StatusBadRequest, "Record creation failed")
}

tableID, err := h.tableService.CreateTable(&table)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Record creation failed")
}

return c.JSON(http.StatusCreated, map[string]string{"id": tableID})
}

func (h *tableHandler) UpdateTableById(c echo.Context) error {
tableID := c.Param("table_id")

table := new(models.Table)
if err := c.Bind(table); err != nil {
return c.JSON(http.StatusBadRequest, "Record creation failed")
}

tableID, err := h.tableService.UpdateTable(table, tableID)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Record creation failed")
}

return c.JSON(http.StatusOK, map[string]string{"id": tableID})
}

func (h *tableHandler) DeleteTableById(c echo.Context) error {
tableID := c.Param("table_id")

if err := h.tableService.DeleteTable(tableID); err != nil {
return c.JSON(http.StatusInternalServerError, "Record deletion failed")
}

return c.JSON(http.StatusOK, "Record deletion succeeded")
}
53 changes: 53 additions & 0 deletions backend/services/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package services

import (
"backend/models"
"backend/stores"
)

type (
TableService interface {
GetTableById(id string) (*models.Table, error)
GetTables() ([]models.Table, error)
CreateTable(table *models.Table) (string, error)
UpdateTable(table *models.Table, id string) (string, error)
DeleteTable(id string) error
}

tableService struct {
stores *stores.Stores
}
)

func NewTableService(stores *stores.Stores) TableService {
return &tableService{stores}
}

func (s *tableService) GetTableById(id string) (*models.Table, error) {
return s.stores.Table.GetById(id)
}

func (s *tableService) GetTables() ([]models.Table, error) {
return s.stores.Table.GetAll()
}

func (s *tableService) CreateTable(table *models.Table) (string, error) {
tableID, err := s.stores.Table.Create(table)
return tableID, err
}

func (s *tableService) UpdateTable(newTable *models.Table, id string) (string, error) {
table, err := s.stores.Table.GetById(id)
if err != nil {
return "", err
}
table.Name = newTable.Name
table.Empty = newTable.Empty

updatedTableID, err := s.stores.Table.Update(table)
return updatedTableID, err
}

func (s *tableService) DeleteTable(id string) error {
return s.stores.Table.DeleteById(id)
}
64 changes: 64 additions & 0 deletions backend/stores/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package stores

import (
"backend/models"

"github.com/labstack/gommon/log"
"gorm.io/gorm"
)

type (
TableStore interface {
Create(table *models.Table) (string, error)
Update(table *models.Table) (string, error)
GetAll() ([]models.Table, error)
GetById(id string) (*models.Table, error)
DeleteById(id string) error
}

tableStore struct {
*gorm.DB
}
)

func (s *tableStore) Create(table *models.Table) (string, error) {
result := s.DB.Create(table)
if result.Error != nil {
return "", result.Error
}
return table.ID, nil
}

func (s *tableStore) Update(table *models.Table) (string, error) {

result := s.DB.Save(table)
if result.Error != nil {
return "", result.Error
}
return table.ID, nil
}

func (s *tableStore) GetById(id string) (*models.Table, error) {
var table models.Table
result := s.DB.Preload("Orders").First(&table, "id = ?", id)
if result.Error != nil {
log.Error("can't find table: ", result.Error)
return nil, result.Error
}
return &table, nil
}

func (s *tableStore) GetAll() ([]models.Table, error) {
var tables []models.Table

result := s.DB.Preload("Orders").Find(&tables)
if result.Error != nil {
log.Error("Can't find tables: ", result.Error)
return nil, result.Error
}
return tables, nil
}

func (s *tableStore) DeleteById(id string) error {
return s.DB.Delete(&models.Table{}, id).Error
}

0 comments on commit 3068ff0

Please sign in to comment.