-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9528ddd
commit b5e1293
Showing
16 changed files
with
357 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
backend/application/dashboard/profile/changepassword/request.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package changepassword | ||
|
||
type validationErrors map[string]string | ||
|
||
type Request struct { | ||
UserUUID string `json:"-"` | ||
CurrentPassword string `json:"current_password"` | ||
NewPassword string `json:"new_password"` | ||
} | ||
|
||
func (r *Request) Validate() (bool, validationErrors) { | ||
return true, nil | ||
} |
5 changes: 5 additions & 0 deletions
5
backend/application/dashboard/profile/changepassword/response.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package changepassword | ||
|
||
type ChangePasswordResponse struct { | ||
ValidationErrors validationErrors `json:"errors,omitempty"` | ||
} |
61 changes: 61 additions & 0 deletions
61
backend/application/dashboard/profile/changepassword/usecase.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package changepassword | ||
|
||
import ( | ||
"crypto/rand" | ||
|
||
"github.com/khanzadimahdi/testproject/domain/password" | ||
"github.com/khanzadimahdi/testproject/domain/user" | ||
) | ||
|
||
type UseCase struct { | ||
userRepository user.Repository | ||
hasher password.Hasher | ||
} | ||
|
||
func NewUseCase(userRepository user.Repository, hasher password.Hasher) *UseCase { | ||
return &UseCase{ | ||
userRepository: userRepository, | ||
hasher: hasher, | ||
} | ||
} | ||
|
||
func (uc *UseCase) ChangePassword(request Request) (*ChangePasswordResponse, error) { | ||
if ok, validation := request.Validate(); !ok { | ||
return &ChangePasswordResponse{ | ||
ValidationErrors: validation, | ||
}, nil | ||
} | ||
|
||
u, err := uc.userRepository.GetOne(request.UserUUID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !uc.passwordIsValid(u, []byte(request.CurrentPassword)) { | ||
return &ChangePasswordResponse{ | ||
ValidationErrors: validationErrors{ | ||
"current_password": "current password is not valid", | ||
}, | ||
}, nil | ||
} | ||
|
||
salt := make([]byte, 64) | ||
if _, err := rand.Read(salt); err != nil { | ||
return nil, err | ||
} | ||
|
||
u.PasswordHash = password.Hash{ | ||
Value: uc.hasher.Hash([]byte(request.NewPassword), salt), | ||
Salt: salt, | ||
} | ||
|
||
if err := uc.userRepository.Save(&u); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ChangePasswordResponse{}, err | ||
} | ||
|
||
func (uc *UseCase) passwordIsValid(u user.User, password []byte) bool { | ||
return uc.hasher.Equal(password, u.PasswordHash.Value, u.PasswordHash.Salt) | ||
} |
1 change: 1 addition & 0 deletions
1
backend/application/dashboard/profile/changepassword/usecase_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package changepassword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package getprofile | ||
|
||
type GetProfileResponse struct { | ||
UUID string `json:"uuid,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Avatar string `json:"avatar,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
Username string `json:"username,omitempty"` | ||
} |
30 changes: 30 additions & 0 deletions
30
backend/application/dashboard/profile/getprofile/usecase.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package getprofile | ||
|
||
import ( | ||
"github.com/khanzadimahdi/testproject/domain/user" | ||
) | ||
|
||
type UseCase struct { | ||
userRepository user.Repository | ||
} | ||
|
||
func NewUseCase(userRepository user.Repository) *UseCase { | ||
return &UseCase{ | ||
userRepository: userRepository, | ||
} | ||
} | ||
|
||
func (uc *UseCase) Profile(UUID string) (*GetProfileResponse, error) { | ||
u, err := uc.userRepository.GetOne(UUID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &GetProfileResponse{ | ||
UUID: UUID, | ||
Name: u.Name, | ||
Avatar: u.Avatar, | ||
Email: u.Email, | ||
Username: u.Username, | ||
}, err | ||
} |
1 change: 1 addition & 0 deletions
1
backend/application/dashboard/profile/getprofile/usecase_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package getprofile |
15 changes: 15 additions & 0 deletions
15
backend/application/dashboard/profile/updateprofile/request.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package updateprofile | ||
|
||
type validationErrors map[string]string | ||
|
||
type Request struct { | ||
UserUUID string `json:"-"` | ||
Name string `json:"name"` | ||
Avatar string `json:"avatar"` | ||
Email string `json:"email"` | ||
Username string `json:"username"` | ||
} | ||
|
||
func (r *Request) Validate() (bool, validationErrors) { | ||
return true, nil | ||
} |
5 changes: 5 additions & 0 deletions
5
backend/application/dashboard/profile/updateprofile/response.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package updateprofile | ||
|
||
type UpdateProfileResponse struct { | ||
ValidationErrors validationErrors `json:"errors,omitempty"` | ||
} |
76 changes: 76 additions & 0 deletions
76
backend/application/dashboard/profile/updateprofile/usecase.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package updateprofile | ||
|
||
import ( | ||
"github.com/khanzadimahdi/testproject/domain" | ||
"github.com/khanzadimahdi/testproject/domain/user" | ||
) | ||
|
||
type UseCase struct { | ||
userRepository user.Repository | ||
} | ||
|
||
func NewUseCase(userRepository user.Repository) *UseCase { | ||
return &UseCase{ | ||
userRepository: userRepository, | ||
} | ||
} | ||
|
||
func (uc *UseCase) UpdateProfile(request Request) (*UpdateProfileResponse, error) { | ||
if ok, validation := request.Validate(); !ok { | ||
return &UpdateProfileResponse{ | ||
ValidationErrors: validation, | ||
}, nil | ||
} | ||
|
||
// make sure email is unique | ||
exists, err := uc.anotherUserExists(request.Email, request.UserUUID) | ||
if err != nil { | ||
return nil, err | ||
} else if exists { | ||
return &UpdateProfileResponse{ | ||
ValidationErrors: map[string]string{ | ||
"email": "another user with this email already exists", | ||
}, | ||
}, nil | ||
} | ||
|
||
// make sure username is unique | ||
exists, err = uc.anotherUserExists(request.Username, request.UserUUID) | ||
if err != nil { | ||
return nil, err | ||
} else if exists { | ||
return &UpdateProfileResponse{ | ||
ValidationErrors: map[string]string{ | ||
"username": "another user with this email already exists", | ||
}, | ||
}, nil | ||
} | ||
|
||
user, err := uc.userRepository.GetOne(request.UserUUID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
user.Name = request.Name | ||
user.Avatar = request.Avatar | ||
user.Email = request.Email | ||
user.Username = request.Username | ||
|
||
err = uc.userRepository.Save(&user) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &UpdateProfileResponse{}, err | ||
} | ||
|
||
func (uc *UseCase) anotherUserExists(identity string, currentUserUUID string) (bool, error) { | ||
u, err := uc.userRepository.GetOneByIdentity(identity) | ||
if err == domain.ErrNotExists { | ||
return false, nil | ||
} else if err != nil { | ||
return false, err | ||
} | ||
|
||
return u.UUID != currentUserUUID, nil | ||
} |
1 change: 1 addition & 0 deletions
1
backend/application/dashboard/profile/updateprofile/usecase_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package updateprofile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
backend/presentation/http/api/dashboard/profile/changepassword.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package profile | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/khanzadimahdi/testproject/application/auth" | ||
"github.com/khanzadimahdi/testproject/application/dashboard/profile/changepassword" | ||
"github.com/khanzadimahdi/testproject/domain" | ||
) | ||
|
||
type changePasswordHandler struct { | ||
userCase *changepassword.UseCase | ||
} | ||
|
||
func NewChangePasswordHandler(userCase *changepassword.UseCase) *changePasswordHandler { | ||
return &changePasswordHandler{ | ||
userCase: userCase, | ||
} | ||
} | ||
|
||
func (h *changePasswordHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | ||
var request changepassword.Request | ||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | ||
rw.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
request.UserUUID = auth.FromContext(r.Context()).UUID | ||
|
||
response, err := h.userCase.ChangePassword(request) | ||
|
||
switch true { | ||
case errors.Is(err, domain.ErrNotExists): | ||
rw.WriteHeader(http.StatusNotFound) | ||
case err != nil: | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
case len(response.ValidationErrors) > 0: | ||
rw.WriteHeader(http.StatusBadRequest) | ||
json.NewEncoder(rw).Encode(response) | ||
default: | ||
rw.WriteHeader(http.StatusNoContent) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
backend/presentation/http/api/dashboard/profile/getprofile.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package profile | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/khanzadimahdi/testproject/application/auth" | ||
"github.com/khanzadimahdi/testproject/application/dashboard/profile/getprofile" | ||
"github.com/khanzadimahdi/testproject/domain" | ||
) | ||
|
||
type getProfileHandler struct { | ||
useCase *getprofile.UseCase | ||
} | ||
|
||
func NewGetProfileHandler(useCase *getprofile.UseCase) *getProfileHandler { | ||
return &getProfileHandler{ | ||
useCase: useCase, | ||
} | ||
} | ||
|
||
func (h *getProfileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | ||
userUUID := auth.FromContext(r.Context()).UUID | ||
|
||
response, err := h.useCase.Profile(userUUID) | ||
|
||
switch true { | ||
case errors.Is(err, domain.ErrNotExists): | ||
rw.WriteHeader(http.StatusNotFound) | ||
case err != nil: | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
default: | ||
rw.Header().Add("Content-Type", "application/json") | ||
rw.WriteHeader(http.StatusOK) | ||
json.NewEncoder(rw).Encode(response) | ||
} | ||
} |
Oops, something went wrong.