Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdea345

Browse files
committed
Cleanup database abstraction
1 parent 29a019a commit bdea345

File tree

5 files changed

+12
-90
lines changed

5 files changed

+12
-90
lines changed

coderd/organizations.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,9 @@ func (api *api) postProjectsByOrganization(rw http.ResponseWriter, r *http.Reque
270270
render.JSON(rw, r, project)
271271
}
272272

273-
// Lists all projects in an organization.
274273
func (api *api) projectsByOrganization(rw http.ResponseWriter, r *http.Request) {
275274
organization := httpmw.OrganizationParam(r)
276-
projects, err := api.Database.GetProjectsByOrganizationIDs(r.Context(), []string{organization.ID})
275+
projects, err := api.Database.GetProjectsByOrganization(r.Context(), organization.ID)
277276
if errors.Is(err, sql.ErrNoRows) {
278277
err = nil
279278
}

database/databasefake/databasefake.go

+4-26
Original file line numberDiff line numberDiff line change
@@ -281,26 +281,6 @@ func (q *fakeQuerier) GetWorkspaceBuildByWorkspaceIDAndName(_ context.Context, a
281281
return database.WorkspaceBuild{}, sql.ErrNoRows
282282
}
283283

284-
func (q *fakeQuerier) GetWorkspacesByProjectAndUserID(_ context.Context, arg database.GetWorkspacesByProjectAndUserIDParams) ([]database.Workspace, error) {
285-
q.mutex.Lock()
286-
defer q.mutex.Unlock()
287-
288-
workspaces := make([]database.Workspace, 0)
289-
for _, workspace := range q.workspace {
290-
if workspace.OwnerID != arg.OwnerID {
291-
continue
292-
}
293-
if workspace.ProjectID.String() != arg.ProjectID.String() {
294-
continue
295-
}
296-
workspaces = append(workspaces, workspace)
297-
}
298-
if len(workspaces) == 0 {
299-
return nil, sql.ErrNoRows
300-
}
301-
return workspaces, nil
302-
}
303-
304284
func (q *fakeQuerier) GetWorkspacesByUserID(_ context.Context, ownerID string) ([]database.Workspace, error) {
305285
q.mutex.Lock()
306286
defer q.mutex.Unlock()
@@ -475,17 +455,15 @@ func (q *fakeQuerier) GetParameterSchemasByJobID(_ context.Context, jobID uuid.U
475455
return parameters, nil
476456
}
477457

478-
func (q *fakeQuerier) GetProjectsByOrganizationIDs(_ context.Context, ids []string) ([]database.Project, error) {
458+
func (q *fakeQuerier) GetProjectsByOrganization(_ context.Context, id string) ([]database.Project, error) {
479459
q.mutex.Lock()
480460
defer q.mutex.Unlock()
481461

482462
projects := make([]database.Project, 0)
483463
for _, project := range q.project {
484-
for _, id := range ids {
485-
if project.OrganizationID == id {
486-
projects = append(projects, project)
487-
break
488-
}
464+
if project.OrganizationID == id {
465+
projects = append(projects, project)
466+
break
489467
}
490468
}
491469
if len(projects) == 0 {

database/querier.go

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

database/query.sql

+2-11
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ WHERE
157157
LIMIT
158158
1;
159159

160-
-- name: GetProjectsByOrganizationIDs :many
160+
-- name: GetProjectsByOrganization :many
161161
SELECT
162162
*
163163
FROM
164164
project
165165
WHERE
166-
organization_id = ANY(@ids :: text [ ]);
166+
organization_id = $1;
167167

168168
-- name: GetParameterSchemasByJobID :many
169169
SELECT
@@ -287,15 +287,6 @@ WHERE
287287
owner_id = @owner_id
288288
AND LOWER(name) = LOWER(@name);
289289

290-
-- name: GetWorkspacesByProjectAndUserID :many
291-
SELECT
292-
*
293-
FROM
294-
workspace
295-
WHERE
296-
owner_id = $1
297-
AND project_id = $2;
298-
299290
-- name: GetWorkspaceOwnerCountsByProjectIDs :many
300291
SELECT
301292
project_id,

database/query.sql.go

+4-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)