Skip to content

Commit 988217c

Browse files
authored
Merge branch 'dev' into feature/user
2 parents c7f5fbe + 028530b commit 988217c

File tree

17 files changed

+384
-41
lines changed

17 files changed

+384
-41
lines changed

action.yml renamed to .github/workflows/action.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ jobs:
88
push_to_registry:
99
name: Push Docker image to Docker Hub
1010
runs-on: ubuntu-latest
11+
if: github.ref == 'refs/heads/dev'
1112
steps:
1213
- name: Check out the repo
1314
uses: actions/checkout@v3
1415

1516
- name: Log in to Docker Hub
16-
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
17+
uses: docker/login-action@v2
1718
with:
1819
username: ${{ secrets.DOCKER_USERNAME }}
1920
password: ${{ secrets.DOCKER_PASSWORD }}
@@ -25,9 +26,9 @@ jobs:
2526
images: my-docker-hub-namespace/my-docker-hub-repository
2627

2728
- name: Build and push Docker image
28-
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
29+
uses: docker/build-push-action@v3
2930
with:
30-
context: .
31+
context: "{{defaultContext}}"
3132
push: true
32-
tags: portfolio-api
33-
labels: dev
33+
tags: wong801/portfolio-api:latest
34+
labels: development
-3.25 KB
Binary file not shown.

src/controllers/company.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controller
33
import (
44
"net/http"
55

6+
"github.com/Wong801/gin-api/src/api"
67
model "github.com/Wong801/gin-api/src/models"
78
service "github.com/Wong801/gin-api/src/services"
89
"github.com/gin-gonic/gin"
@@ -38,7 +39,7 @@ func (cc CompanyController) Search() func(c *gin.Context) {
3839
status, list, errService := cc.s.Search(cQuery.Name)
3940
c.Set("status", status)
4041
if errService != nil {
41-
c.Set("error", errService)
42+
c.Set("error", api.MakeResultError(errService))
4243
return
4344
}
4445

@@ -54,13 +55,14 @@ func (cc CompanyController) Get() func(c *gin.Context) {
5455
err := c.ShouldBindUri(&cUri)
5556
if err != nil {
5657
c.Set("status", http.StatusBadRequest)
57-
c.Set("error", err)
58+
c.Set("error", api.MakeRequestError(err))
59+
return
5860
}
5961

6062
status, data, errService := cc.s.Get(cUri.Id)
6163
c.Set("status", status)
6264
if errService != nil {
63-
c.Set("error", errService)
65+
c.Set("error", api.MakeResultError(errService))
6466
return
6567
}
6668

@@ -76,7 +78,8 @@ func (cc CompanyController) Create() func(c *gin.Context) {
7678
err := c.ShouldBind(&company)
7779
if err != nil {
7880
c.Set("status", http.StatusBadRequest)
79-
c.Set("error", err)
81+
c.Set("error", api.MakeRequestError(err))
82+
return
8083
}
8184
if logoPath, ok := c.Get("company_logo"); ok {
8285
company.Logo = logoPath.(string)
@@ -85,7 +88,7 @@ func (cc CompanyController) Create() func(c *gin.Context) {
8588
status, data, errService := cc.s.Create(company)
8689
c.Set("status", status)
8790
if errService != nil {
88-
c.Set("error", errService)
91+
c.Set("error", api.MakeResultError(errService))
8992
return
9093
}
9194

@@ -103,7 +106,8 @@ func (cc CompanyController) Update() func(c *gin.Context) {
103106
err := c.ShouldBind(&company)
104107
if err != nil {
105108
c.Set("status", http.StatusBadRequest)
106-
c.Set("error", err)
109+
c.Set("error", api.MakeRequestError(err))
110+
return
107111
}
108112
if logoPath, ok := c.Get("company_logo"); ok {
109113
company.Logo = logoPath.(string)
@@ -112,7 +116,7 @@ func (cc CompanyController) Update() func(c *gin.Context) {
112116
status, data, errService := cc.s.Update(cUri.Id, company)
113117
c.Set("status", status)
114118
if errService != nil {
115-
c.Set("error", errService)
119+
c.Set("error", api.MakeResultError(errService))
116120
return
117121
}
118122

@@ -128,13 +132,14 @@ func (cc CompanyController) Delete() func(c *gin.Context) {
128132
err := c.ShouldBindUri(&cUri)
129133
if err != nil {
130134
c.Set("status", http.StatusBadRequest)
131-
c.Set("error", err)
135+
c.Set("error", api.MakeRequestError(err))
136+
return
132137
}
133138

134139
status, data, errService := cc.s.Delete(cUri.Id)
135140
c.Set("status", status)
136141
if errService != nil {
137-
c.Set("error", errService)
142+
c.Set("error", api.MakeResultError(errService))
138143
return
139144
}
140145

src/controllers/skill.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package controller
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/Wong801/gin-api/src/api"
7+
model "github.com/Wong801/gin-api/src/models"
8+
service "github.com/Wong801/gin-api/src/services"
9+
"github.com/gin-gonic/gin"
10+
)
11+
12+
type skillUri struct {
13+
Id int `uri:"id" binding:"required"`
14+
}
15+
16+
type SkillController struct {
17+
s service.CRUDService
18+
}
19+
20+
func InitSkillController() *SkillController {
21+
return &SkillController{
22+
s: *service.InitCRUDService("skill", model.Skill{}),
23+
}
24+
}
25+
26+
func (cc SkillController) Search() func(c *gin.Context) {
27+
return func(c *gin.Context) {
28+
status, list, errService := cc.s.Search([]model.Skill{})
29+
c.Set("status", status)
30+
if errService != nil {
31+
c.Set("error", api.MakeResultError(errService))
32+
return
33+
}
34+
35+
c.Set("data", list)
36+
c.Next()
37+
}
38+
}
39+
40+
func (cc SkillController) Get() func(c *gin.Context) {
41+
return func(c *gin.Context) {
42+
var cUri skillUri
43+
44+
err := c.ShouldBindUri(&cUri)
45+
if err != nil {
46+
c.Set("status", http.StatusBadRequest)
47+
c.Set("error", api.MakeRequestError(err))
48+
return
49+
}
50+
51+
status, data, errService := cc.s.Get(cUri.Id)
52+
c.Set("status", status)
53+
if errService != nil {
54+
c.Set("error", api.MakeResultError(errService))
55+
return
56+
}
57+
58+
c.Set("data", data)
59+
c.Next()
60+
}
61+
}
62+
63+
func (cc SkillController) Create() func(c *gin.Context) {
64+
return func(c *gin.Context) {
65+
var skill model.Skill
66+
67+
err := c.ShouldBind(&skill)
68+
if err != nil {
69+
c.Set("status", http.StatusBadRequest)
70+
c.Set("error", api.MakeRequestError(err))
71+
return
72+
}
73+
status, data, errService := cc.s.Create(skill)
74+
c.Set("status", status)
75+
if errService != nil {
76+
c.Set("error", api.MakeResultError(errService))
77+
return
78+
}
79+
80+
c.Set("data", data)
81+
c.Next()
82+
}
83+
}
84+
85+
func (cc SkillController) Update() func(c *gin.Context) {
86+
return func(c *gin.Context) {
87+
var cUri skillUri
88+
var skill model.Skill
89+
90+
c.ShouldBindUri(&cUri)
91+
err := c.ShouldBind(&skill)
92+
if err != nil {
93+
c.Set("status", http.StatusBadRequest)
94+
c.Set("error", err)
95+
return
96+
}
97+
98+
status, data, errService := cc.s.Update(cUri.Id, skill)
99+
c.Set("status", status)
100+
if errService != nil {
101+
c.Set("error", api.MakeResultError(errService))
102+
return
103+
}
104+
105+
c.Set("data", data)
106+
c.Next()
107+
}
108+
}
109+
110+
func (cc SkillController) Delete() func(c *gin.Context) {
111+
return func(c *gin.Context) {
112+
var cUri skillUri
113+
114+
err := c.ShouldBindUri(&cUri)
115+
if err != nil {
116+
c.Set("status", http.StatusBadRequest)
117+
c.Set("error", api.MakeRequestError(err))
118+
return
119+
}
120+
121+
status, data, errService := cc.s.Delete(cUri.Id)
122+
c.Set("status", status)
123+
if errService != nil {
124+
c.Set("error", api.MakeResultError(errService))
125+
return
126+
}
127+
128+
c.Set("data", data)
129+
c.Next()
130+
}
131+
}

src/db/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ func (h Adapter) Close() error {
5353
func (h Adapter) MigrateModels() {
5454
h.Database.AutoMigrate(&model.User{})
5555
h.Database.AutoMigrate(&model.Company{})
56+
h.Database.AutoMigrate(&model.Skill{})
5657
}

src/middlewares/authenticate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func (m middleware) Authenticate() func(c *gin.Context) {
1616
if err != nil {
1717
c.Set("status", http.StatusUnauthorized)
1818
c.Set("error", "Unauthorized")
19+
c.Abort()
1920
return
2021
}
2122

@@ -28,16 +29,19 @@ func (m middleware) Authenticate() func(c *gin.Context) {
2829
if err == jwt.ErrSignatureInvalid {
2930
c.Set("status", http.StatusUnauthorized)
3031
c.Set("error", "Unauthorized")
32+
c.Abort()
3133
return
3234
}
3335
c.Set("status", http.StatusBadRequest)
3436
c.Set("error", "Unauthorized")
37+
c.Abort()
3538
return
3639
}
3740

3841
if !token.Valid {
3942
c.Set("status", http.StatusUnauthorized)
4043
c.Set("error", "Invalid Token")
44+
c.Abort()
4145
return
4246
}
4347
id, _ := strconv.Atoi(claims.ID)

src/middlewares/file_handler.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package middleware
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
func (m middleware) LogoHandler(reference string, storePath string) func(c *gin.Context) {
6+
return func(c *gin.Context) {
7+
file, err := c.FormFile(reference)
8+
if err == nil {
9+
dst := storePath + file.Filename
10+
11+
c.Set("company_logo", "/"+dst)
12+
c.SaveUploadedFile(file, dst)
13+
}
14+
c.Next()
15+
}
16+
}

src/middlewares/response.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package middleware
22

33
import (
4+
"fmt"
45
"net/http"
56

67
entity "github.com/Wong801/gin-api/src/entities"
@@ -21,10 +22,10 @@ func getSuccessStatus(status any) int {
2122
return http.StatusOK
2223
}
2324

24-
func getErrorMessage(c *gin.Context) any {
25+
func getErrorMessage(c *gin.Context) interface{} {
2526
err := c.Errors.Last()
2627
if err != nil {
27-
return entity.ResultError{Reason: c.Error(err).Error()}
28+
return &entity.ResultError{Reason: c.Error(err).Error()}
2829
}
2930
customError, _ := c.Get("error")
3031
return customError
@@ -42,6 +43,7 @@ func (m middleware) Response() gin.HandlerFunc {
4243
})
4344
return
4445
}
46+
fmt.Println(c.MustGet("data"))
4547
c.JSON(getSuccessStatus(status), &entity.HttpResponse{
4648
Success: true,
4749
Data: c.MustGet("data"),

src/models/company.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package model
22

33
type Company struct {
4-
Id int `json:"id" form:"id" gorm:"primaryKey"`
4+
ID int `json:"id" form:"id" gorm:"primaryKey"`
55
Name string `json:"name" form:"name" binding:"required"`
66
Logo string `json:"logo" form:"logo"`
77
Link string `json:"link" form:"link" binding:"omitempty"`

src/models/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package model
33
import "time"
44

55
type Timestamp struct {
6-
CreatedAt time.Time `json:"createdAt" form:"createdAt" gorm:"autoCreateTime:true"`
7-
UpdatedAt time.Time `json:"updatedAt" form:"updatedAt" gorm:"autoUpdateTime:true"`
6+
CreatedAt time.Time `json:"createdAt" form:"createdAt" gorm:"type:timestamp;default:current_timestamp;autoCreateTime"`
7+
UpdatedAt time.Time `json:"updatedAt" form:"updatedAt" gorm:"type:timestamp;default:current_timestamp;autoUpdateTime"`
88
}

0 commit comments

Comments
 (0)