2025-07-30 23:18:20 +07:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-03 00:34:25 +07:00
|
|
|
"apskel-pos-be/internal/appcontext"
|
|
|
|
|
"apskel-pos-be/internal/constants"
|
2025-07-30 23:18:20 +07:00
|
|
|
"apskel-pos-be/internal/contract"
|
|
|
|
|
"apskel-pos-be/internal/processor"
|
|
|
|
|
"apskel-pos-be/internal/transformer"
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
type TableServiceImpl struct {
|
2025-07-30 23:18:20 +07:00
|
|
|
tableProcessor *processor.TableProcessor
|
|
|
|
|
tableTransformer *transformer.TableTransformer
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func NewTableService(tableProcessor *processor.TableProcessor, tableTransformer *transformer.TableTransformer) *TableServiceImpl {
|
|
|
|
|
return &TableServiceImpl{
|
2025-07-30 23:18:20 +07:00
|
|
|
tableProcessor: tableProcessor,
|
|
|
|
|
tableTransformer: tableTransformer,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func (s *TableServiceImpl) CreateTable(ctx context.Context, apctx *appcontext.ContextInfo, req *contract.CreateTableRequest) *contract.Response {
|
|
|
|
|
modelReq := transformer.CreateTableRequestToModel(apctx, req)
|
2025-07-30 23:18:20 +07:00
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
response, err := s.tableProcessor.Create(ctx, modelReq, apctx.OrganizationID)
|
2025-07-30 23:18:20 +07:00
|
|
|
if err != nil {
|
2025-08-03 00:34:25 +07:00
|
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
|
|
|
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
contractResponse := s.tableTransformer.ToContract(*response)
|
|
|
|
|
return contract.BuildSuccessResponse(contractResponse)
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func (s *TableServiceImpl) UpdateTable(ctx context.Context, id uuid.UUID, req *contract.UpdateTableRequest) *contract.Response {
|
|
|
|
|
modelReq := transformer.UpdateTableRequestToModel(req)
|
2025-07-30 23:18:20 +07:00
|
|
|
|
|
|
|
|
response, err := s.tableProcessor.Update(ctx, id, modelReq)
|
|
|
|
|
if err != nil {
|
2025-08-03 00:34:25 +07:00
|
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
|
|
|
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
contractResponse := s.tableTransformer.ToContract(*response)
|
|
|
|
|
return contract.BuildSuccessResponse(contractResponse)
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func (s *TableServiceImpl) DeleteTable(ctx context.Context, id uuid.UUID) *contract.Response {
|
|
|
|
|
err := s.tableProcessor.Delete(ctx, id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
|
|
|
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
return contract.BuildSuccessResponse(map[string]interface{}{
|
|
|
|
|
"message": "Table deleted successfully",
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-07-30 23:18:20 +07:00
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func (s *TableServiceImpl) GetTableByID(ctx context.Context, id uuid.UUID) *contract.Response {
|
|
|
|
|
response, err := s.tableProcessor.GetByID(ctx, id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errorResp := contract.NewResponseError(constants.NotFoundErrorCode, constants.TableEntity, "Table not found")
|
|
|
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
contractResponse := s.tableTransformer.ToContract(*response)
|
|
|
|
|
return contract.BuildSuccessResponse(contractResponse)
|
|
|
|
|
}
|
2025-07-30 23:18:20 +07:00
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func (s *TableServiceImpl) ListTables(ctx context.Context, req *contract.ListTablesQuery) *contract.Response {
|
|
|
|
|
modelReq := transformer.ListTablesQueryToModel(req)
|
2025-07-30 23:18:20 +07:00
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
response, err := s.tableProcessor.List(ctx, modelReq)
|
2025-07-30 23:18:20 +07:00
|
|
|
if err != nil {
|
2025-08-03 00:34:25 +07:00
|
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
|
|
|
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contractTables := make([]contract.TableResponse, len(response.Tables))
|
|
|
|
|
for i, table := range response.Tables {
|
|
|
|
|
contractTables[i] = *s.tableTransformer.ToContract(table)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
contractResponse := &contract.ListTablesResponse{
|
2025-07-30 23:18:20 +07:00
|
|
|
Tables: contractTables,
|
|
|
|
|
TotalCount: response.TotalCount,
|
|
|
|
|
Page: response.Page,
|
|
|
|
|
Limit: response.Limit,
|
|
|
|
|
TotalPages: response.TotalPages,
|
2025-08-03 00:34:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return contract.BuildSuccessResponse(contractResponse)
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func (s *TableServiceImpl) OccupyTable(ctx context.Context, tableID uuid.UUID, req *contract.OccupyTableRequest) *contract.Response {
|
|
|
|
|
modelReq := transformer.OccupyTableRequestToModel(req)
|
2025-07-30 23:18:20 +07:00
|
|
|
|
|
|
|
|
response, err := s.tableProcessor.OccupyTable(ctx, tableID, modelReq)
|
|
|
|
|
if err != nil {
|
2025-08-03 00:34:25 +07:00
|
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
|
|
|
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
contractResponse := s.tableTransformer.ToContract(*response)
|
|
|
|
|
return contract.BuildSuccessResponse(contractResponse)
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func (s *TableServiceImpl) ReleaseTable(ctx context.Context, tableID uuid.UUID, req *contract.ReleaseTableRequest) *contract.Response {
|
|
|
|
|
modelReq := transformer.ReleaseTableRequestToModel(req)
|
2025-07-30 23:18:20 +07:00
|
|
|
|
|
|
|
|
response, err := s.tableProcessor.ReleaseTable(ctx, tableID, modelReq)
|
|
|
|
|
if err != nil {
|
2025-08-03 00:34:25 +07:00
|
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
|
|
|
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
contractResponse := s.tableTransformer.ToContract(*response)
|
|
|
|
|
return contract.BuildSuccessResponse(contractResponse)
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func (s *TableServiceImpl) GetAvailableTables(ctx context.Context, outletID uuid.UUID) *contract.Response {
|
2025-07-30 23:18:20 +07:00
|
|
|
tables, err := s.tableProcessor.GetAvailableTables(ctx, outletID)
|
|
|
|
|
if err != nil {
|
2025-08-03 00:34:25 +07:00
|
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
|
|
|
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
responses := make([]contract.TableResponse, len(tables))
|
|
|
|
|
for i, table := range tables {
|
|
|
|
|
responses[i] = *s.tableTransformer.ToContract(table)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
return contract.BuildSuccessResponse(responses)
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
func (s *TableServiceImpl) GetOccupiedTables(ctx context.Context, outletID uuid.UUID) *contract.Response {
|
2025-07-30 23:18:20 +07:00
|
|
|
tables, err := s.tableProcessor.GetOccupiedTables(ctx, outletID)
|
|
|
|
|
if err != nil {
|
2025-08-03 00:34:25 +07:00
|
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
|
|
|
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
responses := make([]contract.TableResponse, len(tables))
|
|
|
|
|
for i, table := range tables {
|
|
|
|
|
responses[i] = *s.tableTransformer.ToContract(table)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 00:34:25 +07:00
|
|
|
return contract.BuildSuccessResponse(responses)
|
2025-07-30 23:18:20 +07:00
|
|
|
}
|