Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/tasks.TaskResponse"
"$ref": "#/definitions/tasks.TaskDetailsResponse"
}
},
"400": {
Expand Down
17 changes: 17 additions & 0 deletions internal/server/dto/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ func ToUserBrief(u *users.User) UserBrief {
}
}

func ResolveUserBrief(id *int64, lookup map[int64]users.User) *UserBrief {
if id == nil {
return nil
}

if u, ok := lookup[*id]; ok {
return lo.ToPtr(ToUserBrief(&u))
}

return &UserBrief{
ID: *id,
Name: "",
Role: "",
CreatedAt: "",
}
}

type UserBriefList struct {
Items []UserBrief `json:"items"`
Total int `json:"total"`
Expand Down
44 changes: 16 additions & 28 deletions internal/server/tasks/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/bit-issues/backend/internal/comments"
"github.com/bit-issues/backend/internal/server/dto"
"github.com/bit-issues/backend/internal/tasks"
"github.com/bit-issues/backend/internal/users"
"github.com/samber/lo"
)

Expand Down Expand Up @@ -104,18 +105,9 @@ type TaskResponse struct {
}

// newTaskResponse converts a domain Task to a TaskResponse DTO.
// It requires fetching author and assignee from the users service.
func newTaskResponse(task *tasks.Task) TaskResponse {
var assignee *dto.UserBrief
if task.AssigneeID != nil {
assignee = &dto.UserBrief{
ID: *task.AssigneeID,
Name: "",
Role: "",
CreatedAt: "",
}
}

// It enriches author and assignee fields from the pre-fetched users map.
// If a user is not found in the map, only the ID is populated (stub).
func newTaskResponse(task *tasks.Task, usersMap map[int64]users.User) TaskResponse {
return TaskResponse{
ID: task.ID,
ProjectSlug: task.ProjectSlug,
Expand All @@ -124,16 +116,11 @@ func newTaskResponse(task *tasks.Task) TaskResponse {
Description: task.Description,
Priority: string(task.Priority),
Status: string(task.Status),
Author: dto.UserBrief{
ID: task.AuthorID,
Name: "",
Role: "",
CreatedAt: "",
},
Assignee: assignee,
DueDate: task.DueDate,
CreatedAt: task.CreatedAt.Format(time.RFC3339),
UpdatedAt: task.UpdatedAt.Format(time.RFC3339),
Author: *dto.ResolveUserBrief(&task.AuthorID, usersMap),
Assignee: dto.ResolveUserBrief(task.AssigneeID, usersMap),
DueDate: task.DueDate,
CreatedAt: task.CreatedAt.Format(time.RFC3339),
UpdatedAt: task.UpdatedAt.Format(time.RFC3339),
}
}

Expand All @@ -146,13 +133,14 @@ type TaskDetailsResponse struct {

func newTaskDetailsResponse(
task *tasks.Task,
usersMap map[int64]users.User,
comments []comments.Comment,
attachmentList []attachments.AttachmentWithURL,
) *TaskDetailsResponse {
return &TaskDetailsResponse{
TaskResponse: newTaskResponse(task),
Comments: toCommentsList(comments),
Attachments: toAttachmentsList(attachmentList),
TaskResponse: newTaskResponse(task, usersMap),
Comments: toCommentsList(comments, usersMap),
Attachments: toAttachmentsList(attachmentList, usersMap),
}
}

Expand All @@ -165,12 +153,12 @@ type TaskListResponse struct {
}

// toTaskListResponse converts a list of domain Tasks to TaskListResponse DTO.
func toTaskListResponse(items []tasks.Task, total int) TaskListResponse {
return TaskListResponse{
func toTaskListResponse(items []tasks.Task, usersMap map[int64]users.User, total int) *TaskListResponse {
return &TaskListResponse{
Items: lo.Map(
items,
func(t tasks.Task, _ int) TaskResponse {
return newTaskResponse(&t)
return newTaskResponse(&t, usersMap)
},
),
Total: total,
Expand Down
27 changes: 11 additions & 16 deletions internal/server/tasks/dto_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/bit-issues/backend/internal/attachments"
"github.com/bit-issues/backend/internal/server/dto"
"github.com/bit-issues/backend/internal/users"
)

type AttachmentUploadRequest struct {
Expand Down Expand Up @@ -50,42 +51,36 @@ func toUploadResponse(result *attachments.UploadResult) AttachmentUploadResponse
}
}

func toConfirmResponse(attachment *attachments.Attachment, downloadURL string) AttachmentConfirmResponse {
func toConfirmResponse(
attachment *attachments.Attachment,
downloadURL string,
usersMap map[int64]users.User,
) AttachmentConfirmResponse {
return AttachmentConfirmResponse{
ID: attachment.ID,
FileName: attachment.FileName,
SizeBytes: attachment.SizeBytes,
DownloadURL: downloadURL,
UploadedAt: attachment.UploadedAt.UTC().Format(time.RFC3339),
UploadedBy: dto.UserBrief{
ID: attachment.UploadedBy,
Name: "",
Role: "",
CreatedAt: "",
},
UploadedBy: *dto.ResolveUserBrief(&attachment.UploadedBy, usersMap),
}
}

func toAttachmentResponse(item attachments.AttachmentWithURL) AttachmentResponse {
func toAttachmentResponse(item attachments.AttachmentWithURL, usersMap map[int64]users.User) AttachmentResponse {
return AttachmentResponse{
ID: item.ID,
FileName: item.FileName,
SizeBytes: item.SizeBytes,
UploadedAt: item.UploadedAt.UTC().Format(time.RFC3339),
DownloadURL: item.DownloadURL,
UploadedBy: dto.UserBrief{
ID: item.UploadedBy,
Name: "",
Role: "",
CreatedAt: "",
},
UploadedBy: *dto.ResolveUserBrief(&item.UploadedBy, usersMap),
}
}

func toAttachmentsList(items []attachments.AttachmentWithURL) []AttachmentResponse {
func toAttachmentsList(items []attachments.AttachmentWithURL, usersMap map[int64]users.User) []AttachmentResponse {
result := make([]AttachmentResponse, 0, len(items))
for _, item := range items {
result = append(result, toAttachmentResponse(item))
result = append(result, toAttachmentResponse(item, usersMap))
}

return result
Expand Down
18 changes: 8 additions & 10 deletions internal/server/tasks/dto_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/bit-issues/backend/internal/comments"
"github.com/bit-issues/backend/internal/server/dto"
"github.com/bit-issues/backend/internal/users"
)

// CommentCreateRequest represents the request body for creating a new comment.
Expand Down Expand Up @@ -33,26 +34,23 @@ type CommentResponse struct {
}

// toCommentResponse converts a domain Comment to an API response.
func toCommentResponse(comment *comments.Comment) CommentResponse {
// It enriches the author field from the pre-fetched users map.
// If the user is not found, only the ID is populated (stub).
func toCommentResponse(comment *comments.Comment, usersMap map[int64]users.User) CommentResponse {
return CommentResponse{
ID: comment.ID,
Author: dto.UserBrief{
ID: comment.AuthorID,
Name: "",
Role: "",
CreatedAt: "",
},
ID: comment.ID,
Author: *dto.ResolveUserBrief(&comment.AuthorID, usersMap),
Content: comment.Content,
CreatedAt: comment.CreatedAt.UTC().Format(time.RFC3339),
UpdatedAt: comment.UpdatedAt.UTC().Format(time.RFC3339),
}
}

// toCommentsList converts a list of comments to an API response.
func toCommentsList(items []comments.Comment) []CommentResponse {
func toCommentsList(items []comments.Comment, usersMap map[int64]users.User) []CommentResponse {
comments := make([]CommentResponse, 0, len(items))
for _, item := range items {
comments = append(comments, toCommentResponse(&item))
comments = append(comments, toCommentResponse(&item, usersMap))
}

return comments
Expand Down
Loading
Loading