-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: unify pipeline trigger chart endpoint with credit chart #228
Merged
donch1989
merged 4 commits into
main
from
jvalles/ins-4936-unify-pipeline-and-credit-chart-endpoint-contracts
Jul 26, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,10 +61,11 @@ type Repository interface { | |
UpdateOrganization(ctx context.Context, id string, user *datamodel.Owner) error | ||
DeleteOrganization(ctx context.Context, id string) error | ||
|
||
GetOwner(ctx context.Context, id string, includeAvatar bool) (*datamodel.Owner, error) | ||
GetOwnerByUID(ctx context.Context, uid uuid.UUID) (*datamodel.Owner, error) | ||
|
||
listOwners(ctx context.Context, ownerType string, pageSize int, pageToken string, filter filtering.Filter) ([]*datamodel.Owner, int64, string, error) | ||
createOwner(ctx context.Context, ownerType string, user *datamodel.Owner) error | ||
getOwner(ctx context.Context, ownerType string, id string, includeAvatar bool) (*datamodel.Owner, error) | ||
getOwnerByUID(ctx context.Context, ownerType string, uid uuid.UUID) (*datamodel.Owner, error) | ||
updateOwner(ctx context.Context, ownerType string, id string, user *datamodel.Owner) error | ||
deleteOwner(ctx context.Context, ownerType string, id string) error | ||
|
||
|
@@ -119,11 +120,32 @@ func (r *repository) ListUsers(ctx context.Context, pageSize int, pageToken stri | |
func (r *repository) CreateUser(ctx context.Context, user *datamodel.Owner) error { | ||
return r.createOwner(ctx, "user", user) | ||
} | ||
|
||
// ownerWithType is used to wrap an owner fetch with a tyupe check. | ||
func ownerWithType(o *datamodel.Owner, ownerType string) (*datamodel.Owner, error) { | ||
if !o.OwnerType.Valid || o.OwnerType.String != ownerType { | ||
return nil, gorm.ErrRecordNotFound | ||
} | ||
|
||
return o, nil | ||
} | ||
|
||
func (r *repository) GetUser(ctx context.Context, id string, includeAvatar bool) (*datamodel.Owner, error) { | ||
return r.getOwner(ctx, "user", id, includeAvatar) | ||
owner, err := r.GetOwner(ctx, id, includeAvatar) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ownerWithType(owner, "user") | ||
} | ||
|
||
func (r *repository) GetUserByUID(ctx context.Context, uid uuid.UUID) (*datamodel.Owner, error) { | ||
return r.getOwnerByUID(ctx, "user", uid) | ||
owner, err := r.GetOwnerByUID(ctx, uid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ownerWithType(owner, "user") | ||
} | ||
func (r *repository) UpdateUser(ctx context.Context, id string, user *datamodel.Owner) error { | ||
return r.updateOwner(ctx, "user", id, user) | ||
|
@@ -138,12 +160,25 @@ func (r *repository) ListOrganizations(ctx context.Context, pageSize int, pageTo | |
func (r *repository) CreateOrganization(ctx context.Context, org *datamodel.Owner) error { | ||
return r.createOwner(ctx, "organization", org) | ||
} | ||
|
||
func (r *repository) GetOrganization(ctx context.Context, id string, includeAvatar bool) (*datamodel.Owner, error) { | ||
return r.getOwner(ctx, "organization", id, includeAvatar) | ||
owner, err := r.GetOwner(ctx, id, includeAvatar) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ownerWithType(owner, "organization") | ||
} | ||
|
||
func (r *repository) GetOrganizationByUID(ctx context.Context, uid uuid.UUID) (*datamodel.Owner, error) { | ||
return r.getOwnerByUID(ctx, "organization", uid) | ||
owner, err := r.GetOwnerByUID(ctx, uid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ownerWithType(owner, "organization") | ||
} | ||
|
||
func (r *repository) UpdateOrganization(ctx context.Context, id string, org *datamodel.Owner) error { | ||
return r.updateOwner(ctx, "organization", id, org) | ||
} | ||
|
@@ -250,12 +285,11 @@ func (r *repository) createOwner(ctx context.Context, ownerType string, owner *d | |
return nil | ||
} | ||
|
||
func (r *repository) getOwner(ctx context.Context, ownerType string, id string, includeAvatar bool) (*datamodel.Owner, error) { | ||
|
||
func (r *repository) GetOwner(ctx context.Context, id string, includeAvatar bool) (*datamodel.Owner, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @donch1989 I mentioned this change in your PR yesterday, what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we can expose it. |
||
db := r.CheckPinnedUser(ctx, r.db) | ||
|
||
var owner datamodel.Owner | ||
queryBuilder := db.Model(&datamodel.Owner{}).Where("owner_type = ?", ownerType).Where("id = ?", id) | ||
queryBuilder := db.Model(&datamodel.Owner{}).Where("id = ?", id) | ||
if !includeAvatar { | ||
queryBuilder = queryBuilder.Omit("profile_avatar") | ||
} | ||
|
@@ -265,12 +299,12 @@ func (r *repository) getOwner(ctx context.Context, ownerType string, id string, | |
return &owner, nil | ||
} | ||
|
||
func (r *repository) getOwnerByUID(ctx context.Context, ownerType string, uid uuid.UUID) (*datamodel.Owner, error) { | ||
|
||
func (r *repository) GetOwnerByUID(ctx context.Context, uid uuid.UUID) (*datamodel.Owner, error) { | ||
db := r.CheckPinnedUser(ctx, r.db) | ||
|
||
var owner datamodel.Owner | ||
if result := db.Model(&datamodel.Owner{}).Omit("profile_avatar").Where("owner_type = ?", ownerType).Where("uid = ?", uid.String()).First(&owner); result.Error != nil { | ||
result := db.Model(&datamodel.Owner{}).Omit("profile_avatar").Where("uid = ?", uid.String()).First(&owner) | ||
if result.Error != nil { | ||
return nil, result.Error | ||
} | ||
return &owner, nil | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💭 @donch1989 if owner ID is unique perhaps we don't need the type checks in these methods and we can delegate on the service layer to check the authenticated user permissions to perform these actions.