Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
KranjQ committed Dec 12, 2024
1 parent 50bb506 commit c24a75c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func main() {
addReaction := addreaction.NewHandler(communicationsClient, authClient, personalitiesClient, communicationsClient, imageUseCase, websocketUsecase, logger)
getMatches := getmatches.NewHandler(communicationsClient, authClient, personalitiesClient, imageUseCase, logger)
sendReport := sendreport.NewHandler(authClient, messageClient, communicationsClient, logger)
sendMessage := sendmessage.NewHandler(messageClient, websocketUsecase, authClient, communicationsClient, logger)
sendMessage := sendmessage.NewHandler(messageClient, websocketUsecase, authClient, communicationsClient, personalitiesClient, logger)
getAllChats := getallchats.NewHandler(communicationsClient, authClient, personalitiesClient, imageUseCase, messageClient, logger)
setConnection := setconnection.NewHandler(websocketUsecase, authClient, logger)
changePassword := changepassword.NewHandler(authClient, personalitiesClient, logger)
Expand Down
6 changes: 5 additions & 1 deletion internal/pkg/message/delivery/http/sendmessage/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ type Handler struct {
logger *zap.Logger
}

func NewHandler(messageClient generatedMessage.MessageClient, ws WebSocketService, sessionClient generatedAuth.AuthClient, communicationsClient generatedCommunications.CommunicationsClient, logger *zap.Logger) *Handler {
func NewHandler(messageClient generatedMessage.MessageClient,
ws WebSocketService, sessionClient generatedAuth.AuthClient,
communicationsClient generatedCommunications.CommunicationsClient,
personalitiesClient generatedPersonalities.PersonalitiesClient, logger *zap.Logger) *Handler {
return &Handler{
messageClient: messageClient,
ws: ws,
sessionClient: sessionClient,
communicationsClient: communicationsClient,
personalitiesClient: personalitiesClient,
logger: logger,
}
}
Expand Down
20 changes: 18 additions & 2 deletions internal/pkg/message/delivery/http/sendmessage/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
generatedMessage "github.com/go-park-mail-ru/2024_2_SaraFun/internal/pkg/message/delivery/grpc/gen"
messagemocks "github.com/go-park-mail-ru/2024_2_SaraFun/internal/pkg/message/delivery/grpc/gen/mocks"
websocketmocks "github.com/go-park-mail-ru/2024_2_SaraFun/internal/pkg/message/delivery/http/sendmessage/mocks"
generatedPersonalities "github.com/go-park-mail-ru/2024_2_SaraFun/internal/pkg/personalities/delivery/grpc/gen"
personalitiesmocks "github.com/go-park-mail-ru/2024_2_SaraFun/internal/pkg/personalities/delivery/grpc/gen/mocks"
"github.com/go-park-mail-ru/2024_2_SaraFun/internal/utils/consts"
"github.com/golang/mock/gomock"
"go.uber.org/zap"
Expand All @@ -29,8 +31,9 @@ func TestHandler(t *testing.T) {
messageClient := messagemocks.NewMockMessageClient(mockCtrl)
authClient := authmocks.NewMockAuthClient(mockCtrl)
communicationsClient := communicationsmocks.NewMockCommunicationsClient(mockCtrl)
personalitiesClient := personalitiesmocks.NewMockPersonalitiesClient(mockCtrl)
wsClient := websocketmocks.NewMockWebSocketService(mockCtrl)
handler := NewHandler(messageClient, wsClient, authClient, communicationsClient, logger)
handler := NewHandler(messageClient, wsClient, authClient, communicationsClient, personalitiesClient, logger)

tests := []struct {
name string
Expand All @@ -45,6 +48,9 @@ func TestHandler(t *testing.T) {
authReturn int
authError error
authTimes int
usernameReturn string
usernameError error
usernameTimes int
messageIDReturn int
messageError error
messageTimes int
Expand Down Expand Up @@ -73,6 +79,9 @@ func TestHandler(t *testing.T) {
Time: time.DateTime,
},
status: "",
usernameReturn: "sparkit",
usernameError: nil,
usernameTimes: 1,
messageIDReturn: 1,
messageError: nil,
messageTimes: 1,
Expand Down Expand Up @@ -101,6 +110,9 @@ func TestHandler(t *testing.T) {
Time: time.DateTime,
},
status: "block exists",
usernameReturn: "sparkit",
usernameError: nil,
usernameTimes: 1,
messageIDReturn: 1,
messageError: nil,
messageTimes: 1,
Expand Down Expand Up @@ -133,7 +145,11 @@ func TestHandler(t *testing.T) {
addMessageResponse := &generatedMessage.AddMessageResponse{MessageID: int32(tt.messageIDReturn)}
messageClient.EXPECT().AddMessage(ctx, addMessageReq).Return(addMessageResponse, tt.messageError).Times(tt.messageTimes)

wsClient.EXPECT().WriteMessage(ctx, tt.message.Author, tt.message.Receiver, tt.message.Body).Return(tt.wsError).Times(tt.wsTimes)
getUsernameReq := &generatedPersonalities.GetUsernameByUserIDRequest{UserID: int32(tt.authReturn)}
getUsernameResponse := &generatedPersonalities.GetUsernameByUserIDResponse{Username: tt.usernameReturn}
personalitiesClient.EXPECT().GetUsernameByUserID(ctx, getUsernameReq).Return(getUsernameResponse, tt.usernameError).Times(tt.usernameTimes)

wsClient.EXPECT().WriteMessage(ctx, tt.message.Author, tt.message.Receiver, tt.message.Body, tt.usernameReturn).Return(tt.wsError).Times(tt.wsTimes)

req := httptest.NewRequest(tt.method, tt.path, bytes.NewBuffer(tt.body))
req = req.WithContext(ctx)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/pkg/websockets/usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Repository interface {
AddConnection(ctx context.Context, conn *ws.Conn, userId int) error
DeleteConnection(ctx context.Context, userId int) error
WriteMessage(ctx context.Context, authorID int, receiverID int, message string) error
WriteMessage(ctx context.Context, authorID int, receiverID int, message string, username string) error
SendNotification(ctx context.Context, receiverID int, receiverImageLink string, authorUsername string) error
}

Expand Down Expand Up @@ -46,9 +46,9 @@ func (u *UseCase) DeleteConnection(ctx context.Context, userId int) error {
return nil
}

func (u *UseCase) WriteMessage(ctx context.Context, authorID int, receiverID int, message string) error {
func (u *UseCase) WriteMessage(ctx context.Context, authorID int, receiverID int, message string, username string) error {
u.logger.Info("Usecase WriteMessage start", zap.Int("user_id", receiverID))
err := u.repo.WriteMessage(ctx, authorID, receiverID, message)
err := u.repo.WriteMessage(ctx, authorID, receiverID, message, username)
if err != nil {
u.logger.Error("repo WriteMessage call in Usecase failed", zap.Error(err))
return fmt.Errorf("repo WriteMessage call in Usecase failed: %w", err)
Expand Down

0 comments on commit c24a75c

Please sign in to comment.