Skip to content

Commit 6876bf2

Browse files
committed
look πŸ‘€ at the 😲 stars 🀩✨⭐, look how they shine 🌟 for you 🫡🏿
1 parent 69242d1 commit 6876bf2

File tree

8 files changed

+147
-34
lines changed

8 files changed

+147
-34
lines changed

β€Žinternal/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewHandler(ctx context.Context, backend *backend.Backend) *gin.Engine {
3939

4040
r.GET("/sounds", h.ListSamples)
4141
r.GET("/sounds/search", h.SearchSamples)
42-
r.GET("/sounds/tags", h.ListTags)
42+
r.GET("/sounds/tags", h.ListTagsAndCategories)
4343
r.GET("/sounds/categories", h.ListCategories)
4444
r.GET("/sounds/categories/:id/tags", h.ListCategoryTags)
4545
r.GET("/sounds/users/:id", h.ListUserSamples)

β€Žinternal/api/create_sample.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (h *handler) CreateSample(c *gin.Context) {
7878
var input CreateSampleInput
7979

8080
if err := c.Bind(&input); err != nil {
81+
log.Err(err).Msg(err.Error())
8182
c.JSON(http.StatusBadRequest, M{
8283
"message": "invalid request body",
8384
})
@@ -170,7 +171,7 @@ func (h *handler) CreateSample(c *gin.Context) {
170171
}
171172

172173
c.JSON(http.StatusCreated, M{
173-
"message": "sample created",
174-
"sampleID": sampleID,
174+
"message": "sample created",
175+
"sample_id": sampleID,
175176
})
176177
}

β€Žinternal/api/list_tags.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

β€Žinternal/api/tags.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
func (h *handler) ListTags(c *gin.Context) {}
10+
11+
func (h *handler) ListTagsAndCategories(c *gin.Context) {
12+
result, err := h.backend.ListTagsAndCategories(c.Request.Context())
13+
if err != nil {
14+
c.JSON(http.StatusInternalServerError, M{
15+
"message": "internal server error",
16+
})
17+
return
18+
}
19+
20+
c.JSON(http.StatusOK, M{
21+
"message": "success",
22+
"items": result,
23+
"total_items": len(result),
24+
})
25+
}

β€Žinternal/backend/tags.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
)
6+
7+
type ListTagsAndCategoriesOutputTag struct {
8+
ID int `json:"id"`
9+
Name string `json:"name"`
10+
}
11+
12+
type ListTagsAndCategoriesOutput struct {
13+
Category string `json:"category"`
14+
Tags []ListTagsAndCategoriesOutputTag `json:"tags"`
15+
}
16+
17+
func (b *Backend) ListTagsAndCategories(ctx context.Context) ([]ListTagsAndCategoriesOutput, error) {
18+
result, err := b.repo.ListTagsAndCategories(ctx)
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
var output []ListTagsAndCategoriesOutput
24+
25+
for _, item := range result {
26+
var tag ListTagsAndCategoriesOutputTag
27+
28+
tag.ID = item.TagID
29+
tag.Name = item.TagName
30+
31+
var category ListTagsAndCategoriesOutput
32+
33+
category.Category = item.CategoryName
34+
category.Tags = append(category.Tags, tag)
35+
36+
output = append(output, category)
37+
}
38+
39+
// TODO: OPTIMIZE THIS BULLSHIT
40+
for i := 0; i < len(output); i++ {
41+
for j := i + 1; j < len(output); j++ {
42+
if output[i].Category == output[j].Category {
43+
output[i].Tags = append(output[i].Tags, output[j].Tags...)
44+
output = append(output[:j], output[j+1:]...)
45+
j--
46+
}
47+
}
48+
}
49+
50+
return output, nil
51+
}

β€Žinternal/repository/list_samples.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (r *Repository) ListSamples(ctx context.Context) ([]ListSamplesResult, erro
3030
GROUP BY
3131
samples.id,
3232
users.username
33-
ORDER BY sold DESC;`
33+
ORDER BY samples.created_at DESC;`
3434

3535
rows, err := r.db.Query(ctx, query)
3636
if err != nil {

β€Žinternal/repository/tags.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,43 @@ func (r *Repository) ListTags(ctx context.Context) ([]model.Tag, error) {
4242

4343
return tags, nil
4444
}
45+
46+
type ListTagsAndCategoriesResult struct {
47+
TagID int `json:"tag_id"`
48+
TagName string `json:"tag_name"`
49+
CategoryID int `json:"category_id"`
50+
CategoryName string `json:"category_name"`
51+
}
52+
53+
func (r *Repository) ListTagsAndCategories(ctx context.Context) ([]ListTagsAndCategoriesResult, error) {
54+
var result []ListTagsAndCategoriesResult
55+
56+
query := `
57+
SELECT
58+
tags.id,
59+
tags.name,
60+
categories.id,
61+
categories.name
62+
FROM
63+
tags
64+
LEFT JOIN categories ON tags.category_id = categories.id;
65+
`
66+
67+
rows, err := r.db.Query(ctx, query)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
for rows.Next() {
73+
var row ListTagsAndCategoriesResult
74+
75+
err := rows.Scan(&row.TagID, &row.TagName, &row.CategoryID, &row.CategoryName)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
result = append(result, row)
81+
}
82+
83+
return result, nil
84+
}

β€Žtunema-rds.session.sql

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@
4949
-- GROUP BY
5050
-- samples.id,
5151
-- users.username;
52-
-- SELECT
53-
-- tags.id AS tag_id,
54-
-- tags.name AS tag_name,
55-
-- categories.name AS category_name
56-
-- FROM
57-
-- tags
58-
-- LEFT JOIN categories ON tags.category_id = categories.id;
52+
SELECT
53+
tags.id AS tag_id,
54+
tags.name AS tag_name,
55+
categories.name AS category_name
56+
FROM
57+
tags
58+
LEFT JOIN categories ON tags.category_id = categories.id;
59+
5960
-- SELECT
6061
-- samples.*,
6162
-- users.username AS artist_name,
@@ -82,21 +83,21 @@
8283
-- LEFT JOIN order_products ON order_products.sample_id = samples.id
8384
-- WHERE
8485
-- samples.user_id = 1
85-
SELECT
86-
samples.*,
87-
users.username AS artist_name,
88-
ARRAY_AGG(tags.name) AS tags,
89-
COUNT(order_products.sample_id) AS sold
90-
FROM
91-
samples
92-
LEFT JOIN users ON samples.user_id = users.id
93-
LEFT JOIN sample_tags ON samples.id = sample_tags.sample_id
94-
LEFT JOIN tags ON sample_tags.tag_id = tags.id
95-
LEFT JOIN order_products ON order_products.sample_id = samples.id
96-
WHERE
97-
samples.user_id = 2
98-
GROUP BY
99-
samples.id,
100-
users.username
101-
ORDER BY
102-
created_at DESC
86+
-- SELECT
87+
-- samples.*,
88+
-- users.username AS artist_name,
89+
-- ARRAY_AGG(tags.name) AS tags,
90+
-- COUNT(order_products.sample_id) AS sold
91+
-- FROM
92+
-- samples
93+
-- LEFT JOIN users ON samples.user_id = users.id
94+
-- LEFT JOIN sample_tags ON samples.id = sample_tags.sample_id
95+
-- LEFT JOIN tags ON sample_tags.tag_id = tags.id
96+
-- LEFT JOIN order_products ON order_products.sample_id = samples.id
97+
-- WHERE
98+
-- samples.user_id = 2
99+
-- GROUP BY
100+
-- samples.id,
101+
-- users.username
102+
-- ORDER BY
103+
-- created_at DESC

0 commit comments

Comments
Β (0)