Skip to content

Commit

Permalink
refactor (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
tahadostifam authored Sep 21, 2024
1 parent e8b79c5 commit 78aad09
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 40 deletions.
2 changes: 0 additions & 2 deletions config/config.development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ minio:
access_key: uD3h2W3CONpYe8Tb4nbp
secret_key: fKF98iAk1eDSpdtdJWcJHgivjpF4Su4HCdgkpYvQ

sms:

logger:
file_name: kavka.logs
level: debug
Expand Down
16 changes: 8 additions & 8 deletions internal/service/auth/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type DetailedValidation struct {
}

func (a *AuthService) Register(ctx context.Context, name string, lastName string, username string, email string, password string, verifyEmailRedirectUrl string) (verifyEmailToken string, varror *vali.Varror) {
varrors := a.validator.Validate(RegisterValidation{name, lastName, username, email, password})
varrors := a.validator.Validate(registerValidation{name, lastName, username, email, password})
if len(varrors) > 0 {
return "", &vali.Varror{ValidationErrors: varrors}
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (a *AuthService) Register(ctx context.Context, name string, lastName string
}

func (a *AuthService) Authenticate(ctx context.Context, accessToken string) (*model.User, *vali.Varror) {
varrors := a.validator.Validate(AuthenticateValidation{accessToken})
varrors := a.validator.Validate(authenticateValidation{accessToken})
if len(varrors) > 0 {
return nil, &vali.Varror{ValidationErrors: varrors}
}
Expand All @@ -124,7 +124,7 @@ func (a *AuthService) Authenticate(ctx context.Context, accessToken string) (*mo
}

func (a *AuthService) VerifyEmail(ctx context.Context, verifyEmailToken string) *vali.Varror {
varrors := a.validator.Validate(VerifyEmailValidation{verifyEmailToken})
varrors := a.validator.Validate(verifyEmailValidation{verifyEmailToken})
if len(varrors) > 0 {
return &vali.Varror{ValidationErrors: varrors}
}
Expand All @@ -148,7 +148,7 @@ func (a *AuthService) VerifyEmail(ctx context.Context, verifyEmailToken string)
}

func (a *AuthService) Login(ctx context.Context, email string, password string) (_ *model.User, act string, rft string, varror *vali.Varror) {
varrors := a.validator.Validate(LoginValidation{email, password})
varrors := a.validator.Validate(loginValidation{email, password})
if len(varrors) > 0 {
return nil, "", "", &vali.Varror{ValidationErrors: varrors}
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func (a *AuthService) Login(ctx context.Context, email string, password string)
}

func (a *AuthService) ChangePassword(ctx context.Context, userID model.UserID, oldPassword string, newPassword string) *vali.Varror {
varrors := a.validator.Validate(ChangePasswordValidation{oldPassword, newPassword})
varrors := a.validator.Validate(changePasswordValidation{oldPassword, newPassword})
if len(varrors) > 0 {
return &vali.Varror{ValidationErrors: varrors}
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func (a *AuthService) ChangePassword(ctx context.Context, userID model.UserID, o
}

func (a *AuthService) RefreshToken(ctx context.Context, userID model.UserID, refreshToken string) (string, *vali.Varror) {
varrors := a.validator.Validate(RefreshTokenValidation{userID, refreshToken})
varrors := a.validator.Validate(refreshTokenValidation{userID, refreshToken})
if len(varrors) > 0 {
return "", &vali.Varror{ValidationErrors: varrors}
}
Expand All @@ -290,7 +290,7 @@ func (a *AuthService) RefreshToken(ctx context.Context, userID model.UserID, ref
}

func (a *AuthService) SendResetPassword(ctx context.Context, email string, resetPasswordRedirectUrl string) (token string, timeout time.Duration, varror *vali.Varror) {
varrors := a.validator.Validate(SendResetPasswordValidation{email})
varrors := a.validator.Validate(sendResetPasswordValidation{email})
if len(varrors) > 0 {
return "", 0, &vali.Varror{ValidationErrors: varrors}
}
Expand Down Expand Up @@ -330,7 +330,7 @@ func (a *AuthService) SendResetPassword(ctx context.Context, email string, reset
}

func (a *AuthService) SubmitResetPassword(ctx context.Context, token string, newPassword string) *vali.Varror {
varrors := a.validator.Validate(SubmitResetPasswordValidation{token, newPassword})
varrors := a.validator.Validate(submitResetPasswordValidation{token, newPassword})
if len(varrors) > 0 {
return &vali.Varror{ValidationErrors: varrors}
}
Expand Down
16 changes: 8 additions & 8 deletions internal/service/auth/auth_vali.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package auth

type RegisterValidation struct {
type registerValidation struct {
Name string `validate:"required,min=1,max=40"`
LastName string `validate:"required,min=1,max=40"`
Username string `validate:"required,min=3,max=25"`
Email string `validate:"required,email"` // Email format validation
Password string `validate:"required,min=8"`
}

type AuthenticateValidation struct {
type authenticateValidation struct {
AccessToken string `validate:"required"`
}

type VerifyEmailValidation struct {
type verifyEmailValidation struct {
VerifyEmailToken string `validate:"required"`
}

type LoginValidation struct {
type loginValidation struct {
Email string `validate:"required"`
Password string `validate:"required"`
}

type ChangePasswordValidation struct {
type changePasswordValidation struct {
OldPassword string `validate:"required"`
NewPassword string `validate:"required,min=8"`
}

type RefreshTokenValidation struct {
type refreshTokenValidation struct {
UserID string `validate:"required"`
RefreshToken string `validate:"required"`
}

type SendResetPasswordValidation struct {
type sendResetPasswordValidation struct {
Email string `validate:"required,email"`
}

type SubmitResetPasswordValidation struct {
type submitResetPasswordValidation struct {
ResetPasswordToken string `validate:"required"`
NewPassword string `validate:"required,min=8"`
}
10 changes: 5 additions & 5 deletions internal/service/chat/chat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewChatService(logger *log.SubLogger, chatRepo repository.ChatRepository, u

// find single chat with chat id
func (s *ChatService) GetChat(ctx context.Context, userID model.UserID, chatID model.ChatID) (*model.Chat, *vali.Varror) {
varrors := s.validator.Validate(GetChatValidation{chatID})
varrors := s.validator.Validate(getChatValidation{chatID})
if len(varrors) > 0 {
return nil, &vali.Varror{ValidationErrors: varrors}
}
Expand All @@ -53,7 +53,7 @@ func (s *ChatService) GetChat(ctx context.Context, userID model.UserID, chatID m

// get the chats that belongs to user
func (s *ChatService) GetUserChats(ctx context.Context, userID model.UserID) ([]model.ChatDTO, *vali.Varror) {
varrors := s.validator.Validate(GetUserChatsValidation{userID})
varrors := s.validator.Validate(getUserChatsValidation{userID})
if len(varrors) > 0 {
return nil, &vali.Varror{ValidationErrors: varrors}
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func (s *ChatService) GetDirectChat(ctx context.Context, userID, recipientUserID
}

func (s *ChatService) CreateDirect(ctx context.Context, userID model.UserID, recipientUserID model.UserID) (*model.ChatDTO, *vali.Varror) {
varrors := s.validator.Validate(CreateDirectValidation{userID, recipientUserID})
varrors := s.validator.Validate(createDirectValidation{userID, recipientUserID})
if len(varrors) > 0 {
return nil, &vali.Varror{ValidationErrors: varrors}
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func (s *ChatService) CreateDirect(ctx context.Context, userID model.UserID, rec
}

func (s *ChatService) CreateGroup(ctx context.Context, userID model.UserID, title string, username string, description string) (*model.ChatDTO, *vali.Varror) {
varrors := s.validator.Validate(CreateGroupValidation{userID, title, username, description})
varrors := s.validator.Validate(createGroupValidation{userID, title, username, description})
if len(varrors) > 0 {
return nil, &vali.Varror{ValidationErrors: varrors}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func (s *ChatService) CreateGroup(ctx context.Context, userID model.UserID, titl
}

func (s *ChatService) CreateChannel(ctx context.Context, userID model.UserID, title string, username string, description string) (*model.ChatDTO, *vali.Varror) {
varrors := s.validator.Validate(CreateChannelValidation{userID, title, username, description})
varrors := s.validator.Validate(createChannelValidation{userID, title, username, description})
if len(varrors) > 0 {
return nil, &vali.Varror{ValidationErrors: varrors}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/service/chat/chat_vali.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ package chat

import "github.com/kavkaco/Kavka-Core/internal/model"

type GetChatValidation struct {
type getChatValidation struct {
ChatID model.ChatID `validate:"required"`
}

type GetUserChatsValidation struct {
type getUserChatsValidation struct {
UserID model.UserID `validate:"required"`
}

type CreateDirectValidation struct {
type createDirectValidation struct {
UserID model.UserID `validate:"required"`
RecipientUserID model.UserID `validate:"required"`
}

type CreateChannelValidation struct {
type createChannelValidation struct {
UserID model.UserID `validate:"required"`
Title string `validate:"required,min=1"`
Username string `validate:"required,min=3"`
Description string
}
type CreateGroupValidation struct {
type createGroupValidation struct {
UserID model.UserID `validate:"required"`
Title string `validate:"required,min=1"`
Username string `validate:"required,min=3"`
Expand Down
4 changes: 2 additions & 2 deletions internal/service/message/message_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *MessageService) FetchMessages(ctx context.Context, chatID model.ChatID)
}

func (s *MessageService) SendTextMessage(ctx context.Context, chatID model.ChatID, userID model.UserID, messageContent string) (*model.MessageGetter, *vali.Varror) {
varrors := s.validator.Validate(InsertTextMessageValidation{chatID, userID, messageContent})
varrors := s.validator.Validate(insertTextMessageValidation{chatID, userID, messageContent})
if len(varrors) > 0 {
return nil, &vali.Varror{ValidationErrors: varrors}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func (s *MessageService) SendTextMessage(ctx context.Context, chatID model.ChatI
}

func (s *MessageService) DeleteMessage(ctx context.Context, chatID model.ChatID, userID model.UserID, messageID model.MessageID) *vali.Varror {
varrors := s.validator.Validate(DeleteMessageValidation{chatID, userID, messageID})
varrors := s.validator.Validate(deleteMessageValidation{chatID, userID, messageID})
if len(varrors) > 0 {
return &vali.Varror{ValidationErrors: varrors}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/service/message/message_vali.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package message

import "github.com/kavkaco/Kavka-Core/internal/model"

type InsertTextMessageValidation struct {
type insertTextMessageValidation struct {
ChatID model.ChatID `validate:"required"`
UserID model.UserID `validate:"required"`
MessageContent string `validate:"required"`
}

type DeleteMessageValidation struct {
type deleteMessageValidation struct {
ChatID model.ChatID `validate:"required"`
UserID model.UserID `validate:"required"`
MessageID model.MessageID `validate:"required"`
Expand Down
2 changes: 1 addition & 1 deletion internal/service/search/search_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewSearchService(logger *log.SubLogger, searchRepository repository.SearchR
}

func (s *SearchService) Search(ctx context.Context, input string) (*model.SearchResultDTO, *vali.Varror) {
varrors := s.validator.Validate(SearchValidation{input})
varrors := s.validator.Validate(searchValidation{input})
if len(varrors) > 0 {
return nil, &vali.Varror{ValidationErrors: varrors}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/search/search_vali.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package search

type SearchValidation struct {
type searchValidation struct {
Input string `validate:"required,min=3"`
}
8 changes: 4 additions & 4 deletions internal/service/search/search_vali_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ func (s *ValiTestSuite) SetupSuite() {

func (s *ValiTestSuite) TestSearchValidation() {
testCases := []struct {
Input *SearchValidation
Input *searchValidation
Valid bool
}{
{
Input: &SearchValidation{Input: ""},
Input: &searchValidation{Input: ""},
Valid: false,
},
{
Input: &SearchValidation{Input: " "},
Input: &searchValidation{Input: " "},
Valid: false,
},
{
Input: &SearchValidation{Input: "Sample"},
Input: &searchValidation{Input: "Sample"},
Valid: true,
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/user/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewUserService(userRepo repository.UserRepository) *UserService {
}

func (s *UserService) UpdateProfile(ctx context.Context, userID model.UserID, name, lastName, username, biography string) *vali.Varror {
validationErrors := s.validator.Validate(UpdateProfileValidation{name, lastName, username})
validationErrors := s.validator.Validate(updateProfileValidation{name, lastName, username})
if len(validationErrors) > 0 {
return &vali.Varror{ValidationErrors: validationErrors}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/user/user_vali.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package user

type UpdateProfileValidation struct {
type updateProfileValidation struct {
Name string `validate:"required,min=3,max=40"`
LastName string `validate:"required,min=3,max=40"`
Username string `validate:"required,min=4,max=20"`
Expand Down

0 comments on commit 78aad09

Please sign in to comment.