Skip to content

Commit

Permalink
Return assignments from the API
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoQuaresma committed Aug 11, 2024
1 parent ad3b5d0 commit 9b30ecf
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 103 deletions.
4 changes: 2 additions & 2 deletions api/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestDeleteComment_FailWhenUserIsNotAdminOrOwner(t *testing.T) {
require.NoError(t, err, "error making request")
require.Equal(t, http.StatusCreated, httpRes.StatusCode)

member := testutil.NewMember(t, &sdk)
member, _ := testutil.NewMember(t, &sdk)
memberSdk := tEnv.AuthSDK(member.Email, member.Password)

httpRes, err = memberSdk.DeleteComment(ticketRes.Data.ID, commentRes.Data.ID)
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestPatchComment_FailWhenUserIsNotAdminOrOwner(t *testing.T) {
require.NoError(t, err, "error making request")
require.Equal(t, http.StatusCreated, httpRes.StatusCode)

member := testutil.NewMember(t, &sdk)
member, _ := testutil.NewMember(t, &sdk)
memberSdk := tEnv.AuthSDK(member.Email, member.Password)

newContent := gofakeit.Sentence(10)
Expand Down
3 changes: 2 additions & 1 deletion api/database/migrations/000002_create_tickets_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ CREATE TABLE IF NOT EXISTS tickets (
CREATE TABLE IF NOT EXISTS ticket_labels (
ticket_id INTEGER REFERENCES tickets (id) ON DELETE CASCADE,
label_id INTEGER REFERENCES labels (id) ON DELETE CASCADE,
PRIMARY KEY (ticket_id, label_id)
PRIMARY KEY (ticket_id, label_id),
UNIQUE (ticket_id, label_id)
);
12 changes: 11 additions & 1 deletion api/database/queries/assignments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ RETURNING *;

-- name: DeleteAssignment :exec
DELETE FROM assignments
WHERE id = $1;
WHERE id = $1;

-- name: DeleteAssignmentByTicketIDAndUserID :exec
DELETE FROM assignments
WHERE ticket_id = $1 AND user_id = $2;

-- name: GetAssignmentsByTicketID :many
SELECT assignments.*, sqlc.embed(users)
FROM assignments
JOIN users ON assignments.user_id = users.id
WHERE ticket_id = $1;
15 changes: 8 additions & 7 deletions api/database/queries/labels.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-- name: AssignLabelToTicket :exec
WITH label AS (
INSERT INTO ticket_labels (ticket_id, label_id)
VALUES (
@ticket_id,
(
SELECT id
FROM labels
WHERE name = @label_name
)
INSERT INTO ticket_labels (ticket_id, label_id)
SELECT @ticket_id, id
FROM label
RETURNING *;
)
);

-- name: UnassignLabelFromTicket :exec
DELETE FROM ticket_labels
Expand All @@ -24,4 +24,5 @@ SELECT * FROM labels;
-- name: CreateLabel :one
INSERT INTO labels (name, created_by)
VALUES ($1, $2)
RETURNING *;
RETURNING *;

20 changes: 9 additions & 11 deletions api/database/queries/tickets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ INSERT INTO tickets (title, created_by)
VALUES ($1, $2)
RETURNING *;

-- name: GetTicketsByIDs :many
SELECT tickets.*, sqlc.embed(users), array_agg(labels.name)::text[] AS labels
FROM tickets
JOIN ticket_labels ON tickets.id = ticket_labels.ticket_id
JOIN labels ON ticket_labels.label_id = labels.id
JOIN users ON tickets.created_by = users.id
WHERE tickets.id = ANY(@ids::int[])
GROUP BY tickets.id, users.id;

-- name: GetTicketByID :one
SELECT tickets.*, sqlc.embed(users), array_remove(array_agg(labels.name), NULL)::text[] AS labels
SELECT
tickets.*,
sqlc.embed(users),
array_remove(array_agg(DISTINCT labels.name), NULL)::text[] AS labels,
array_remove(array_agg(DISTINCT assignments.user_id), NULL)::integer[] AS assigned_to
FROM tickets
LEFT JOIN ticket_labels ON tickets.id = ticket_labels.ticket_id
LEFT JOIN labels ON ticket_labels.label_id = labels.id
LEFT JOIN users ON tickets.created_by = users.id
LEFT JOIN assignments ON tickets.id = assignments.ticket_id
WHERE tickets.id = @id
GROUP BY tickets.id, users.id
LIMIT 1;
Expand All @@ -42,11 +38,13 @@ RETURNING *;
SELECT
tickets.*,
sqlc.embed(users),
array_remove(array_agg(labels.name), NULL)::text[] AS labels
array_remove(array_agg(DISTINCT labels.name), NULL)::text[] AS labels,
array_remove(array_agg(DISTINCT assignments.user_id), NULL)::integer[] AS assigned_to
FROM tickets
LEFT JOIN ticket_labels ON tickets.id = ticket_labels.ticket_id
LEFT JOIN labels ON ticket_labels.label_id = labels.id
LEFT JOIN users ON tickets.created_by = users.id
LEFT JOIN assignments ON tickets.id = assignments.ticket_id
WHERE
CASE
WHEN @title::text != '' THEN
Expand Down
4 changes: 2 additions & 2 deletions api/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
)

func NewMember(t *testing.T, sdk *sdk.Client) api.CreateUserRequest {
func NewMember(t *testing.T, sdk *sdk.Client) (api.CreateUserRequest, api.CreateUserResponse) {
req := api.CreateUserRequest{
Name: gofakeit.Name(),
Username: gofakeit.Username(),
Expand All @@ -25,7 +25,7 @@ func NewMember(t *testing.T, sdk *sdk.Client) api.CreateUserRequest {
require.NoError(t, err, "error making request")
require.Equal(t, http.StatusCreated, httpRes.StatusCode)

return req
return req, res
}

func RequireValidationError(t *testing.T, errors []api.ValidationError, field string, validator string) {
Expand Down
Loading

0 comments on commit 9b30ecf

Please sign in to comment.