Skip to content

Commit 54fa14c

Browse files
committed
Refactor services
Clean some codes Remove extra comments
1 parent a8d98cf commit 54fa14c

File tree

13 files changed

+103
-127
lines changed

13 files changed

+103
-127
lines changed

src/api/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func RegisterRoutes(r *gin.Engine, cfg *config.Config) {
4949
{
5050
// Test
5151
health := v1.Group("/health")
52-
test_router := v1.Group("/test" /*middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"})*/)
52+
testRouter := v1.Group("/test" /*middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"})*/)
5353

5454
// User
5555
users := v1.Group("/users")
@@ -79,7 +79,7 @@ func RegisterRoutes(r *gin.Engine, cfg *config.Config) {
7979

8080
// Test
8181
routers.Health(health)
82-
routers.TestRouter(test_router)
82+
routers.TestRouter(testRouter)
8383

8484
// User
8585
routers.User(users, cfg)

src/api/middlewares/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Authentication(cfg *config.Config) gin.HandlerFunc {
2222
claimMap := map[string]interface{}{}
2323
auth := c.GetHeader(constants.AuthorizationHeaderKey)
2424
token := strings.Split(auth, " ")
25-
if auth == "" {
25+
if auth == "" || len(token) < 2 {
2626
err = &service_errors.ServiceError{EndUserMessage: service_errors.TokenRequired}
2727
} else {
2828
claimMap, err = tokenService.GetClaims(token[1])

src/api/routers/basic.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"github.com/naeemaei/golang-clean-web-api/config"
77
)
88

9+
const GetByFilterExp string = "/get-by-filter"
910
func Country(r *gin.RouterGroup, cfg *config.Config) {
1011
h := handlers.NewCountryHandler(cfg)
1112

1213
r.POST("/", h.Create)
1314
r.PUT("/:id", h.Update)
1415
r.DELETE("/:id", h.Delete)
1516
r.GET("/:id", h.GetById)
16-
r.POST("/get-by-filter", h.GetByFilter)
17+
r.POST(GetByFilterExp, h.GetByFilter)
1718
}
1819

1920
func City(r *gin.RouterGroup, cfg *config.Config) {
@@ -23,7 +24,7 @@ func City(r *gin.RouterGroup, cfg *config.Config) {
2324
r.PUT("/:id", h.Update)
2425
r.DELETE("/:id", h.Delete)
2526
r.GET("/:id", h.GetById)
26-
r.POST("/get-by-filter", h.GetByFilter)
27+
r.POST(GetByFilterExp, h.GetByFilter)
2728
}
2829

2930
func File(r *gin.RouterGroup, cfg *config.Config) {
@@ -33,7 +34,7 @@ func File(r *gin.RouterGroup, cfg *config.Config) {
3334
r.PUT("/:id", h.Update)
3435
r.DELETE("/:id", h.Delete)
3536
r.GET("/:id", h.GetById)
36-
r.POST("/get-by-filter", h.GetByFilter)
37+
r.POST(GetByFilterExp, h.GetByFilter)
3738
}
3839

3940
func Company(r *gin.RouterGroup, cfg *config.Config) {
@@ -43,7 +44,7 @@ func Company(r *gin.RouterGroup, cfg *config.Config) {
4344
r.PUT("/:id", h.Update)
4445
r.DELETE("/:id", h.Delete)
4546
r.GET("/:id", h.GetById)
46-
r.POST("/get-by-filter", h.GetByFilter)
47+
r.POST(GetByFilterExp, h.GetByFilter)
4748
}
4849

4950
func Color(r *gin.RouterGroup, cfg *config.Config) {
@@ -53,7 +54,7 @@ func Color(r *gin.RouterGroup, cfg *config.Config) {
5354
r.PUT("/:id", h.Update)
5455
r.DELETE("/:id", h.Delete)
5556
r.GET("/:id", h.GetById)
56-
r.POST("/get-by-filter", h.GetByFilter)
57+
r.POST(GetByFilterExp, h.GetByFilter)
5758
}
5859

5960
func Year(r *gin.RouterGroup, cfg *config.Config) {
@@ -63,5 +64,5 @@ func Year(r *gin.RouterGroup, cfg *config.Config) {
6364
r.PUT("/:id", h.Update)
6465
r.DELETE("/:id", h.Delete)
6566
r.GET("/:id", h.GetById)
66-
r.POST("/get-by-filter", h.GetByFilter)
67+
r.POST(GetByFilterExp, h.GetByFilter)
6768
}

src/api/routers/cars.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func CarType(r *gin.RouterGroup, cfg *config.Config) {
1313
r.PUT("/:id", h.Update)
1414
r.DELETE("/:id", h.Delete)
1515
r.GET("/:id", h.GetById)
16-
r.POST("/get-by-filter", h.GetByFilter)
16+
r.POST(GetByFilterExp, h.GetByFilter)
1717
}
1818

1919
func Gearbox(r *gin.RouterGroup, cfg *config.Config) {
@@ -23,7 +23,7 @@ func Gearbox(r *gin.RouterGroup, cfg *config.Config) {
2323
r.PUT("/:id", h.Update)
2424
r.DELETE("/:id", h.Delete)
2525
r.GET("/:id", h.GetById)
26-
r.POST("/get-by-filter", h.GetByFilter)
26+
r.POST(GetByFilterExp, h.GetByFilter)
2727
}
2828

2929
func CarModel(r *gin.RouterGroup, cfg *config.Config) {
@@ -33,7 +33,7 @@ func CarModel(r *gin.RouterGroup, cfg *config.Config) {
3333
r.PUT("/:id", h.Update)
3434
r.DELETE("/:id", h.Delete)
3535
r.GET("/:id", h.GetById)
36-
r.POST("/get-by-filter", h.GetByFilter)
36+
r.POST(GetByFilterExp, h.GetByFilter)
3737
}
3838

3939
func CarModelColor(r *gin.RouterGroup, cfg *config.Config) {
@@ -43,7 +43,7 @@ func CarModelColor(r *gin.RouterGroup, cfg *config.Config) {
4343
r.PUT("/:id", h.Update)
4444
r.DELETE("/:id", h.Delete)
4545
r.GET("/:id", h.GetById)
46-
r.POST("/get-by-filter", h.GetByFilter)
46+
r.POST(GetByFilterExp, h.GetByFilter)
4747
}
4848

4949
func CarModelYear(r *gin.RouterGroup, cfg *config.Config) {
@@ -53,7 +53,7 @@ func CarModelYear(r *gin.RouterGroup, cfg *config.Config) {
5353
r.PUT("/:id", h.Update)
5454
r.DELETE("/:id", h.Delete)
5555
r.GET("/:id", h.GetById)
56-
r.POST("/get-by-filter", h.GetByFilter)
56+
r.POST(GetByFilterExp, h.GetByFilter)
5757
}
5858

5959
func CarModelPriceHistory(r *gin.RouterGroup, cfg *config.Config) {
@@ -63,7 +63,7 @@ func CarModelPriceHistory(r *gin.RouterGroup, cfg *config.Config) {
6363
r.PUT("/:id", h.Update)
6464
r.DELETE("/:id", h.Delete)
6565
r.GET("/:id", h.GetById)
66-
r.POST("/get-by-filter", h.GetByFilter)
66+
r.POST(GetByFilterExp, h.GetByFilter)
6767
}
6868

6969
func CarModelImage(r *gin.RouterGroup, cfg *config.Config) {
@@ -73,7 +73,7 @@ func CarModelImage(r *gin.RouterGroup, cfg *config.Config) {
7373
r.PUT("/:id", h.Update)
7474
r.DELETE("/:id", h.Delete)
7575
r.GET("/:id", h.GetById)
76-
r.POST("/get-by-filter", h.GetByFilter)
76+
r.POST(GetByFilterExp, h.GetByFilter)
7777
}
7878

7979
func CarModelProperty(r *gin.RouterGroup, cfg *config.Config) {
@@ -83,7 +83,7 @@ func CarModelProperty(r *gin.RouterGroup, cfg *config.Config) {
8383
r.PUT("/:id", h.Update)
8484
r.DELETE("/:id", h.Delete)
8585
r.GET("/:id", h.GetById)
86-
r.POST("/get-by-filter", h.GetByFilter)
86+
r.POST(GetByFilterExp, h.GetByFilter)
8787
}
8888

8989
func CarModelComment(r *gin.RouterGroup, cfg *config.Config) {
@@ -93,5 +93,5 @@ func CarModelComment(r *gin.RouterGroup, cfg *config.Config) {
9393
r.PUT("/:id", h.Update)
9494
r.DELETE("/:id", h.Delete)
9595
r.GET("/:id", h.GetById)
96-
r.POST("/get-by-filter", h.GetByFilter)
96+
r.POST(GetByFilterExp, h.GetByFilter)
9797
}

src/api/routers/properties.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func PropertyCategory(r *gin.RouterGroup, cfg *config.Config) {
1313
r.PUT("/:id", h.Update)
1414
r.DELETE("/:id", h.Delete)
1515
r.GET("/:id", h.GetById)
16-
r.POST("/get-by-filter", h.GetByFilter)
16+
r.POST(GetByFilterExp, h.GetByFilter)
1717
}
1818

1919
func Property(r *gin.RouterGroup, cfg *config.Config) {
@@ -23,5 +23,5 @@ func Property(r *gin.RouterGroup, cfg *config.Config) {
2323
r.PUT("/:id", h.Update)
2424
r.DELETE("/:id", h.Delete)
2525
r.GET("/:id", h.GetById)
26-
r.POST("/get-by-filter", h.GetByFilter)
26+
r.POST(GetByFilterExp, h.GetByFilter)
2727
}

src/cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func main() {
2828
if err != nil {
2929
logger.Fatal(logging.Postgres, logging.Startup, err.Error(), nil)
3030
}
31-
migrations.Up_1()
31+
migrations.Up1()
3232

3333
api.InitServer(cfg)
3434
}

src/data/db/migrations/1_Init.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212
"gorm.io/gorm"
1313
)
1414

15+
const countStarExp = "count(*)"
16+
1517
var logger = logging.NewLogger(config.GetConfig())
1618

17-
func Up_1() {
19+
func Up1() {
1820
database := db.GetDb()
1921

2022
createTables(database)
@@ -121,7 +123,7 @@ func createCountry(database *gorm.DB) {
121123
count := 0
122124
database.
123125
Model(&models.Country{}).
124-
Select("count(*)").
126+
Select(countStarExp).
125127
Find(&count)
126128
if count == 0 {
127129
database.Create(&models.Country{Name: "Iran", Cities: []models.City{
@@ -191,7 +193,7 @@ func createPropertyCategory(database *gorm.DB) {
191193

192194
database.
193195
Model(&models.PropertyCategory{}).
194-
Select("count(*)").
196+
Select(countStarExp).
195197
Find(&count)
196198
if count == 0 {
197199
database.Create(&models.PropertyCategory{Name: "Body"}) // بدنه
@@ -231,7 +233,7 @@ func createProperty(database *gorm.DB, cat string) {
231233

232234
database.
233235
Model(&models.Property{}).
234-
Select("count(*)").
236+
Select(countStarExp).
235237
Where("category_id = ?", catModel.Id).
236238
Find(&count)
237239

@@ -286,7 +288,7 @@ func createCarType(database *gorm.DB) {
286288
count := 0
287289
database.
288290
Model(&models.CarType{}).
289-
Select("count(*)").
291+
Select(countStarExp).
290292
Find(&count)
291293
if count == 0 {
292294
database.Create(&models.CarType{Name: "Crossover"})
@@ -301,7 +303,7 @@ func createGearbox(database *gorm.DB) {
301303
count := 0
302304
database.
303305
Model(&models.Gearbox{}).
304-
Select("count(*)").
306+
Select(countStarExp).
305307
Find(&count)
306308
if count == 0 {
307309
database.Create(&models.Gearbox{Name: "Manual"})
@@ -313,7 +315,7 @@ func createColor(database *gorm.DB) {
313315
count := 0
314316
database.
315317
Model(&models.Color{}).
316-
Select("count(*)").
318+
Select(countStarExp).
317319
Find(&count)
318320
if count == 0 {
319321
database.Create(&models.Color{Name: "Black", HexCode: "#000000"})
@@ -326,7 +328,7 @@ func createYear(database *gorm.DB) {
326328
count := 0
327329
database.
328330
Model(&models.PersianYear{}).
329-
Select("count(*)").
331+
Select(countStarExp).
330332
Find(&count)
331333
if count == 0 {
332334

@@ -374,6 +376,6 @@ func createYear(database *gorm.DB) {
374376
}
375377
}
376378

377-
func Down_1() {
378-
379+
func Down1() {
380+
// nothing
379381
}

src/data/models/base_model.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ type BaseModel struct {
2222
func (m *BaseModel) BeforeCreate(tx *gorm.DB) (err error) {
2323
value := tx.Statement.Context.Value("UserId")
2424
var userId = -1
25-
// TODO: check userId type
2625
if value != nil {
2726
userId = int(value.(float64))
2827
}
@@ -34,7 +33,6 @@ func (m *BaseModel) BeforeCreate(tx *gorm.DB) (err error) {
3433
func (m *BaseModel) BeforeUpdate(tx *gorm.DB) (err error) {
3534
value := tx.Statement.Context.Value("UserId")
3635
var userId = &sql.NullInt64{Valid: false}
37-
// TODO: check userId type
3836
if value != nil {
3937
userId = &sql.NullInt64{Valid: true, Int64: int64(value.(float64))}
4038
}
@@ -46,7 +44,6 @@ func (m *BaseModel) BeforeUpdate(tx *gorm.DB) (err error) {
4644
func (m *BaseModel) BeforeDelete(tx *gorm.DB) (err error) {
4745
value := tx.Statement.Context.Value("UserId")
4846
var userId = &sql.NullInt64{Valid: false}
49-
// TODO: check userId type
5047
if value != nil {
5148
userId = &sql.NullInt64{Valid: true, Int64: int64(value.(float64))}
5249
}

src/data/models/user.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,3 @@ type UserRole struct {
2525
UserId int
2626
RoleId int
2727
}
28-
29-
/*
30-
user -> n roles
31-
role -> n users
32-
33-
users n <-> n roles
34-
35-
user
36-
user_role -> user_id, role_id, ....
37-
role
38-
39-
*/

src/pkg/logging/logger.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,4 @@ func NewLogger(cfg *config.Config) Logger {
2929
}
3030
panic("logger not supported")
3131
}
32-
33-
// file <- filebeat -> elasticsearch -> kibana
32+

0 commit comments

Comments
 (0)