Skip to content

Commit 583fe04

Browse files
authored
perf: replace preload with joins #19
1 parent 0915237 commit 583fe04

4 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/services/category_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (service *CategoryService) Create(input *types.CreateCategory, output *type
2525
func (service *CategoryService) Find(name string, output *types.Category) error {
2626
return service.DB.
2727
Table(service.TABLE).
28-
Where("name = ?", name).
28+
Where("categories.name = ?", name).
2929
First(output).
3030
Error
3131
}

src/services/event_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (service *EventService) FindAllForUser(user *types.User, output *[]types.Ev
123123
Table(service.TABLE).
124124
Joins("CreatedBy").
125125
Joins("ModifiedBy").
126-
Where("created_by_id = ?", user_id).
126+
Where("events.created_by_id = ?", user_id).
127127
Find(output).
128128
Error
129129
}

src/services/product_service.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (service *ProductService) Find(key string, output *types.Product) error {
5353
id, _ := uuid.Parse(key)
5454
return service.DB.
5555
Table(service.TABLE).
56-
Preload("Category").
56+
Joins("Category").
5757
Where("products.product_key = ? OR products.id = ?", key, id).
5858
First(output).
5959
Error
@@ -128,21 +128,21 @@ func (service *ProductService) Search(filter types.ProductFilter) (*[]types.Prod
128128

129129
if filter.MinPrice > 0 || filter.MaxPrice > 0 {
130130
query.
131-
Where("price BETWEEN ? AND ?", filter.MinPrice, filter.MaxPrice)
131+
Where("products.price BETWEEN ? AND ?", filter.MinPrice, filter.MaxPrice)
132132
}
133133

134134
switch filter.Sort {
135135
case "rating":
136-
query.Order("rating DESC")
136+
query.Order("products.rating DESC")
137137
case "price":
138-
query.Order("price DESC")
138+
query.Order("products.price DESC")
139139
case "totalReviews":
140-
query.Order("total_reviews DESC")
140+
query.Order("products.total_reviews DESC")
141141
default:
142-
query.Order("updated_at DESC")
142+
query.Order("products.updated_at DESC")
143143
}
144144
err := query.
145-
Preload("Category").
145+
Joins("Category").
146146
Find(products).
147147
Error
148148
return products, err

src/services/user_service.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ type UserService struct {
1111
func (service *UserService) FindByEmail(email string, output *types.User) error {
1212
return service.DB.
1313
Table(service.TABLE).
14-
Where("email = ?", email).
14+
Where("users.email = ?", email).
1515
First(output).
1616
Error
1717
}
1818

1919
func (service *UserService) FindById(id string, output *types.User) error {
2020
return service.DB.
2121
Table(service.TABLE).
22-
Where("id = ?", id).
22+
Where("users.id = ?", id).
2323
First(output).
2424
Error
2525
}
2626

2727
func (service *UserService) FindByIdAndEmail(id string, email string, output *types.User) error {
2828
return service.DB.
2929
Table(service.TABLE).
30-
Where("id = ? AND email = ?", id, email).
30+
Where("users.id = ? AND users.email = ?", id, email).
3131
First(output).
3232
Error
3333
}
3434

3535
func (service *UserService) FindByIdOrEmail(id string, email string, output *types.User) error {
3636
return service.DB.
3737
Table(service.TABLE).
38-
Where("id = ? OR email = ?", id, email).
38+
Where("users.id = ? OR users.email = ?", id, email).
3939
First(output).
4040
Error
4141
}
@@ -69,7 +69,7 @@ func (service *UserService) Create(input *types.CreateUser, output *types.User)
6969
func (service *UserService) DeleteById(key string) error {
7070
return service.DB.
7171
Table(service.TABLE).
72-
Where("id = ?", key).
72+
Where("users.id = ?", key).
7373
Delete(&types.User{}).
7474
Error
7575
}

0 commit comments

Comments
 (0)