Skip to content

Commit c4085a8

Browse files
committed
CRUD for Posts(Final Part)
1 parent e594015 commit c4085a8

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

database/postgres.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ func (repo *PostgresRepository) GetUserByEmail(ctx context.Context, email string
112112
func (repo *PostgresRepository) UpdatePost(ctx context.Context, post *models.Post) error {
113113
_, err := repo.db.ExecContext(ctx,
114114
"UPDATE posts SET post_content = $1 WHERE id = $2 and user_id = $3",
115-
post.PostContent, post.Id, post.UserId )
115+
post.PostContent, post.Id, post.UserId)
116+
return err
117+
}
118+
119+
func (repo *PostgresRepository) DeletePost(ctx context.Context, id string, userId string) error {
120+
_, err := repo.db.ExecContext(ctx, "DELETE FROM posts WHERE id = $1 AND user_id = $2",
121+
id, userId)
116122
return err
117123
}

handlers/posts.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func GetPostByIdHandler(s server.Server) http.HandlerFunc {
8787

8888
func UpdatePostHandler(s server.Server) http.HandlerFunc {
8989
return func(w http.ResponseWriter, r *http.Request) {
90+
params := mux.Vars(r)
9091
tokenString := strings.TrimSpace(r.Header.Get("Authorization")) // reto propuesto:
9192
token, err := jwt.ParseWithClaims(tokenString, &models.AppClaims{}, // buscar una forma de no repetir tanto el mismo codigo para validar tokens
9293
func(token *jwt.Token) (interface{}, error) {
@@ -97,7 +98,6 @@ func UpdatePostHandler(s server.Server) http.HandlerFunc {
9798
return
9899
}
99100
if claims, ok := token.Claims.(*models.AppClaims); ok && token.Valid {
100-
params := mux.Vars(r)
101101
var postRequest = UpsertPostRequest{}
102102
err := json.NewDecoder(r.Body).Decode(&postRequest);
103103
if err != nil {
@@ -123,4 +123,33 @@ func UpdatePostHandler(s server.Server) http.HandlerFunc {
123123
return
124124
}
125125
}
126+
}
127+
128+
func DeletePostHandler(s server.Server) http.HandlerFunc {
129+
return func(w http.ResponseWriter, r *http.Request) {
130+
params := mux.Vars(r)
131+
tokenString := strings.TrimSpace(r.Header.Get("Authorization"))
132+
token, err := jwt.ParseWithClaims(tokenString, &models.AppClaims{},
133+
func(token *jwt.Token) (interface{}, error) {
134+
return []byte(s.Config().JWTSecret), nil
135+
})
136+
if err != nil {
137+
http.Error(w, err.Error(), http.StatusUnauthorized)
138+
return
139+
}
140+
if claims, ok := token.Claims.(*models.AppClaims); ok && token.Valid {
141+
err = repository.DeletePost(r.Context(), params["id"], claims.UserId)
142+
if err != nil {
143+
http.Error(w, err.Error(), http.StatusInternalServerError)
144+
return
145+
}
146+
w.Header().Set("Content-Type", "application/json")
147+
json.NewEncoder(w).Encode(PostUpdateResponse{
148+
Message: "Post Deleted",
149+
})
150+
} else {
151+
http.Error(w, err.Error(), http.StatusInternalServerError)
152+
return
153+
}
154+
}
126155
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ func BindRoutes(s server.Server, r *mux.Router) {
4949
r.HandleFunc("/posts", handlers.InsertPostHandler(s)).Methods(http.MethodPost)
5050
r.HandleFunc("/posts/{id}", handlers.GetPostByIdHandler(s)).Methods(http.MethodGet)
5151
r.HandleFunc("/posts/{id}", handlers.UpdatePostHandler(s)).Methods(http.MethodPut)
52+
r.HandleFunc("/posts/{id}", handlers.DeletePostHandler(s)).Methods(http.MethodDelete)
5253
}

repository/repository.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Repository interface {
1414
InsertPost(ctx context.Context, post *models.Post) error
1515
GetPostById(ctx context.Context, id string) (*models.Post, error)
1616
UpdatePost(ctx context.Context, post *models.Post) error
17+
DeletePost(ctx context.Context, id string, userId string) error
1718
Close() error
1819
}
1920

@@ -49,4 +50,8 @@ func GetPostById(ctx context.Context, id string) (*models.Post, error) {
4950

5051
func UpdatePost(ctx context.Context, post *models.Post) error {
5152
return implementation.UpdatePost(ctx, post)
53+
}
54+
55+
func DeletePost(ctx context.Context, id string, userId string) error {
56+
return implementation.DeletePost(ctx, id, userId)
5257
}

0 commit comments

Comments
 (0)