@@ -6,14 +6,15 @@ import (
6
6
"strings"
7
7
8
8
"github.com/golang-jwt/jwt/v4"
9
+ "github.com/gorilla/mux"
9
10
"github.com/segmentio/ksuid"
10
11
11
12
"github.com/Euler-B/API-REST_Go/models"
12
13
"github.com/Euler-B/API-REST_Go/repository"
13
14
"github.com/Euler-B/API-REST_Go/server"
14
15
)
15
16
16
- type InsertPostRequest struct {
17
+ type UpsertPostRequest struct {
17
18
PostContent string `json:"post_content"`
18
19
}
19
20
@@ -22,6 +23,10 @@ type PostResponse struct {
22
23
PostContent string `json:"post_content"`
23
24
}
24
25
26
+ type PostUpdateResponse struct {
27
+ Message string `json:"message"`
28
+ }
29
+
25
30
func InsertPostHandler (s server.Server ) http.HandlerFunc {
26
31
return func (w http.ResponseWriter , r * http.Request ) {
27
32
tokenString := strings .TrimSpace (r .Header .Get ("Authorization" ))
@@ -34,7 +39,7 @@ func InsertPostHandler(s server.Server) http.HandlerFunc {
34
39
return
35
40
}
36
41
if claims , ok := token .Claims .(* models.AppClaims ); ok && token .Valid {
37
- var postRequest = InsertPostRequest {}
42
+ var postRequest = UpsertPostRequest {}
38
43
err := json .NewDecoder (r .Body ).Decode (& postRequest );
39
44
if err != nil {
40
45
http .Error (w , err .Error (), http .StatusBadRequest )
@@ -65,4 +70,57 @@ func InsertPostHandler(s server.Server) http.HandlerFunc {
65
70
return
66
71
}
67
72
}
73
+ }
74
+
75
+ func GetPostByIdHandler (s server.Server ) http.HandlerFunc {
76
+ return func (w http.ResponseWriter , r * http.Request ) {
77
+ params := mux .Vars (r )
78
+ post , err := repository .GetPostById (r .Context (), params ["id" ])
79
+ if err != nil {
80
+ http .Error (w , err .Error (), http .StatusInternalServerError )
81
+ return
82
+ }
83
+ w .Header ().Set ("Content-Type" , "application/json" )
84
+ json .NewEncoder (w ).Encode (post )
85
+ }
86
+ }
87
+
88
+ func UpdatePostHandler (s server.Server ) http.HandlerFunc {
89
+ return func (w http.ResponseWriter , r * http.Request ) {
90
+ tokenString := strings .TrimSpace (r .Header .Get ("Authorization" )) // reto propuesto:
91
+ token , err := jwt .ParseWithClaims (tokenString , & models.AppClaims {}, // buscar una forma de no repetir tanto el mismo codigo para validar tokens
92
+ func (token * jwt.Token ) (interface {}, error ) {
93
+ return []byte (s .Config ().JWTSecret ), nil
94
+ })
95
+ if err != nil {
96
+ http .Error (w , err .Error (), http .StatusUnauthorized )
97
+ return
98
+ }
99
+ if claims , ok := token .Claims .(* models.AppClaims ); ok && token .Valid {
100
+ params := mux .Vars (r )
101
+ var postRequest = UpsertPostRequest {}
102
+ err := json .NewDecoder (r .Body ).Decode (& postRequest );
103
+ if err != nil {
104
+ http .Error (w , err .Error (), http .StatusBadRequest )
105
+ return
106
+ }
107
+ post := models.Post {
108
+ Id : params ["id" ],
109
+ PostContent : postRequest .PostContent ,
110
+ UserId : claims .UserId ,
111
+ }
112
+ err = repository .UpdatePost (r .Context (), & post )
113
+ if err != nil {
114
+ http .Error (w , err .Error (), http .StatusInternalServerError )
115
+ return
116
+ }
117
+ w .Header ().Set ("Content-Type" , "application/json" )
118
+ json .NewEncoder (w ).Encode (PostUpdateResponse {
119
+ Message : "Post Updated" ,
120
+ })
121
+ } else {
122
+ http .Error (w , err .Error (), http .StatusInternalServerError )
123
+ return
124
+ }
125
+ }
68
126
}
0 commit comments