Skip to content

Commit

Permalink
fix package name, empty string in begining of method and others.
Browse files Browse the repository at this point in the history
  • Loading branch information
mchekalov committed Mar 5, 2024
1 parent 7ed702f commit 03a93e3
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 57 deletions.
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"log"

"chat-server/internal/app"
"github.com/mchekalov/chat-server/internal/app"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module chat-server
module github.com/mchekalov/chat-server

go 1.22

Expand Down
7 changes: 3 additions & 4 deletions internal/api/create.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package api

import (
desc "chat-server/pkg/chat_api_v1"
"context"

"chat-server/internal/converter"
desc "github.com/mchekalov/chat-server/pkg/chat_api_v1"

"github.com/mchekalov/chat-server/internal/converter"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// Create creates a chat room in API layer
func (i *Implementation) Create(ctx context.Context, request *desc.CreateRequest) (*desc.CreateResponse, error) {

output, err := i.chatService.Create(ctx, converter.ToCreateChatInput(request))
if err != nil {
// log.Error("failed to create chat", sl.ErrAttr(err))

return nil, status.Error(codes.Internal, "failed to create chat")
}
Expand Down
6 changes: 3 additions & 3 deletions internal/api/delete.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package api

import (
"chat-server/internal/converter"
desc "chat-server/pkg/chat_api_v1"
"context"

"github.com/mchekalov/chat-server/internal/converter"
desc "github.com/mchekalov/chat-server/pkg/chat_api_v1"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)

// Delete deletes a chat room in API layer
func (i *Implementation) Delete(ctx context.Context, request *desc.DeleteRequest) (*emptypb.Empty, error) {

err := i.chatService.Delete(ctx, converter.ToDeleteChatInput(request))
if err != nil {
return nil, status.Error(codes.Internal, "failed to delete chat")
Expand Down
5 changes: 2 additions & 3 deletions internal/api/sendmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package api
import (
"context"

"chat-server/internal/converter"
"github.com/mchekalov/chat-server/internal/converter"

desc "chat-server/pkg/chat_api_v1"
desc "github.com/mchekalov/chat-server/pkg/chat_api_v1"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -14,7 +14,6 @@ import (

// SendMessage get a new message in API layer.
func (i *Implementation) SendMessage(ctx context.Context, request *desc.SendMessageRequest) (*emptypb.Empty, error) {

err := i.chatService.SendMessage(ctx, converter.ToSendMessageInput(request))
if err != nil {
return nil, status.Error(codes.Internal, "failed to delete chat")
Expand Down
4 changes: 2 additions & 2 deletions internal/api/service.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

import (
"chat-server/internal/service"
desc "chat-server/pkg/chat_api_v1"
"github.com/mchekalov/chat-server/internal/service"
desc "github.com/mchekalov/chat-server/pkg/chat_api_v1"
)

// Implementation represents the implementation of the chat API server.
Expand Down
6 changes: 3 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"

"chat-server/internal/closer"
"chat-server/internal/config"
desc "chat-server/pkg/chat_api_v1"
"github.com/mchekalov/chat-server/internal/closer"
"github.com/mchekalov/chat-server/internal/config"
desc "github.com/mchekalov/chat-server/pkg/chat_api_v1"
)

// App represents the chat server application.
Expand Down
19 changes: 10 additions & 9 deletions internal/app/service_provider.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package app

import (
"chat-server/internal/api"
"chat-server/internal/client/db"
"chat-server/internal/client/db/pg"
"chat-server/internal/closer"
"chat-server/internal/config"
"chat-server/internal/repository"
chatrepository "chat-server/internal/repository/chat"
"chat-server/internal/service"
chatservice "chat-server/internal/service/chat"
"context"
"log"

"github.com/mchekalov/chat-server/internal/api"
"github.com/mchekalov/chat-server/internal/client/db"
"github.com/mchekalov/chat-server/internal/client/db/pg"
"github.com/mchekalov/chat-server/internal/closer"
"github.com/mchekalov/chat-server/internal/config"
"github.com/mchekalov/chat-server/internal/repository"
chatrepository "github.com/mchekalov/chat-server/internal/repository/chat"
"github.com/mchekalov/chat-server/internal/service"
chatservice "github.com/mchekalov/chat-server/internal/service/chat"
)

type serviceProvider struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/client/db/pg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/pkg/errors"

"chat-server/internal/client/db"
"github.com/mchekalov/chat-server/internal/client/db"
)

type pgClient struct {
Expand Down
5 changes: 3 additions & 2 deletions internal/client/db/pg/pg.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package pg

import (
"chat-server/internal/client/db"
"chat-server/internal/client/db/prettier"
"context"
"fmt"
"log"

"github.com/mchekalov/chat-server/internal/client/db"
"github.com/mchekalov/chat-server/internal/client/db/prettier"

"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
Expand Down
4 changes: 2 additions & 2 deletions internal/client/db/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/jackc/pgx/v5"
"github.com/pkg/errors"

"chat-server/internal/client/db"
"chat-server/internal/client/db/pg"
"github.com/mchekalov/chat-server/internal/client/db"
"github.com/mchekalov/chat-server/internal/client/db/pg"
)

type manager struct {
Expand Down
5 changes: 2 additions & 3 deletions internal/converter/chat.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package converter

import (
"chat-server/internal/model"
desc "chat-server/pkg/chat_api_v1"
"github.com/mchekalov/chat-server/internal/model"
desc "github.com/mchekalov/chat-server/pkg/chat_api_v1"

"github.com/brianvoe/gofakeit/v6"
)
Expand All @@ -27,7 +27,6 @@ type SendMessageInput struct {
// ToCreateChatInput converts a CreateRequest object from the API to a model.Chat entity.
func ToCreateChatInput(req *desc.CreateRequest) *model.Chat {
return &model.Chat{
ChatID: gofakeit.Int64(),
ChatName: req.Chatname,
}
}
Expand Down
1 change: 0 additions & 1 deletion internal/model/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package model

// Chat represents a chat room with a unique ID and name.
type Chat struct {
ChatID int64 // Unique identifier for the chat room.
ChatName string // Name of the chat room.
}

Expand Down
6 changes: 2 additions & 4 deletions internal/repository/chat/converter/chat.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package converter

import (
"chat-server/internal/model"
modelRepo "chat-server/internal/repository/chat/model"
"github.com/mchekalov/chat-server/internal/model"
modelRepo "github.com/mchekalov/chat-server/internal/repository/chat/model"
)

// FromRepoToChat converts a repository Chat model to a chat system Chat entity.
func FromRepoToChat(chat *modelRepo.Chat) *model.Chat {
return &model.Chat{
ChatID: chat.ChatID,
ChatName: chat.ChatName,
}
}

// FromChatToRepo converts a chat system Chat entity to a repository Chat model.
func FromChatToRepo(chat *model.Chat) *modelRepo.Chat {
return &modelRepo.Chat{
ChatID: chat.ChatID,
ChatName: chat.ChatName,
}
}
Expand Down
1 change: 0 additions & 1 deletion internal/repository/chat/model/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import "time"

// Chat represents a chat room with a unique ID and name in repo layer
type Chat struct {
ChatID int64
ChatName string
}

Expand Down
16 changes: 9 additions & 7 deletions internal/repository/chat/repository.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package chat

import (
"chat-server/internal/client/db"
"chat-server/internal/model"
"chat-server/internal/repository"
"chat-server/internal/repository/chat/converter"
"context"
"fmt"

"github.com/mchekalov/chat-server/internal/client/db"
"github.com/mchekalov/chat-server/internal/model"
"github.com/mchekalov/chat-server/internal/repository"
"github.com/mchekalov/chat-server/internal/repository/chat/converter"

"github.com/Masterminds/squirrel"
)
Expand All @@ -14,6 +16,7 @@ const (
tableName = "chats"
chatidColumn = "chat_id"
chatnameColumn = "chat_name"
chatIDColumn = "chat_id"
)

const (
Expand All @@ -35,14 +38,13 @@ func NewRepository(db db.Client) repository.ChatRepository {
}

func (r *repo) Create(ctx context.Context, chat *model.Chat) (int64, error) {

// Better place to convert from service model to repo model
chatRepo := converter.FromChatToRepo(chat)

builder := r.sq.Insert(tableName).
Columns(chatnameColumn).
Values(chatRepo.ChatName).
Suffix("RETURNING chat_id")
Suffix(fmt.Sprintf("RETURNING %v", chatIDColumn))

query, args, err := builder.ToSql()
if err != nil {
Expand Down Expand Up @@ -82,7 +84,7 @@ func (r *repo) Delete(ctx context.Context, id *model.ChatDelete) error {
return nil
}

func (r *repo) SendMessage(ctx context.Context, message *model.Message) error {
func (r *repo) SaveMessage(ctx context.Context, message *model.Message) error {
builder := r.sq.Insert(tableMessages).
Columns(chatidColumn, userNameColumn, messageTextColumn).
Values(message.ChatID, message.UserName, message.MessageText)
Expand Down
5 changes: 3 additions & 2 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package repository

import (
"chat-server/internal/model"
"context"

"github.com/mchekalov/chat-server/internal/model"
)

// ChatRepository defines an interface for interacting with the repository layer
// to perform operations related to chat entities.
type ChatRepository interface {
Create(ctx context.Context, chat *model.Chat) (int64, error)
Delete(ctx context.Context, id *model.ChatDelete) error
SendMessage(ctx context.Context, message *model.Message) error
SaveMessage(ctx context.Context, message *model.Message) error
}
12 changes: 5 additions & 7 deletions internal/service/chat/service.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package chat

import (
"chat-server/internal/model"
repository "chat-server/internal/repository"
"chat-server/internal/service"
"context"

"github.com/mchekalov/chat-server/internal/model"
repository "github.com/mchekalov/chat-server/internal/repository"
"github.com/mchekalov/chat-server/internal/service"
)

type serv struct {
Expand All @@ -17,7 +18,6 @@ func NewService(chatRepository repository.ChatRepository) service.ChatService {
}

func (s *serv) Create(ctx context.Context, in *model.Chat) (int64, error) {

output, err := s.chatRepository.Create(ctx, in)
if err != nil {
return 0, err
Expand All @@ -27,7 +27,6 @@ func (s *serv) Create(ctx context.Context, in *model.Chat) (int64, error) {
}

func (s *serv) Delete(ctx context.Context, in *model.ChatDelete) error {

err := s.chatRepository.Delete(ctx, in)
if err != nil {
return err
Expand All @@ -38,8 +37,7 @@ func (s *serv) Delete(ctx context.Context, in *model.ChatDelete) error {
}

func (s *serv) SendMessage(ctx context.Context, in *model.Message) error {

err := s.chatRepository.SendMessage(ctx, in)
err := s.chatRepository.SaveMessage(ctx, in)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package service

import (
"chat-server/internal/model"
"context"

"github.com/mchekalov/chat-server/internal/model"
)

// ChatService defines an interface for interacting with the service layer
Expand Down

0 comments on commit 03a93e3

Please sign in to comment.