Skip to content

Commit

Permalink
[Backend] Make possibility to set option showTranscription to user pr…
Browse files Browse the repository at this point in the history
…ofile
  • Loading branch information
Yan Matskevich committed Nov 11, 2024
1 parent 187a358 commit 6d573e8
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ restart: ## Stops ran containers, destroys them and restart
docker compose -f compose-dev-backend.yml build ;\
docker compose -f compose-dev-backend.yml up -d

release_latest: ## Creates and pushes imaged to docker hub using the last git tag
release_latest: ## Creates and pushes images to docker hub using the last git tag
docker buildx create --use ;\
docker buildx build --push --platform linux/amd64,linux/arm64 \
-t macyan/webdict:${GITTAG} -t macyan/webdict:latest .
3 changes: 2 additions & 1 deletion backend/pkg/app/command/update_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type UpdateProfile struct {
CurrentPassword string
NewPassword string
DefaultLangID string
ListOptions user.ListOptions
}

type UpdateProfileHandler struct {
Expand Down Expand Up @@ -41,7 +42,7 @@ func (h UpdateProfileHandler) Handle(cmd UpdateProfile) error {
return err
}

if err = usr.ApplyChanges(cmd.Name, cmd.Email, passwd, usr.Role(), cmd.DefaultLangID); err != nil {
if err = usr.ApplyChanges(cmd.Name, cmd.Email, passwd, usr.Role(), cmd.DefaultLangID, cmd.ListOptions); err != nil {
return err
}

Expand Down
3 changes: 3 additions & 0 deletions backend/pkg/app/command/update_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,19 @@ func TestUpdateProfileHandler_Handle_PositiveCases(t *testing.T) {
CurrentPassword: currentPasswd,
NewPassword: newPasswd,
DefaultLangID: langID,
ListOptions: user.NewListOptions(true),
}

handler := NewUpdateProfileHandler(&usrRepo, &cipher, &langRepo)
assert.Nil(t, handler.Handle(cmd))

updatedUsr := usrRepo.Calls[1].Arguments[0].(*user.User)
data := updatedUsr.ToMap()
listData := updatedUsr.ListOptions()

assert.Equal(t, cmd.Name, data["name"])
assert.Equal(t, cmd.Email, data["email"])
assert.Equal(t, newHash, data["password"])
assert.Equal(t, langID, data["defaultLangID"])
assert.Equal(t, true, listData.ToMap()["showTranscription"])
}
2 changes: 1 addition & 1 deletion backend/pkg/app/command/update_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (h UpdateUserHandler) Handle(cmd UpdateUser) error {
return err
}

if err = usr.ApplyChanges(cmd.Name, cmd.Email, passwd, cmd.Role, usr.DefaultLangID()); err != nil {
if err = usr.ApplyChanges(cmd.Name, cmd.Email, passwd, cmd.Role, usr.DefaultLangID(), usr.ListOptions()); err != nil {
return err
}

Expand Down
84 changes: 55 additions & 29 deletions backend/pkg/app/domain/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"unicode/utf8"
)

type User struct {
id string
name string
email string
password string
role Role
defaultLangID string
type ListOptions struct {
showTranscription bool
}

func NewListOptions(showTranscription bool) ListOptions {
return ListOptions{showTranscription: showTranscription}
}

func (l *ListOptions) ToMap() map[string]interface{} {
return map[string]interface{}{
"showTranscription": l.showTranscription,
}
}

type Role int
Expand All @@ -28,13 +33,26 @@ func (r Role) valid() bool {
return r >= Admin && r <= Author
}

type User struct {
id string
name string
email string
password string
role Role
defaultLangID string
listOptions ListOptions
}

func NewUser(name, email, password string, role Role) (*User, error) {
u := User{
id: uuid.New().String(),
name: name,
email: email,
password: password,
role: role,
listOptions: ListOptions{
showTranscription: false,
},
}

if err := u.validate(); err != nil {
Expand Down Expand Up @@ -64,24 +82,29 @@ func (u *User) DefaultLangID() string {
return u.defaultLangID
}

func (u *User) ApplyChanges(name, email, passwd string, role Role, defaultLangID string) error {
func (u *User) ListOptions() ListOptions {
return u.listOptions
}

func (u *User) ApplyChanges(name, email, passwd string, role Role, defaultLangID string, listOptions ListOptions) error {
updated := *u
updated.applyChanges(name, email, passwd, role, defaultLangID)
updated.applyChanges(name, email, passwd, role, defaultLangID, listOptions)

if err := updated.validate(); err != nil {
return err
}

u.applyChanges(name, email, passwd, role, defaultLangID)
u.applyChanges(name, email, passwd, role, defaultLangID, listOptions)
return nil
}

func (u *User) applyChanges(name, email, passwd string, role Role, defaultLangID string) {
func (u *User) applyChanges(name, email, passwd string, role Role, defaultLangID string, listOptions ListOptions) {
u.name = name
u.email = email
u.role = role
u.defaultLangID = defaultLangID
u.password = passwd
u.listOptions = listOptions
}

func (u *User) ToMap() map[string]interface{} {
Expand All @@ -92,24 +115,7 @@ func (u *User) ToMap() map[string]interface{} {
"password": u.password,
"role": int(u.role),
"defaultLangID": u.defaultLangID,
}
}

func UnmarshalFromDB(
id string,
name string,
email string,
password string,
role int,
defaultLangID string,
) *User {
return &User{
id: id,
name: name,
email: email,
password: password,
role: Role(role),
defaultLangID: defaultLangID,
"listOptions": u.listOptions.ToMap(),
}
}

Expand Down Expand Up @@ -140,3 +146,23 @@ func (u *User) validate() error {

return err
}

func UnmarshalFromDB(
id string,
name string,
email string,
password string,
role Role,
defaultLangID string,
listOptions ListOptions,
) *User {
return &User{
id: id,
name: name,
email: email,
password: password,
role: role,
defaultLangID: defaultLangID,
listOptions: listOptions,
}
}
56 changes: 33 additions & 23 deletions backend/pkg/app/domain/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func TestNewUser(t *testing.T) {
assert.Equal(t, got.password, tt.args.password)
assert.Equal(t, got.email, tt.args.email)
assert.Equal(t, got.role, tt.args.role)
assert.Equal(t, false, got.listOptions.showTranscription)
})
}
}
Expand All @@ -132,11 +133,12 @@ func TestUnmarshalFromDB(t *testing.T) {
name: "testName",
email: "testEmail",
password: "testPassword",
role: 0,
role: Role(0),
defaultLangID: "testLang",
listOptions: ListOptions{showTranscription: true},
}

assert.Equal(t, &user, UnmarshalFromDB(user.id, user.name, user.email, user.password, int(user.role), user.defaultLangID))
assert.Equal(t, &user, UnmarshalFromDB(user.id, user.name, user.email, user.password, user.role, user.defaultLangID, user.listOptions))
}

func TestRole_valid(t *testing.T) {
Expand Down Expand Up @@ -170,17 +172,19 @@ func TestRole_valid(t *testing.T) {

func TestUser_ApplyChanges(t *testing.T) {
type fields struct {
name string
email string
password string
role Role
name string
email string
password string
role Role
listOptions ListOptions
}
type args struct {
name string
email string
passwd string
role Role
defaultLangID string
listOptions ListOptions
}
tests := []struct {
name string
Expand All @@ -191,16 +195,18 @@ func TestUser_ApplyChanges(t *testing.T) {
{
"Error on validation, changes should not be applied",
fields{
name: "testName",
email: "[email protected]",
password: "testPasswd",
role: Admin,
name: "testName",
email: "[email protected]",
password: "testPasswd",
role: Admin,
listOptions: ListOptions{showTranscription: true},
},
args{
name: "name",
email: "invalidEmail",
passwd: "testPasswd",
role: Author,
name: "name",
email: "invalidEmail",
passwd: "testPasswd",
role: Author,
listOptions: ListOptions{showTranscription: false},
},
func(t assert.TestingT, err error, usr *User, details string) {
assert.True(t, strings.Contains(err.Error(), "email is not valid"), details)
Expand All @@ -213,17 +219,19 @@ func TestUser_ApplyChanges(t *testing.T) {
{
"Applied changes",
fields{
name: "testName",
email: "[email protected]",
password: "testPasswd",
role: Admin,
name: "testName",
email: "[email protected]",
password: "testPasswd",
role: Admin,
listOptions: ListOptions{showTranscription: true},
},
args{
name: "name",
email: "[email protected]",
passwd: "updatedPasswd",
role: Author,
defaultLangID: "langID",
listOptions: ListOptions{showTranscription: false},
},
func(t assert.TestingT, err error, usr *User, details string) {
assert.Nil(t, err, details)
Expand All @@ -232,18 +240,20 @@ func TestUser_ApplyChanges(t *testing.T) {
assert.Equal(t, "updatedPasswd", usr.password)
assert.Equal(t, Author, usr.role)
assert.Equal(t, "langID", usr.defaultLangID)
assert.Equal(t, false, usr.listOptions.showTranscription)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &User{
name: tt.fields.name,
email: tt.fields.email,
password: tt.fields.password,
role: tt.fields.role,
name: tt.fields.name,
email: tt.fields.email,
password: tt.fields.password,
role: tt.fields.role,
listOptions: tt.fields.listOptions,
}
tt.wantFn(t, u.ApplyChanges(tt.args.name, tt.args.email, tt.args.passwd, tt.args.role, tt.args.defaultLangID), u, fmt.Sprintf("ApplyChanges(%v, %v, %v, %v, %v)", tt.args.name, tt.args.email, tt.args.passwd, tt.args.role, tt.args.defaultLangID))
tt.wantFn(t, u.ApplyChanges(tt.args.name, tt.args.email, tt.args.passwd, tt.args.role, tt.args.defaultLangID, tt.args.listOptions), u, fmt.Sprintf("ApplyChanges(%v, %v, %v, %v, %v)", tt.args.name, tt.args.email, tt.args.passwd, tt.args.role, tt.args.defaultLangID))
})
}
}
5 changes: 5 additions & 0 deletions backend/pkg/app/query/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ type UserView struct {
Email string
Role RoleView
DefaultLang LangView
ListOptions UserListOptionsView
}

type UserListOptionsView struct {
ShowTranscription bool
}

func (v *UserView) sanitize(sanitizer *strictSanitizer) {
Expand Down
4 changes: 3 additions & 1 deletion backend/pkg/server/profile_handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/macyan13/webdict/backend/pkg/app/command"
Expand Down Expand Up @@ -52,8 +53,9 @@ func (s *HTTPServer) UpdateProfile() gin.HandlerFunc {
CurrentPassword: request.CurrentPassword,
NewPassword: request.NewPassword,
DefaultLangID: request.DefaultLangID,
ListOptions: user.NewListOptions(request.ListOptions.ShowTranscription),
}); err != nil {
if err == user.ErrEmailAlreadyExists {
if errors.Is(err, user.ErrEmailAlreadyExists) {
s.badRequest(c, fmt.Errorf("user with email %s already exists", request.Email))
return
}
Expand Down
3 changes: 3 additions & 0 deletions backend/pkg/server/profile_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestHTTPServer_GetProfile(t *testing.T) {
assert.Equal(t, name, profile.Name)
assert.Equal(t, email, profile.Email)
assert.Equal(t, int(user.Author), profile.Role.ID)
assert.Equal(t, false, profile.ListOptions.ShowTranscription)
}

func TestHTTPServer_GetProfile_Unauthorized(t *testing.T) {
Expand Down Expand Up @@ -51,6 +52,7 @@ func TestHTTPServer_TestHTTPServer_UpdateProfile(t *testing.T) {
Email: updatedEmail,
CurrentPassword: currentPasswd,
NewPassword: newPasswd,
ListOptions: profileListOptions{ShowTranscription: true},
}

jsonValue, _ := json.Marshal(updRequest)
Expand All @@ -63,6 +65,7 @@ func TestHTTPServer_TestHTTPServer_UpdateProfile(t *testing.T) {
profile := getProfile(t, s, updatedEmail, newPasswd)
assert.Equal(t, updatedName, profile.Name)
assert.Equal(t, updatedEmail, profile.Email)
assert.Equal(t, true, profile.ListOptions.ShowTranscription)
}

func TestHTTPServer_TestHTTPServerUnauthorized(t *testing.T) {
Expand Down
Loading

0 comments on commit 6d573e8

Please sign in to comment.