/
controller.go
114 lines (93 loc) · 3.06 KB
/
controller.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package person
import (
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/jobsgowhere/jobsgowhere/pkg/web"
"github.com/volatiletech/sqlboiler/v4/boil"
)
// Controller interface
type Controller interface {
PostProfile(ginCtx *gin.Context)
PutProfile(ginCtx *gin.Context)
GetProfile(ginCtx *gin.Context)
}
// personController struct
type personController struct {
service Service
}
// NewController for person repository
func NewController(exec boil.ContextExecutor) Controller {
repo := &personRepository{executor: exec}
svc := &personService{repo: repo}
pc := &personController{service: svc}
return pc
}
// create talent
func (c *personController) GetProfile(ginCtx *gin.Context) {
iamID := ginCtx.GetString("iam_id")
profile, err := c.service.GetProfile(ginCtx.Request.Context(), iamID)
if err != nil {
if err.Error() == "profile_not_found" {
web.RespondError(ginCtx, http.StatusNotFound, ProfileNotFound, "Profile not found for the given iam_id")
return
} else {
log.Println("Error occurred personController::GetProfile" + err.Error())
web.RespondError(ginCtx, http.StatusInternalServerError, "internal_error", "An error occurred in the server, please retry after sometime. err="+err.Error())
return
}
}
if strings.TrimSpace(profile.ID) == "" {
web.RespondError(ginCtx, http.StatusNotFound, ProfileNotFound, "Profile not found for the given iam_id")
return
}
web.RespondOK(ginCtx, profile)
}
// create profile
func (c *personController) PostProfile(ginCtx *gin.Context) {
var profileParams ProfileParams
err := ginCtx.Bind(&profileParams)
iamID := ginCtx.GetString("iam_id")
if err != nil {
web.RespondError(ginCtx, http.StatusInternalServerError, "internal_error", err.Error())
return
}
if !validProfileParams(profileParams) {
web.RespondError(ginCtx, http.StatusBadRequest, "not_enough_arguments", "Required parameters are missing")
return
}
person, err := c.service.CreateProfile(ginCtx.Request.Context(), iamID, profileParams)
if err != nil {
web.RespondError(ginCtx, http.StatusInternalServerError, "internal_error", err.Error())
return
}
web.RespondOK(ginCtx, person)
}
// edit profile
func (c *personController) PutProfile(ginCtx *gin.Context) {
var profileParams ProfileParams
err := ginCtx.Bind(&profileParams)
iamID := ginCtx.GetString("iam_id")
if err != nil {
web.RespondError(ginCtx, http.StatusInternalServerError, "internal_error", err.Error())
return
}
if !validProfileParams(profileParams) {
web.RespondError(ginCtx, http.StatusBadRequest, "not_enough_arguments", "Required parameters are missing")
return
}
person, err := c.service.EditProfile(ginCtx.Request.Context(), iamID, profileParams)
if err != nil {
web.RespondError(ginCtx, http.StatusInternalServerError, "internal_error", err.Error())
return
}
web.RespondOK(ginCtx, person)
}
func validProfileParams(pp ProfileParams) bool {
if strings.TrimSpace(pp.FirstName) == "" || strings.TrimSpace(pp.LastName) == "" ||
strings.TrimSpace(pp.Email) == "" || strings.TrimSpace(pp.AvatarURL) == "" {
return false
}
return true
}