-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_update_duelist.go
100 lines (84 loc) · 2.98 KB
/
handler_update_duelist.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
package handlers
import (
"encoding/json"
e "errors"
"log/slog"
"net/http"
"github.com/JPauloMoura/Yu-Gi-Oh-Connect/internal/entities"
"github.com/JPauloMoura/Yu-Gi-Oh-Connect/internal/handlers/dto"
"github.com/JPauloMoura/Yu-Gi-Oh-Connect/pkg/errors"
"github.com/JPauloMoura/Yu-Gi-Oh-Connect/pkg/response"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
)
// @Summary UpdateDuelist
// @Description Realiza a atualização de um perfil de duelista na plataforma.
// @Tags Duelist
// @Accept json
// @Produce json
// @Param uid path string true "6457d5dc-6a4b-409f-972e-f8bb8f9f9f67"
// @param body body dto.UpdateDuelistDTO true "UpdateDuelistDTO object"
// @Success 200 {object} response.Response
// @Router /duelist/{uid} [PUT]
func (h HandlerDuelist) UpdateDuelist(w http.ResponseWriter, r *http.Request) {
duelistId, err := uuid.Parse(chi.URLParam(r, "id"))
if err != nil {
slog.Error("failed to parse id", err)
response.Encode(w, errors.ErrorInvalidId, http.StatusBadRequest)
return
}
var requestBody dto.UpdateDuelistDTO
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
slog.Error("failed to decode body", slog.String("error", err.Error()))
response.Encode(w, errors.Join(errors.ErrorInvalidRequest, err), http.StatusBadRequest)
return
}
if err := requestBody.Validate(); err != nil {
slog.Error("failed to validate duelist", slog.String("error", err.Error()))
response.Encode(w, err, http.StatusBadRequest)
return
}
if requestBody.Cep != "" {
requestBody.Address, err = h.svcCep.GetAddress(requestBody.Cep)
if err == errors.ErrorInvalidCep {
slog.Error("failed get address", slog.String("error", err.Error()))
response.Encode(w, err, http.StatusBadRequest)
return
}
if err != nil {
slog.Error("failed get address", slog.String("error", err.Error()))
response.Encode(w, err, http.StatusInternalServerError)
return
}
}
duelist := createEntityDuelistByUpdateDuelistDTO(requestBody)
duelist.Id = duelistId.String()
err = h.svcDuelist.UpdateDuelist(duelist)
if e.Is(err, errors.ErrorDuelistNotFound) {
slog.Warn("failed to update duelist", slog.Any("error", err))
response.Encode(w, err, http.StatusNotFound)
return
}
if err != nil {
slog.Error("failed to update duelist", slog.String("error", err.Error()), slog.Any("duelist", duelist))
response.Encode(w, err, http.StatusInternalServerError)
return
}
response.Encode(w, "updated", http.StatusOK)
}
func createEntityDuelistByUpdateDuelistDTO(dto dto.UpdateDuelistDTO) entities.Duelist {
var duelist entities.Duelist
duelist.Name = dto.Name
duelist.Presentation = dto.Presentation
duelist.BirthDate = dto.BirthDate
duelist.Contact.Email = dto.Email
duelist.Contact.Phone = dto.Phone
if dto.Address != nil {
duelist.Address.Cep = dto.Address.Zipcode
duelist.Address.State = dto.Address.StateShortname
duelist.Address.City = dto.Address.City
duelist.Address.District = dto.Address.District
duelist.Address.Street = dto.Address.Street
}
return duelist
}