Skip to content

Commit f5e3ad0

Browse files
committed
fix: use dto in service
1 parent cea11ee commit f5e3ad0

File tree

7 files changed

+145
-158
lines changed

7 files changed

+145
-158
lines changed

app/events/repository/get_event.go

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,10 @@ import (
66
"github.com/hammer-code/lms-be/domain"
77
)
88

9-
func (repo *repository) GetEvent(ctx context.Context, eventID uint) (dataDTO domain.EventDTO, err error) {
9+
func (repo *repository) GetEvent(ctx context.Context, eventID uint) (data domain.Event, err error) {
1010
db := repo.db.DB(ctx).Model(&domain.Event{})
1111

12-
data := domain.Event{}
13-
1412
err = db.Where("id = ?", eventID).Preload("Tags").Preload("Speakers").Preload("Author").First(&data).Error
1513

16-
if err == nil {
17-
dataDTO = domain.EventDTO{
18-
ID: data.ID,
19-
Title: data.Title,
20-
Description: data.Description,
21-
Slug: data.Slug,
22-
Image: data.Image,
23-
Date: data.Date,
24-
Type: data.Type,
25-
Location: data.Location,
26-
Duration: data.Duration,
27-
Capacity: data.Capacity,
28-
Status: data.Status,
29-
Tags: data.Tags,
30-
Speakers: data.Speakers,
31-
SessionType: data.SessionType,
32-
RegistrationLink: data.RegistrationLink,
33-
Price: data.Price,
34-
ReservationStartDate: data.ReservationStartDate,
35-
ReservationEndDate: data.ReservationEndDate,
36-
Author: data.Author.Username,
37-
CreatedAt: data.CreatedAt,
38-
UpdatedAt: data.UpdatedAt,
39-
DeletedAt: data.DeletedAt,
40-
}
41-
}
42-
43-
return dataDTO, err
14+
return data, err
4415
}

app/events/repository/get_events.go

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
"github.com/hammer-code/lms-be/domain"
77
)
88

9-
func (repo *repository) GetEvents(ctx context.Context, filter domain.EventFilter) (tData int, dataDTO []domain.EventDTO, err error) {
10-
11-
data := []domain.Event{}
9+
func (repo *repository) GetEvents(ctx context.Context, filter domain.EventFilter) (tData int, data []domain.Event, err error) {
1210

1311
db := repo.db.DB(ctx).Model(&data)
1412

@@ -43,34 +41,5 @@ func (repo *repository) GetEvents(ctx context.Context, filter domain.EventFilter
4341
return
4442
}
4543

46-
if err == nil {
47-
for _, d := range data {
48-
dataDTO = append(dataDTO, domain.EventDTO{
49-
ID: d.ID,
50-
Title: d.Title,
51-
Description: d.Description,
52-
Slug: d.Slug,
53-
Image: d.Image,
54-
Date: d.Date,
55-
Type: d.Type,
56-
Location: d.Location,
57-
Duration: d.Duration,
58-
Capacity: d.Capacity,
59-
Status: d.Status,
60-
Tags: d.Tags,
61-
Speakers: d.Speakers,
62-
SessionType: d.SessionType,
63-
RegistrationLink: d.RegistrationLink,
64-
Price: d.Price,
65-
ReservationStartDate: d.ReservationStartDate,
66-
ReservationEndDate: d.ReservationEndDate,
67-
Author: d.Author.Username,
68-
CreatedAt: d.CreatedAt,
69-
UpdatedAt: d.UpdatedAt,
70-
DeletedAt: d.DeletedAt,
71-
})
72-
}
73-
}
74-
75-
return int(totalData), dataDTO, err
44+
return int(totalData), data, err
7645
}

app/events/usecase/get_event_general.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"github.com/hammer-code/lms-be/utils"
1010
)
1111

12-
func (uc usecase) GetEventByID(ctx context.Context, id uint) (domain.EventDTO, error) {
13-
resp, err := uc.repository.GetEvent(ctx, id)
12+
func (uc usecase) GetEventByID(ctx context.Context, id uint) (resp domain.EventDTO, err error) {
13+
event, err := uc.repository.GetEvent(ctx, id)
1414
if err != nil {
1515
err = utils.NewInternalServerError(ctx, err)
1616
return resp, err
1717
}
1818
baseURL := config.GetConfig().BaseURL
1919

20-
resp.Image = fmt.Sprintf("%s/api/v1/public/storage/images/%s", baseURL, resp.Image)
20+
event.Image = fmt.Sprintf("%s/api/v1/public/storage/images/%s", baseURL, event.Image)
2121
return resp, err
2222
}

app/events/usecase/get_events.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ func (uc usecase) GetEvents(ctx context.Context, filter domain.EventFilter) (res
1818

1919
baseURL := config.GetConfig().BaseURL
2020

21-
for i, data := range datas {
22-
datas[i].Image = fmt.Sprintf("%s/api/v1/public/storage/images/%s", baseURL, data.Image)
21+
for _, data := range datas {
22+
data.Image = fmt.Sprintf("%s/api/v1/public/storage/images/%s", baseURL, data.Image)
23+
resp = append(resp, data.ToDTO())
2324
}
2425

25-
return datas, domain.NewPagination(tData, filter.FilterPagination), err
26+
return resp, domain.NewPagination(tData, filter.FilterPagination), err
2627
}

app/events/usecase/update_event.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func (uc usecase) UpdateEvent(ctx context.Context, id uint, payload domain.Updat
1717
return utils.NewBadRequestError(ctx, "Sorry, invalid event type", errors.New("event type is not valid"))
1818
}
1919

20+
userData := ctx.Value(contextkey.UserKey).(domain.User)
21+
2022
err := uc.repository.UpdateEvent(ctx, domain.Event{
2123
ID: id,
2224
Title: payload.Title,

database/migration/20250818141502_table_change_type_author_on_event.sql

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@ BEGIN
1010
SELECT 1 FROM information_schema.columns
1111
WHERE table_name='events' AND column_name='author_id'
1212
) THEN
13-
-- Rename and change type to INT if needed
14-
ALTER TABLE events RENAME COLUMN author TO author_id;
15-
ALTER TABLE events ALTER COLUMN author_id TYPE INT USING (author_id::integer);
13+
-- Add the new author_id column first
14+
ALTER TABLE events ADD COLUMN author_id INT;
15+
16+
-- You'll need to populate author_id based on your business logic
17+
-- For example, if you have a users table:
18+
-- UPDATE events SET author_id = users.id FROM users WHERE events.author = users.name;
19+
20+
-- Or set a default value for now:
21+
-- UPDATE events SET author_id = 1 WHERE author_id IS NULL;
22+
23+
-- Drop the old author column after migration
24+
-- ALTER TABLE events DROP COLUMN author;
25+
1626
ELSIF NOT EXISTS (
1727
SELECT 1 FROM information_schema.columns
1828
WHERE table_name='events' AND column_name='author_id'
@@ -33,6 +43,21 @@ END $$;
3343

3444
-- +goose Down
3545
-- +goose StatementBegin
36-
ALTER TABLE events RENAME COLUMN author_id TO author;
37-
ALTER TABLE events DROP COLUMN IF EXISTS session_type;
38-
-- +goose StatementEnd
46+
DO $$
47+
BEGIN
48+
-- Add back the author column if it doesn't exist
49+
IF NOT EXISTS (
50+
SELECT 1 FROM information_schema.columns
51+
WHERE table_name='events' AND column_name='author'
52+
) THEN
53+
ALTER TABLE events ADD COLUMN author TEXT;
54+
55+
-- You might want to populate it from a users table:
56+
-- UPDATE events SET author = users.name FROM users WHERE events.author_id = users.id;
57+
END IF;
58+
59+
-- Drop the columns we added
60+
ALTER TABLE events DROP COLUMN IF EXISTS author_id;
61+
ALTER TABLE events DROP COLUMN IF EXISTS session_type;
62+
END $$;
63+
-- +goose StatementEnd

0 commit comments

Comments
 (0)