Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
feat: services.Rulesheets Get by slug
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasfeijo committed Dec 26, 2023
1 parent 047e00f commit 2456a20
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion controllers/v1/rulesheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (rc *rulesheets) GetRulesheets() gin.HandlerFunc {
}

// GetRulesheet godoc
// @Summary Obter Folha de Regra por ID
// @Summary Obter Folha de Regra por ID ou Slug
// @Description Para se obter a folha de regra por ID, basta clicar em **Try it out** e colocar o ID desejado em *id*. Em seguida, clique em **Execute** e caso o ID exista retornará a folha de regra com o número de ID desejado.
// @Tags Rulesheet
// @Accept json
Expand Down
27 changes: 23 additions & 4 deletions services/rulesheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"context"
"fmt"
"unicode"

"github.com/bancodobrasil/featws-api/dtos"
"github.com/bancodobrasil/featws-api/models"
Expand Down Expand Up @@ -152,12 +153,30 @@ func (rs rulesheets) Count(ctx context.Context, entity interface{}) (count int64
// returns it.
func (rs rulesheets) Get(ctx context.Context, id string) (result *dtos.Rulesheet, err error) {

entity, err := rs.repository.Get(ctx, id)
if err != nil {
log.Errorf("Error on fetch rulesheet(get): %v", err)
return
isSlug := true
if unicode.IsDigit(rune(id[0])) {
isSlug = false
}

var entity *models.Rulesheet

if isSlug {
findResult, err2 := rs.repository.Find(ctx, map[string]interface{}{"slug": id}, nil)
if err2 != nil {
log.Errorf("Error on fetch rulesheet(get): %v", err)
return
}
if len(findResult) == 0 {
return nil, nil
}
entity = findResult[0]
} else {
entity, err = rs.repository.Get(ctx, id)
if err != nil {
log.Errorf("Error on fetch rulesheet(get): %v", err)
return
}
}
result = newRulesheetDTO(entity)

if result != nil {
Expand Down
24 changes: 24 additions & 0 deletions services/rulesheets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ func TestGetSucess(t *testing.T) {
}
}

// This test function tests the successful retrieval (by slug) of a rulesheet entity from a
// repository and its filling with data from a Gitlab service.
func TestGetBySlugSucess(t *testing.T) {

ctx := context.Background()
dto := &dtos.Rulesheet{
Slug: "test",
}
entity, err := models.NewRulesheetV1(*dto)
if err != nil {
t.Error("unexpected error on model creation")
}
var opts *repository.FindOptions = nil
repository := new(mocks_repository.Rulesheets)
repository.On("Find", ctx, map[string]interface{}{"slug": "test"}, opts).Return([]*models.Rulesheet{&entity}, nil)
gitlabService := new(mocks_services.Gitlab)
gitlabService.On("Fill", dto).Return(nil)
service := services.NewRulesheets(repository, gitlabService)
_, err = service.Get(ctx, "test")
if err != nil {
t.Error("unexpected error on get")
}
}

// This's a Get function of a Rulesheets service, which tests for an error on
// model creation.
func TestGetWithErrorOnCreateModel(t *testing.T) {
Expand Down

0 comments on commit 2456a20

Please sign in to comment.