/
updateOpening.go
77 lines (64 loc) · 1.77 KB
/
updateOpening.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
package handler
import (
"net/http"
"github.com/arthur404dev/gopportunities/schemas"
"github.com/gin-gonic/gin"
)
// @BasePath /api/v1
// @Summary Update opening
// @Description Update a job opening
// @Tags Openings
// @Accept json
// @Produce json
// @Param id query string true "Opening Identification"
// @Param opening body UpdateOpeningRequest true "Opening data to Update"
// @Success 200 {object} UpdateOpeningResponse
// @Failure 400 {object} ErrorResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /opening [put]
func UpdateOpeningHandler(ctx *gin.Context) {
request := UpdateOpeningRequest{}
ctx.BindJSON(&request)
if err := request.Validate(); err != nil {
logger.Errorf("validation error: %v", err.Error())
sendError(ctx, http.StatusBadRequest, err.Error())
return
}
id := ctx.Query("id")
if id == "" {
sendError(ctx, http.StatusBadRequest, errParamIsRequired("id", "queryParameter").Error())
return
}
opening := schemas.Opening{}
if err := db.First(&opening, id).Error; err != nil {
sendError(ctx, http.StatusNotFound, "opening not found")
return
}
// Update opening
if request.Role != "" {
opening.Role = request.Role
}
if request.Company != "" {
opening.Company = request.Company
}
if request.Location != "" {
opening.Location = request.Location
}
if request.Remote != nil {
opening.Remote = *request.Remote
}
if request.Link != "" {
opening.Link = request.Link
}
if request.Salary > 0 {
opening.Salary = request.Salary
}
// Save opening
if err := db.Save(&opening).Error; err != nil {
logger.Errorf("error updating opening: %v", err.Error())
sendError(ctx, http.StatusInternalServerError, "error updating opening")
return
}
sendSuccess(ctx, "update-opening", opening)
}