-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
44 lines (37 loc) · 863 Bytes
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package utils
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/manjurulhoque/golang-job-portal/internal/models"
"net/http"
)
// AuthorizedUser .
func AuthorizedUser(c *gin.Context) (user models.RetrieveUser, err error) {
authorizedUser, ok := c.Get("AuthorizedUser")
if !ok {
err = errors.New("no 'AuthorizedUser'")
return
}
user, ok = authorizedUser.(models.RetrieveUser)
if !ok {
err = errors.New("'AuthorizedUser' not 'models.User' type")
return
}
return user, nil
}
func RequesterIsJobOwner(c *gin.Context, job *models.Job) bool {
user, err := AuthorizedUser(c)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err})
return false
}
return job.UserId == user.ID
}
func SliceContains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}