Add GetPAT function

This commit is contained in:
Ahsan Barkati 2023-02-15 11:13:33 +05:30
parent 388ef9453c
commit 96267e2e3a
3 changed files with 21 additions and 3 deletions

View File

@ -45,7 +45,6 @@ func (ah *APIHandler) createPAT(w http.ResponseWriter, r *http.Request) {
req.Token = generatePATToken() req.Token = generatePATToken()
zap.S().Infof("Got PAT request: %+v", req) zap.S().Infof("Got PAT request: %+v", req)
if apierr := ah.AppDao().CreatePAT(ctx, &req); apierr != nil { if apierr := ah.AppDao().CreatePAT(ctx, &req); apierr != nil {
RespondError(w, apierr, nil) RespondError(w, apierr, nil)
return return

View File

@ -35,6 +35,7 @@ type ModelDao interface {
GetDomainByEmail(ctx context.Context, email string) (*model.OrgDomain, basemodel.BaseApiError) GetDomainByEmail(ctx context.Context, email string) (*model.OrgDomain, basemodel.BaseApiError)
CreatePAT(ctx context.Context, p *model.PAT) basemodel.BaseApiError CreatePAT(ctx context.Context, p *model.PAT) basemodel.BaseApiError
GetPAT(ctx context.Context, patID string) (*model.PAT, basemodel.BaseApiError)
ListPATs(ctx context.Context, userID string) ([]model.PAT, basemodel.BaseApiError) ListPATs(ctx context.Context, userID string) ([]model.PAT, basemodel.BaseApiError)
DeletePAT(ctx context.Context, id string) basemodel.BaseApiError DeletePAT(ctx context.Context, id string) basemodel.BaseApiError
} }

View File

@ -29,7 +29,7 @@ func (m *modelDao) ListPATs(ctx context.Context, userID string) ([]model.PAT, ba
if err := m.DB().Select(&pats, `SELECT * FROM personal_access_tokens WHERE user_id=?;`, userID); err != nil { if err := m.DB().Select(&pats, `SELECT * FROM personal_access_tokens WHERE user_id=?;`, userID); err != nil {
zap.S().Errorf("Failed to fetch PATs for user: %s, err: %v", userID, zap.Error(err)) zap.S().Errorf("Failed to fetch PATs for user: %s, err: %v", userID, zap.Error(err))
return nil, model.InternalError(fmt.Errorf("Failed to fetch PATs")) return nil, model.InternalError(fmt.Errorf("failed to fetch PATs"))
} }
return pats, nil return pats, nil
} }
@ -38,7 +38,25 @@ func (m *modelDao) DeletePAT(ctx context.Context, id string) basemodel.BaseApiEr
_, err := m.DB().ExecContext(ctx, `DELETE from personal_access_tokens where id=?;`, id) _, err := m.DB().ExecContext(ctx, `DELETE from personal_access_tokens where id=?;`, id)
if err != nil { if err != nil {
zap.S().Errorf("Failed to delete PAT, err: %v", zap.Error(err)) zap.S().Errorf("Failed to delete PAT, err: %v", zap.Error(err))
return model.InternalError(fmt.Errorf("Failed to delete PAT")) return model.InternalError(fmt.Errorf("failed to delete PAT"))
} }
return nil return nil
} }
func (m *modelDao) GetPAT(ctx context.Context, patID string) (*model.PAT, basemodel.BaseApiError) {
pats := []model.PAT{}
if err := m.DB().Select(&pats, `SELECT * FROM personal_access_tokens WHERE id=?;`, patID); err != nil {
zap.S().Errorf("Failed to fetch PAT with ID: %s, err: %v", patID, zap.Error(err))
return nil, model.InternalError(fmt.Errorf("failed to fetch PAT"))
}
if len(pats) != 1 {
return nil, &model.ApiError{
Typ: model.ErrorInternal,
Err: fmt.Errorf("found zero or multiple PATS with same ID"),
}
}
return &pats[0], nil
}