Skip to content

Commit b79f044

Browse files
committed
style(playlist): abstract DTO creation
1 parent 7e7d945 commit b79f044

File tree

1 file changed

+23
-42
lines changed

1 file changed

+23
-42
lines changed

pkg/api/playlist.go

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,40 @@ func GetPlaylist(c *middleware.Context) Response {
5555
return ApiError(500, "Playlist not found", err)
5656
}
5757

58-
itemQuery := m.GetPlaylistItemsByIdQuery{PlaylistId: id}
59-
if err := bus.Dispatch(&itemQuery); err != nil {
60-
log.Warn("itemQuery failed: %v", err)
61-
return ApiError(500, "Playlist items not found", err)
58+
playlistDTOs, _ := LoadPlaylistItemDTOs(id)
59+
60+
dto := &m.PlaylistDTO{
61+
Id: cmd.Result.Id,
62+
Title: cmd.Result.Title,
63+
Timespan: cmd.Result.Timespan,
64+
OrgId: cmd.Result.OrgId,
65+
Items: playlistDTOs,
66+
}
67+
68+
return Json(200, dto)
69+
}
70+
71+
func LoadPlaylistItemDTOs(id int64) ([]m.PlaylistItemDTO, error) {
72+
playlistitems, err := LoadPlaylistItems(id)
73+
74+
if err != nil {
75+
return nil, err
6276
}
6377

6478
playlistDTOs := make([]m.PlaylistItemDTO, 0)
6579

66-
for _, item := range *itemQuery.Result {
80+
for _, item := range playlistitems {
6781
playlistDTOs = append(playlistDTOs, m.PlaylistItemDTO{
6882
Id: item.Id,
6983
PlaylistId: item.PlaylistId,
7084
Type: item.Type,
7185
Value: item.Value,
7286
Order: item.Order,
87+
Title: item.Title,
7388
})
7489
}
7590

76-
dto := &m.PlaylistDTO{
77-
Id: cmd.Result.Id,
78-
Title: cmd.Result.Title,
79-
Timespan: cmd.Result.Timespan,
80-
OrgId: cmd.Result.OrgId,
81-
Items: playlistDTOs,
82-
}
83-
84-
return Json(200, dto)
91+
return playlistDTOs, nil
8592
}
8693

8794
func LoadPlaylistItems(id int64) ([]m.PlaylistItem, error) {
@@ -130,25 +137,12 @@ func LoadPlaylistDashboards(id int64) ([]m.PlaylistDashboardDto, error) {
130137
func GetPlaylistItems(c *middleware.Context) Response {
131138
id := c.ParamsInt64(":id")
132139

133-
items, err := LoadPlaylistItems(id)
140+
playlistDTOs, err := LoadPlaylistItemDTOs(id)
134141

135142
if err != nil {
136143
return ApiError(500, "Could not load playlist items", err)
137144
}
138145

139-
playlistDTOs := make([]m.PlaylistItemDTO, 0)
140-
141-
for _, item := range items {
142-
playlistDTOs = append(playlistDTOs, m.PlaylistItemDTO{
143-
Id: item.Id,
144-
PlaylistId: item.PlaylistId,
145-
Type: item.Type,
146-
Value: item.Value,
147-
Order: item.Order,
148-
Title: item.Title,
149-
})
150-
}
151-
152146
return Json(200, playlistDTOs)
153147
}
154148

@@ -190,20 +184,7 @@ func UpdatePlaylist(c *middleware.Context, query m.UpdatePlaylistQuery) Response
190184
return ApiError(500, "Failed to save playlist", err)
191185
}
192186

193-
items, err := LoadPlaylistItems(query.Id)
194-
195-
playlistDTOs := make([]m.PlaylistItemDTO, 0)
196-
197-
for _, item := range items {
198-
playlistDTOs = append(playlistDTOs, m.PlaylistItemDTO{
199-
Id: item.Id,
200-
PlaylistId: item.PlaylistId,
201-
Type: item.Type,
202-
Value: item.Value,
203-
Order: item.Order,
204-
})
205-
}
206-
187+
playlistDTOs, err := LoadPlaylistItemDTOs(query.Id)
207188
if err != nil {
208189
return ApiError(500, "Failed to save playlist", err)
209190
}

0 commit comments

Comments
 (0)