Skip to content

Commit

Permalink
Merge branch 'main' into errors-propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
Arief Rahmansyah committed Jan 30, 2024
2 parents 2ecd28b + 0857c40 commit 2f910fd
Show file tree
Hide file tree
Showing 224 changed files with 45,669 additions and 20,633 deletions.
19 changes: 13 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,29 @@ generate-client-go:
@echo "Generating Go client from swagger.yaml"
@mv ${CLIENT_GO_EXAMPLES_DIR} ${TEMP_CLIENT_GO_EXAMPLES_DIR}
@rm -rf ${CLIENT_GO_OUTPUT_DIR}
@swagger-codegen generate -i swagger.yaml -l go -o ${TEMP_CLIENT_GO_OUTPUT_DIR} -DpackageName=client
@docker run --rm -v ${PWD}/:/local openapitools/openapi-generator-cli:v7.1.0 generate \
--input-spec /local/swagger.yaml \
--generator-name go \
--output /local/api/client_tmp/. \
--config /local/openapi-api-codegen.yaml \
--openapi-normalizer KEEP_ONLY_FIRST_TAG_IN_OPERATION=true
@mkdir ${CLIENT_GO_OUTPUT_DIR}
@mv ${TEMP_CLIENT_GO_OUTPUT_DIR}/*.go ${CLIENT_GO_OUTPUT_DIR}
@rm -rf ${TEMP_CLIENT_GO_OUTPUT_DIR}
@mv ${TEMP_CLIENT_GO_EXAMPLES_DIR} ${CLIENT_GO_EXAMPLES_DIR}
@goimports -w ${CLIENT_GO_OUTPUT_DIR}


CLIENT_PYTHON_OUTPUT_DIR = ./python/sdk/client
TEMP_CLIENT_PYTHON_OUTPUT_DIR = ./python/sdk/client_tmp
.PHONY: generate-client-python
generate-client-python:
@echo "Generating Python client from swagger.yaml"
@rm -rf ${CLIENT_PYTHON_OUTPUT_DIR}
@swagger-codegen generate -i swagger.yaml -l python -o ${TEMP_CLIENT_PYTHON_OUTPUT_DIR} -DpackageName=client
@mv ${TEMP_CLIENT_PYTHON_OUTPUT_DIR}/client ${CLIENT_PYTHON_OUTPUT_DIR}
@rm -rf ${TEMP_CLIENT_PYTHON_OUTPUT_DIR}
rm -rf ${CLIENT_PYTHON_OUTPUT_DIR}
@docker run --rm -v ${PWD}/:/local openapitools/openapi-generator-cli:v7.1.0 generate \
--input-spec /local/swagger.yaml \
--generator-name python \
--output /local/python/sdk/. \
--config /local/openapi-sdk-codegen.yaml


.PHONY: generate-proto
Expand Down
74 changes: 74 additions & 0 deletions api/api/model_schema_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package api

import (
"errors"
"fmt"
"net/http"

"github.com/caraml-dev/merlin/models"
mErrors "github.com/caraml-dev/merlin/pkg/errors"
)

type ModelSchemaController struct {
*AppContext
}

func (m *ModelSchemaController) GetAllSchemas(r *http.Request, vars map[string]string, _ interface{}) *Response {
ctx := r.Context()
modelID, _ := models.ParseID(vars["model_id"])
modelSchemas, err := m.ModelSchemaService.List(ctx, modelID)
if err != nil {
if errors.Is(err, mErrors.ErrNotFound) {
return NotFound(fmt.Sprintf("Model schemas not found: %v", err))
}
return InternalServerError(fmt.Sprintf("Error get All schemas with model id: %d with error: %v", modelID, err))
}
return Ok(modelSchemas)
}

func (m *ModelSchemaController) GetSchema(r *http.Request, vars map[string]string, _ interface{}) *Response {
ctx := r.Context()
modelID, _ := models.ParseID(vars["model_id"])
modelSchemaID, _ := models.ParseID(vars["schema_id"])
modelSchema, err := m.ModelSchemaService.FindByID(ctx, modelSchemaID, modelID)
if err != nil {
if errors.Is(err, mErrors.ErrNotFound) {
return NotFound(fmt.Sprintf("Model schema with id: %d not found: %v", modelSchemaID, err))
}
return InternalServerError(fmt.Sprintf("Error get schema with id: %d, model id: %d and error: %v", modelSchemaID, modelID, err))
}

return Ok(modelSchema)
}

func (m *ModelSchemaController) CreateOrUpdateSchema(r *http.Request, vars map[string]string, body interface{}) *Response {
ctx := r.Context()
modelID, _ := models.ParseID(vars["model_id"])

modelSchema, ok := body.(*models.ModelSchema)
if !ok {
return BadRequest("Unable to parse request body")
}

if modelSchema.ModelID > 0 && modelSchema.ModelID != modelID {
return BadRequest("Mismatch model id between request path and body")
}

modelSchema.ModelID = modelID
schema, err := m.ModelSchemaService.Save(ctx, modelSchema)
if err != nil {
return InternalServerError(fmt.Sprintf("Error save model schema: %v", err))
}
return Ok(schema)
}

func (m *ModelSchemaController) DeleteSchema(r *http.Request, vars map[string]string, _ interface{}) *Response {
ctx := r.Context()
modelID, _ := models.ParseID(vars["model_id"])
modelSchemaID, _ := models.ParseID(vars["schema_id"])
modelSchema := &models.ModelSchema{ID: modelSchemaID, ModelID: modelID}
if err := m.ModelSchemaService.Delete(ctx, modelSchema); err != nil {
return InternalServerError(fmt.Sprintf("Error delete model schema: %v", err))
}
return NoContent()
}
Loading

0 comments on commit 2f910fd

Please sign in to comment.