Skip to content

Commit 39fd7c1

Browse files
committed
Merge branch 'user-methods' into 'master'
User methods See merge request joinimpact/api!87
2 parents 123a423 + 715e569 commit 39fd7c1

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
11
package users
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/joinimpact/api/internal/users"
7+
"github.com/joinimpact/api/pkg/idctx"
8+
"github.com/joinimpact/api/pkg/resp"
9+
)
10+
11+
// DeletePost deletes a user by ID.
12+
func DeletePost(usersService users.Service) http.HandlerFunc {
13+
type response struct {
14+
Success bool `json:"success"`
15+
}
16+
return func(w http.ResponseWriter, r *http.Request) {
17+
ctx := r.Context()
18+
19+
userID, err := idctx.Get(r, "userID")
20+
if err != nil {
21+
return
22+
}
23+
24+
err = usersService.DeleteUser(ctx, userID)
25+
if err != nil {
26+
switch err.(type) {
27+
case *users.ErrUserNotFound:
28+
resp.NotFound(w, r, resp.Error(404, err.Error()))
29+
case *users.ErrServerError:
30+
resp.ServerError(w, r, resp.Error(500, err.Error()))
31+
default:
32+
resp.ServerError(w, r, resp.UnknownError)
33+
}
34+
return
35+
}
36+
37+
resp.OK(w, r, response{true})
38+
}
39+
}

internal/core/router.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (app *App) Router() *chi.Mux {
6565
r.Use(scopes.Middleware(users.ScopeProviderUsers()))
6666

6767
r.Get("/", users.GetUserProfile(app.usersService))
68+
r.With(permissions.Require(scopes.ScopeOwner)).Delete("/", users.DeletePost(app.usersService))
6869
r.With(permissions.Require(scopes.ScopeOwner)).Patch("/", users.UpdateUserProfile(app.usersService))
6970

7071
r.Get("/tags", users.GetUserTags(app.usersService))
@@ -74,7 +75,7 @@ func (app *App) Router() *chi.Mux {
7475
r.With(permissions.Require(scopes.ScopeOwner)).Post("/profile-picture", users.UploadProfilePicture(app.usersService))
7576

7677
r.With(permissions.Require(scopes.ScopeOwner)).Get("/organizations", organizations.GetUserOrganizations(app.organizationsService))
77-
r.With(permissions.Require(scopes.ScopeOwner)).Get("/opportunities", opportunities.GetByVolunteer(app.opportunitiesService))
78+
r.Get("/opportunities", opportunities.GetByVolunteer(app.opportunitiesService))
7879
r.With(permissions.Require(scopes.ScopeOwner)).Get("/events", events.GetByVolunteer(app.eventsService))
7980

8081
r.Route("/conversations", func(r chi.Router) {

internal/users/service.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package users
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"time"
@@ -33,6 +34,8 @@ type Service interface {
3334
SetUserProfileField(userID int64, profileField models.UserProfileField) error
3435
// UploadProfilePicture uploads a profile picture to the CDN and adds it to the user.
3536
UploadProfilePicture(userID int64, fileReader io.Reader) (string, error)
37+
// DeleteUser deletes a user's account by ID.
38+
DeleteUser(ctx context.Context, userID int64) error
3639
}
3740

3841
// service represents the internal implementation of the Service interface.
@@ -288,3 +291,12 @@ func (s *service) UploadProfilePicture(userID int64, fileReader io.Reader) (stri
288291
ProfilePicture: url,
289292
})
290293
}
294+
295+
// DeleteUser deletes a user by ID.
296+
func (s *service) DeleteUser(ctx context.Context, userID int64) error {
297+
if err := s.userRepository.DeleteByID(userID); err != nil {
298+
return NewErrServerError()
299+
}
300+
301+
return nil
302+
}

0 commit comments

Comments
 (0)