-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: SJRA-51 user set model and get user set by id endpoint
- Loading branch information
1 parent
112614c
commit 47566f9
Showing
13 changed files
with
542 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package repository | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/Ferlab-Ste-Justine/radiant-api/internal/types" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type UserSetsRepository struct { | ||
db *gorm.DB | ||
} | ||
|
||
type UserSetsDAO interface { | ||
GetUserSet(userSetId string) (*types.UserSet, error) | ||
} | ||
|
||
func NewUserSetsRepository(db *gorm.DB) *UserSetsRepository { | ||
return &UserSetsRepository{db: db} | ||
} | ||
|
||
func (r *UserSetsRepository) GetUserSet(userSetId string) (*types.UserSet, error) { | ||
var dao types.UserSetDAO | ||
if result := r.db. | ||
Table(types.UserSetTable.Name). | ||
Preload("ParticipantIds"). | ||
Preload("FileIds"). | ||
Preload("BiospecimenIds"). | ||
Preload("VariantIds"). | ||
Where("id = ?", userSetId). | ||
First(&dao); result.Error != nil { | ||
err := result.Error | ||
if !errors.Is(err, gorm.ErrRecordNotFound) { | ||
return nil, fmt.Errorf("error while fetching user set: %w", err) | ||
} else { | ||
return nil, nil | ||
} | ||
} | ||
mapped, err := r.mapToUserSet(&dao) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return mapped, nil | ||
} | ||
|
||
func (r *UserSetsRepository) mapToUserSet(dao *types.UserSetDAO) (*types.UserSet, error) { | ||
ids := make([]string, 0) | ||
if len(dao.ParticipantIds) > 0 { | ||
ids = r.ParticipantIds(dao.ParticipantIds) | ||
} else if len(dao.FileIds) > 0 { | ||
ids = r.FileIds(dao.FileIds) | ||
} else if len(dao.BiospecimenIds) > 0 { | ||
ids = r.BiospecimenIds(dao.BiospecimenIds) | ||
} else if len(dao.VariantIds) > 0 { | ||
ids = r.VariantIds(dao.VariantIds) | ||
} | ||
|
||
userSet := &types.UserSet{ | ||
ID: dao.ID, | ||
UserId: dao.UserId, | ||
Name: dao.Name, | ||
Type: dao.Type, | ||
Active: dao.Active, | ||
CreatedAt: dao.CreatedAt, | ||
UpdatedAt: dao.UpdatedAt, | ||
Ids: ids, | ||
} | ||
return userSet, nil | ||
} | ||
|
||
func (r *UserSetsRepository) ParticipantIds(dao []types.UserSetParticipantDAO) []string { | ||
var list []string | ||
for _, elem := range dao { | ||
list = append(list, elem.ParticipantId) | ||
} | ||
return list | ||
} | ||
|
||
func (r *UserSetsRepository) FileIds(dao []types.UserSetFileDAO) []string { | ||
var list []string | ||
for _, elem := range dao { | ||
list = append(list, elem.FileId) | ||
} | ||
return list | ||
} | ||
|
||
func (r *UserSetsRepository) BiospecimenIds(dao []types.UserSetBiospecimenDAO) []string { | ||
var list []string | ||
for _, elem := range dao { | ||
list = append(list, elem.BiospecimenId) | ||
} | ||
return list | ||
} | ||
|
||
func (r *UserSetsRepository) VariantIds(dao []types.UserSetVariantDAO) []string { | ||
var list []string | ||
for _, elem := range dao { | ||
list = append(list, elem.VariantId) | ||
} | ||
return list | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package server | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Ferlab-Ste-Justine/radiant-api/internal/types" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func (m *MockRepository) GetUserSet(userSetId string) (*types.UserSet, error) { | ||
if userSetId == "set1" { | ||
return &types.UserSet{ | ||
ID: userSetId, | ||
UserId: "user1", | ||
Name: "set_name", | ||
Type: "set_type", | ||
Active: true, | ||
UpdatedAt: time.Date( | ||
2000, 1, 1, 0, 0, 0, 0, time.UTC), | ||
}, nil | ||
} else if userSetId == "set2" { | ||
return nil, fmt.Errorf("error") | ||
} | ||
return nil, nil | ||
} | ||
|
||
func assertGetUserSet(t *testing.T, userSetId string, status int, expected string) { | ||
repo := &MockRepository{} | ||
router := gin.Default() | ||
router.GET("/users/sets/:user_set_id", GetUserSet(repo)) | ||
|
||
req, _ := http.NewRequest("GET", fmt.Sprintf("/users/sets/%s", userSetId), bytes.NewBuffer([]byte("{}"))) | ||
w := httptest.NewRecorder() | ||
router.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, status, w.Code) | ||
assert.JSONEq(t, expected, w.Body.String()) | ||
} | ||
|
||
func Test_GetUserSet_ok(t *testing.T) { | ||
assertGetUserSet(t, "set1", http.StatusOK, `{"id":"set1", "user_id":"user1", "name":"set_name", "type": "set_type", "active":true, "updated_at": "2000-01-01T00:00:00Z"}`) | ||
} | ||
|
||
func Test_GetUserSet_error(t *testing.T) { | ||
assertGetUserSet(t, "set2", http.StatusInternalServerError, `{"error":"internal server error"}`) | ||
} | ||
|
||
func Test_GetUserSet_notFound(t *testing.T) { | ||
assertGetUserSet(t, "set3", http.StatusNotFound, `{"error":"not found"}`) | ||
} |
Oops, something went wrong.