-
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.
- Loading branch information
Showing
6 changed files
with
285 additions
and
1 deletion.
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
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,103 @@ | ||
package pactasrv | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/RMI/pacta/db" | ||
"github.com/RMI/pacta/oapierr" | ||
api "github.com/RMI/pacta/openapi/pacta" | ||
"github.com/RMI/pacta/pacta" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Returns a portfolio group by ID | ||
// (GET /portfolio-group/{id}) | ||
func (s *Server) FindPortfolioGroupById(ctx context.Context, request api.FindPortfolioGroupByIdRequestObject) (api.FindPortfolioGroupByIdResponseObject, error) { | ||
// TODO(#12) Implement Authorization | ||
pg, err := s.DB.PortfolioGroup(s.DB.NoTxn(ctx), pacta.PortfolioGroupID(request.Id)) | ||
if err != nil { | ||
if db.IsNotFound(err) { | ||
return nil, oapierr.NotFound("portfolio group not found", zap.String("portfolio_group_id", request.Id)) | ||
} | ||
return nil, oapierr.Internal("failed to load portfolio_group", zap.String("portfolio_group_id", request.Id), zap.Error(err)) | ||
} | ||
resp, err := conv.PortfolioGroupToOAPI(pg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return api.FindPortfolioGroupById200JSONResponse(*resp), nil | ||
} | ||
|
||
// Returns the portfolio groups that the user has access to | ||
// (GET /portfolio-groups) | ||
func (s *Server) ListPortfolioGroups(ctx context.Context, request api.ListPortfolioGroupsRequestObject) (api.ListPortfolioGroupsResponseObject, error) { | ||
// TODO(#12) Implement Authorization | ||
ownerID, err := s.getUserOwnerID(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pgs, err := s.DB.PortfolioGroupsByOwner(s.DB.NoTxn(ctx), ownerID) | ||
if err != nil { | ||
return nil, oapierr.Internal("failed to query portfolio groups", zap.Error(err)) | ||
} | ||
items, err := dereference(conv.PortfolioGroupsToOAPI(pgs)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return api.ListPortfolioGroups200JSONResponse{Items: items}, nil | ||
} | ||
|
||
// Creates a portfolio group | ||
// (POST /portfolio-groups) | ||
func (s *Server) CreatePortfolioGroup(ctx context.Context, request api.CreatePortfolioGroupRequestObject) (api.CreatePortfolioGroupResponseObject, error) { | ||
userID, err := getUserID(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pg, err := conv.PortfolioGroupCreateFromOAPI(request.Body, userID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
id, err := s.DB.CreatePortfolioGroup(s.DB.NoTxn(ctx), pg) | ||
if err != nil { | ||
return nil, oapierr.Internal("failed to create portfolio group", zap.Error(err)) | ||
} | ||
pg.ID = id | ||
pg.CreatedAt = s.Now() | ||
resp, err := conv.PortfolioGroupToOAPI(pg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return api.CreatePortfolioGroup200Response{Body: resp}, nil | ||
} | ||
|
||
// Updates portfolio group properties | ||
// (PATCH /portfolio-group/{id}) | ||
func (s *Server) UpdatePortfolioGroup(ctx context.Context, request api.UpdatePortfolioGroupRequestObject) (api.UpdatePortfolioGroupResponseObject, error) { | ||
// TODO(#12) Implement Authorization | ||
id := pacta.PortfolioGroupID(request.Id) | ||
mutations := []db.UpdatePortfolioGroupFn{} | ||
b := request.Body | ||
if b.Name != nil { | ||
mutations = append(mutations, db.SetPortfolioGroupName(*b.Name)) | ||
} | ||
if b.Description != nil { | ||
mutations = append(mutations, db.SetPortfolioGroupDescription(*b.Description)) | ||
} | ||
err := s.DB.UpdatePortfolioGroup(s.DB.NoTxn(ctx), id, mutations...) | ||
if err != nil { | ||
return nil, oapierr.Internal("failed to update portfolio group", zap.String("portfolio_group_id", string(id)), zap.Error(err)) | ||
} | ||
return api.UpdatePortfolioGroup204Response{}, nil | ||
} | ||
|
||
// Deletes a portfolio group by ID | ||
// (DELETE /portfolio-group/{id}) | ||
func (s *Server) DeletePortfolioGroup(ctx context.Context, request api.DeletePortfolioGroupRequestObject) (api.DeletePortfolioGroupResponseObject, error) { | ||
// TODO(#12) Implement Authorization | ||
err := s.DB.DeletePortfolioGroup(s.DB.NoTxn(ctx), pacta.PortfolioGroupID(request.Id)) | ||
if err != nil { | ||
return nil, oapierr.Internal("failed to delete portfolio group", zap.String("portfolio_group_id", request.Id), zap.Error(err)) | ||
} | ||
return api.DeletePortfolioGroup204Response{}, 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