Skip to content

Commit

Permalink
Add commenting on retro brainstorm items (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenWeathers authored Sep 2, 2024
1 parent 68c168a commit b5fc179
Show file tree
Hide file tree
Showing 11 changed files with 467 additions and 56 deletions.
35 changes: 32 additions & 3 deletions docs/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9706,6 +9706,9 @@ const docTemplate = `{
"thunderdome.RetroActionComment": {
"type": "object",
"properties": {
"action_id": {
"type": "string"
},
"comment": {
"type": "string"
},
Expand All @@ -9715,9 +9718,6 @@ const docTemplate = `{
"id": {
"type": "string"
},
"retro_id": {
"type": "string"
},
"updated_date": {
"type": "string"
},
Expand All @@ -9740,6 +9740,12 @@ const docTemplate = `{
"thunderdome.RetroItem": {
"type": "object",
"properties": {
"comments": {
"type": "array",
"items": {
"$ref": "#/definitions/thunderdome.RetroItemComment"
}
},
"content": {
"type": "string"
},
Expand All @@ -9757,6 +9763,29 @@ const docTemplate = `{
}
}
},
"thunderdome.RetroItemComment": {
"type": "object",
"properties": {
"comment": {
"type": "string"
},
"created_date": {
"type": "string"
},
"id": {
"type": "string"
},
"item_id": {
"type": "string"
},
"updated_date": {
"type": "string"
},
"user_id": {
"type": "string"
}
}
},
"thunderdome.RetroUser": {
"type": "object",
"properties": {
Expand Down
35 changes: 32 additions & 3 deletions docs/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -9697,6 +9697,9 @@
"thunderdome.RetroActionComment": {
"type": "object",
"properties": {
"action_id": {
"type": "string"
},
"comment": {
"type": "string"
},
Expand All @@ -9706,9 +9709,6 @@
"id": {
"type": "string"
},
"retro_id": {
"type": "string"
},
"updated_date": {
"type": "string"
},
Expand All @@ -9731,6 +9731,12 @@
"thunderdome.RetroItem": {
"type": "object",
"properties": {
"comments": {
"type": "array",
"items": {
"$ref": "#/definitions/thunderdome.RetroItemComment"
}
},
"content": {
"type": "string"
},
Expand All @@ -9748,6 +9754,29 @@
}
}
},
"thunderdome.RetroItemComment": {
"type": "object",
"properties": {
"comment": {
"type": "string"
},
"created_date": {
"type": "string"
},
"id": {
"type": "string"
},
"item_id": {
"type": "string"
},
"updated_date": {
"type": "string"
},
"user_id": {
"type": "string"
}
}
},
"thunderdome.RetroUser": {
"type": "object",
"properties": {
Expand Down
23 changes: 21 additions & 2 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -879,14 +879,14 @@ definitions:
type: object
thunderdome.RetroActionComment:
properties:
action_id:
type: string
comment:
type: string
created_date:
type: string
id:
type: string
retro_id:
type: string
updated_date:
type: string
user_id:
Expand All @@ -901,6 +901,10 @@ definitions:
type: object
thunderdome.RetroItem:
properties:
comments:
items:
$ref: '#/definitions/thunderdome.RetroItemComment'
type: array
content:
type: string
groupId:
Expand All @@ -912,6 +916,21 @@ definitions:
userId:
type: string
type: object
thunderdome.RetroItemComment:
properties:
comment:
type: string
created_date:
type: string
id:
type: string
item_id:
type: string
updated_date:
type: string
user_id:
type: string
type: object
thunderdome.RetroUser:
properties:
active:
Expand Down
16 changes: 16 additions & 0 deletions internal/db/migrations/20240902102923_add_retro_item_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS thunderdome.retro_item_comment (
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
item_id uuid REFERENCES thunderdome.retro_item(id) ON DELETE CASCADE,
user_id uuid REFERENCES thunderdome.users(id) ON DELETE CASCADE,
comment text,
created_date timestamp with time zone DEFAULT now(),
updated_date timestamp with time zone DEFAULT now()
);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS thunderdome.retro_item_comment;
-- +goose StatementEnd
68 changes: 65 additions & 3 deletions internal/db/retro/item.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package retro

import (
"encoding/json"
"errors"
"fmt"

Expand Down Expand Up @@ -71,16 +72,32 @@ func (d *Service) GetRetroItems(RetroID string) []*thunderdome.RetroItem {
var items = make([]*thunderdome.RetroItem, 0)

itemRows, itemsErr := d.DB.Query(
`SELECT id, user_id, group_id, content, type FROM thunderdome.retro_item WHERE retro_id = $1 ORDER BY created_date ASC;`,
`SELECT
ri.id, ri.user_id, ri.group_id, ri.content, ri.type,
COALESCE(
json_agg(rc ORDER BY rc.created_date) FILTER (WHERE rc.id IS NOT NULL), '[]'
) AS comments
FROM thunderdome.retro_item ri
LEFT JOIN thunderdome.retro_item_comment rc ON rc.item_id = ri.id
WHERE ri.retro_id = $1
GROUP BY ri.id, ri.created_date
ORDER BY ri.created_date ASC;`,
RetroID,
)
if itemsErr == nil {
defer itemRows.Close()
for itemRows.Next() {
var ri = &thunderdome.RetroItem{}
if err := itemRows.Scan(&ri.ID, &ri.UserID, &ri.GroupID, &ri.Content, &ri.Type); err != nil {
var comments string
var ri = &thunderdome.RetroItem{
Comments: make([]*thunderdome.RetroItemComment, 0),
}
if err := itemRows.Scan(&ri.ID, &ri.UserID, &ri.GroupID, &ri.Content, &ri.Type, &comments); err != nil {
d.Logger.Error("get retro items query scan error", zap.Error(err))
} else {
jsonErr := json.Unmarshal([]byte(comments), &ri.Comments)
if jsonErr != nil {
d.Logger.Error("retro item comments json error", zap.Error(jsonErr))
}
items = append(items, ri)
}
}
Expand Down Expand Up @@ -216,3 +233,48 @@ func (d *Service) GroupUserSubtractVote(RetroID string, GroupID string, UserID s

return votes, nil
}

// ItemCommentAdd adds a comment to a retro item
func (d *Service) ItemCommentAdd(RetroID string, ItemID string, UserID string, Comment string) ([]*thunderdome.RetroItem, error) {
if _, err := d.DB.Exec(
`INSERT INTO thunderdome.retro_item_comment (item_id, user_id, comment) VALUES ($1, $2, $3);`,
ItemID,
UserID,
Comment,
); err != nil {
d.Logger.Error("ItemCommentAdd error", zap.Error(err))
}

items := d.GetRetroItems(RetroID)

return items, nil
}

// ItemCommentEdit edits a retro item comment
func (d *Service) ItemCommentEdit(RetroID string, CommentID string, Comment string) ([]*thunderdome.RetroItem, error) {
if _, err := d.DB.Exec(
`UPDATE thunderdome.retro_item_comment SET comment = $2 WHERE id = $1;`,
CommentID,
Comment,
); err != nil {
d.Logger.Error("ItemCommentEdit error", zap.Error(err))
}

items := d.GetRetroItems(RetroID)

return items, nil
}

// ItemCommentDelete deletes a retro item comment
func (d *Service) ItemCommentDelete(RetroID string, CommentID string) ([]*thunderdome.RetroItem, error) {
if _, err := d.DB.Exec(
`DELETE FROM thunderdome.retro_item_comment WHERE id = $1;`,
CommentID,
); err != nil {
d.Logger.Error("ItemCommentDelete error", zap.Error(err))
}

items := d.GetRetroItems(RetroID)

return items, nil
}
65 changes: 65 additions & 0 deletions internal/http/retro/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,71 @@ func (b *Service) CreateItem(ctx context.Context, RetroID string, UserID string,
return msg, nil, false
}

// ItemCommentAdd creates a retro item comment
func (b *Service) ItemCommentAdd(ctx context.Context, RetroID string, UserID string, EventValue string) ([]byte, error, bool) {
var rs struct {
ItemID string `json:"item_id"`
Comment string `json:"comment"`
}
err := json.Unmarshal([]byte(EventValue), &rs)
if err != nil {
return nil, err, false
}

items, err := b.RetroService.ItemCommentAdd(RetroID, rs.ItemID, UserID, rs.Comment)
if err != nil {
return nil, err, false
}

updatedItems, _ := json.Marshal(items)
msg := createSocketEvent("items_updated", string(updatedItems), "")

return msg, nil, false
}

// ItemCommentEdit updates a retro item comment
func (b *Service) ItemCommentEdit(ctx context.Context, RetroID string, UserID string, EventValue string) ([]byte, error, bool) {
var rs struct {
CommentID string `json:"comment_id"`
Comment string `json:"comment"`
}
err := json.Unmarshal([]byte(EventValue), &rs)
if err != nil {
return nil, err, false
}

items, err := b.RetroService.ItemCommentEdit(RetroID, rs.CommentID, rs.Comment)
if err != nil {
return nil, err, false
}

updatedItems, _ := json.Marshal(items)
msg := createSocketEvent("items_updated", string(updatedItems), "")

return msg, nil, false
}

// ItemCommentDelete deletes a retro item comment
func (b *Service) ItemCommentDelete(ctx context.Context, RetroID string, UserID string, EventValue string) ([]byte, error, bool) {
var rs struct {
CommentID string `json:"comment_id"`
}
err := json.Unmarshal([]byte(EventValue), &rs)
if err != nil {
return nil, err, false
}

items, err := b.RetroService.ItemCommentDelete(RetroID, rs.CommentID)
if err != nil {
return nil, err, false
}

updatedItems, _ := json.Marshal(items)
msg := createSocketEvent("items_updated", string(updatedItems), "")

return msg, nil, false
}

// UserMarkReady marks a user as ready to advance to next phase
func (b *Service) UserMarkReady(ctx context.Context, RetroID string, UserID string, EventValue string) ([]byte, error, bool) {
readyUsers, err := b.RetroService.MarkUserReady(RetroID, UserID)
Expand Down
3 changes: 3 additions & 0 deletions internal/http/retro/retro.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func New(
"group_vote": rs.GroupUserVote,
"group_vote_subtract": rs.GroupUserSubtractVote,
"delete_item": rs.DeleteItem,
"item_comment_add": rs.ItemCommentAdd,
"item_comment_edit": rs.ItemCommentEdit,
"item_comment_delete": rs.ItemCommentDelete,
"create_action": rs.CreateAction,
"update_action": rs.UpdateAction,
"delete_action": rs.DeleteAction,
Expand Down
Loading

0 comments on commit b5fc179

Please sign in to comment.