Skip to content

Commit

Permalink
test repository mock removed and replaced with test-db method of test…
Browse files Browse the repository at this point in the history
…ing (#50)
  • Loading branch information
tahadostifam authored Mar 17, 2024
1 parent 976e6e5 commit 5bb6fb9
Show file tree
Hide file tree
Showing 23 changed files with 265 additions and 523 deletions.
24 changes: 19 additions & 5 deletions app/controller/user_controller.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package controller

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
dto "github.com/kavkaco/Kavka-Core/app/dto"
"github.com/kavkaco/Kavka-Core/app/presenters"
"github.com/kavkaco/Kavka-Core/internal/domain/chat"
"github.com/kavkaco/Kavka-Core/internal/domain/user"
"github.com/kavkaco/Kavka-Core/pkg/session"
"github.com/kavkaco/Kavka-Core/utils/bearer"
)

type UserController struct {
userService user.UserService
userService user.Service
chatService chat.Service
}

func NewUserController(userService user.UserService) *UserController {
return &UserController{userService}
func NewUserController(userService user.Service, chatService chat.Service) *UserController {
return &UserController{userService, chatService}
}

func (ctrl *UserController) HandleLogin(ctx *gin.Context) {
Expand Down Expand Up @@ -82,12 +85,23 @@ func (ctrl *UserController) HandleAuthenticate(ctx *gin.Context) {
accessToken, bearerOk := bearer.AccessToken(ctx)

if bearerOk {
// get the user info
userInfo, err := ctrl.userService.Authenticate(accessToken)
if err != nil {
presenters.ResponseError(ctx, err)
presenters.AccessDenied(ctx)
return
}

presenters.ResponseUserInfo(ctx, userInfo)
// gathering user chats
userChats, err := ctrl.chatService.GetUserChats(userInfo.StaticID)
if err != nil {
presenters.ResponseInternalServerError(ctx)
return
}

fmt.Println(userChats)

// FIXME - fix the presenters
// presenters.ResponseUserInfo(ctx, userInfo, userChats)
}
}
27 changes: 22 additions & 5 deletions app/presenters/chat_presenters.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@ type ChatDto struct {
Chat *chat.Chat `json:"chat"`
}

func ChatAsJSON(_ string, obj *chat.Chat) interface{} {
if obj.ChatType == chat.TypeDirect {
obj.ChatDetail = nil
}
// FIXME

Check failure on line 12 in app/presenters/chat_presenters.go

View workflow job for this annotation

GitHub Actions / linting

Comment should end in a period (godot)
func ChatAsJSON(obj chat.Chat) (interface{}, error) {
// if obj.ChatType == chat.TypeDirect {
// obj.ChatDetail = nil
// } else {
// // Determine the specific ChatDetail type based on chatType
// var chatDetail interface{}
// var convertErr error

return obj
// switch obj.ChatType {
// case chat.TypeChannel:
// chatDetail, convertErr = utils.TypeConverter[chat.ChannelChatDetail](obj.ChatDetail)
// case chat.TypeGroup:
// chatDetail, convertErr = utils.TypeConverter[chat.GroupChatDetail](obj.ChatDetail)
// }

// if convertErr != nil {
// return nil, convertErr
// }
// obj.ChatDetail = chatDetail
// }

return obj, nil
}
29 changes: 22 additions & 7 deletions app/presenters/user_presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,40 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kavkaco/Kavka-Core/internal/domain/chat"
"github.com/kavkaco/Kavka-Core/internal/domain/user"
"github.com/kavkaco/Kavka-Core/pkg/session"
)

// the data and information that would be send to user after authentication;
// the messages of chats must not be sent!

type UserInfoDto struct {
Message string `json:"message"`
Code int `json:"code"`
UserInfo *user.User `json:"user"`
Message string `json:"message"`
Code int `json:"code"`
UserInfo *user.User `json:"user"`
UserChats []chat.Chat `json:"chats"`
}

func AccessDenied(ctx *gin.Context) {
code := http.StatusForbidden

ctx.JSON(code, SimpleMessageDto{
Code: code,
Message: "Forbidden",
})
}

func SendTokensHeader(ctx *gin.Context, tokens session.LoginTokens) {
ctx.Header("refresh", tokens.RefreshToken)
ctx.Header("authorization", fmt.Sprintf("Bearer %s", tokens.AccessToken))
}

func ResponseUserInfo(ctx *gin.Context, userInfo *user.User) {
func ResponseUserInfo(ctx *gin.Context, userInfo *user.User, userChats []chat.Chat) {
ctx.JSON(http.StatusOK, UserInfoDto{
Message: "Success",
Code: 200,
UserInfo: userInfo,
Message: "Success",
Code: 200,
UserInfo: userInfo,
UserChats: userChats,
})
}
14 changes: 8 additions & 6 deletions app/router/user_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ package router
import (
"github.com/gin-gonic/gin"
"github.com/kavkaco/Kavka-Core/app/controller"
"github.com/kavkaco/Kavka-Core/internal/domain/chat"
"github.com/kavkaco/Kavka-Core/internal/domain/user"
)

type UserRouter struct {
service user.UserService
ctrl *controller.UserController
router *gin.RouterGroup
userService user.Service
chatService chat.Service
ctrl *controller.UserController
router *gin.RouterGroup
}

func NewUserRouter(router *gin.RouterGroup, service user.UserService) *UserRouter {
ctrl := controller.NewUserController(service)
func NewUserRouter(router *gin.RouterGroup, userService user.Service, chatService chat.Service) *UserRouter {
ctrl := controller.NewUserController(userService, chatService)

router.POST("/login", ctrl.HandleLogin)
router.POST("/verify_otp", ctrl.HandleVerifyOTP)
router.POST("/refresh_token", ctrl.HandleRefreshToken)
router.POST("/authenticate", ctrl.HandleAuthenticate)

return &UserRouter{service, ctrl, router}
return &UserRouter{userService, chatService, ctrl, router}
}
26 changes: 15 additions & 11 deletions app/socket/chats_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package socket
import (
"log"

"github.com/kavkaco/Kavka-Core/app/presenters"
"go.mongodb.org/mongo-driver/bson/primitive"
)

Expand Down Expand Up @@ -32,12 +31,13 @@ func CreateDirect(event string, args MessageHandlerArgs) bool {
return false
}

chat, err := args.socketService.chatService.CreateDirect(args.staticID, staticID.(primitive.ObjectID))
_, err := args.socketService.chatService.CreateDirect(args.staticID, staticID.(primitive.ObjectID))
if err != nil {
return false
}

err = args.conn.WriteJSON(presenters.ChatAsJSON(event, chat))
// FIXME
// err = args.conn.WriteJSON(presenters.ChatAsJSON(event, chat))

return err == nil
}
Expand All @@ -50,13 +50,14 @@ func GetChat(event string, args MessageHandlerArgs) bool {
return false
}

chat, err := args.socketService.chatService.GetChat(staticID.(primitive.ObjectID))
_, err := args.socketService.chatService.GetChat(staticID.(primitive.ObjectID))
if err != nil {
log.Println("find chat error in socket:", err)
return false
}

err = args.conn.WriteJSON(presenters.ChatAsJSON(event, chat))
// FIXME
// err = args.conn.WriteJSON(presenters.ChatAsJSON(event, chat))

return err == nil
}
Expand All @@ -67,14 +68,15 @@ func CreateGroup(event string, args MessageHandlerArgs) bool {
description := args.message.Data["description"]

if title != nil && username != nil && description != nil {
chat, createErr := args.socketService.chatService.CreateGroup(args.staticID, title.(string), username.(string), description.(string))
_, createErr := args.socketService.chatService.CreateGroup(args.staticID, title.(string), username.(string), description.(string))
if createErr != nil {

Check failure on line 72 in app/socket/chats_handler.go

View workflow job for this annotation

GitHub Actions / linting

S1008: should use 'return createErr == nil' instead of 'if createErr != nil { return false }; return true' (gosimple)
return false
}

err := args.conn.WriteJSON(presenters.ChatAsJSON(event, chat))
// FIXME
// err := args.conn.WriteJSON(presenters.ChatAsJSON(event, chat))

return err == nil
return true
}

return false
Expand All @@ -86,14 +88,16 @@ func CreateChannel(event string, args MessageHandlerArgs) bool {
description := args.message.Data["description"]

if title != nil && username != nil && description != nil {
chat, createErr := args.socketService.chatService.CreateChannel(args.staticID, title.(string), username.(string), description.(string))
_, createErr := args.socketService.chatService.CreateChannel(args.staticID, title.(string), username.(string), description.(string))
if createErr != nil {

Check failure on line 92 in app/socket/chats_handler.go

View workflow job for this annotation

GitHub Actions / linting

S1008: should use 'return createErr == nil' instead of 'if createErr != nil { return false }; return true' (gosimple)
return false
}

err := args.conn.WriteJSON(presenters.ChatAsJSON(event, chat))
// FIXME
// err := args.conn.WriteJSON(presenters.ChatAsJSON(event, chat))

return err == nil
// return err == nil
return true
}

return false
Expand Down
4 changes: 2 additions & 2 deletions app/socket/socket_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
)

type Service struct {
userService user.UserService
userService user.Service
chatService chat.Service
msgService message.Service
}
Expand All @@ -40,7 +40,7 @@ type MessageHandlerArgs struct {

var upgrader = websocket.Upgrader{}

func NewSocketService(app *gin.Engine, userService user.UserService, chatService chat.Service, messageService message.Service) *Service {
func NewSocketService(app *gin.Engine, userService user.Service, chatService chat.Service, messageService message.Service) *Service {
socketService := &Service{userService, chatService, messageService}

app.GET("/ws", socketService.handleWebsocket)
Expand Down
17 changes: 11 additions & 6 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/kavkaco/Kavka-Core/app/router"
"github.com/kavkaco/Kavka-Core/app/socket"
"github.com/kavkaco/Kavka-Core/config"
"github.com/kavkaco/Kavka-Core/database"
chatRepository "github.com/kavkaco/Kavka-Core/internal/repository/chat"
messageRepository "github.com/kavkaco/Kavka-Core/internal/repository/message"
userRepository "github.com/kavkaco/Kavka-Core/internal/repository/user"
"github.com/kavkaco/Kavka-Core/internal/service"
"github.com/kavkaco/Kavka-Core/pkg/session"
Expand Down Expand Up @@ -45,16 +48,18 @@ func main() {

userRepo := userRepository.NewUserRepository(mongoDB)
userService := service.NewUserService(userRepo, session, smsService)
router.NewUserRouter(app.Group("/users"), userService)

//chatRepo := chatRepository.NewRepository(mongoDB)
//chatService := service.NewChatService(chatRepo, userRepo)
chatRepo := chatRepository.NewRepository(mongoDB)
chatService := service.NewChatService(chatRepo, userRepo)

//messageRepo := messageRepository.NewMessageRepository(mongoDB)
//messageRepository := service.NewMessageService(messageRepo, chatRepo)
messageRepo := messageRepository.NewMessageRepository(mongoDB)
messageRepository := service.NewMessageService(messageRepo, chatRepo)

// Init routes
router.NewUserRouter(app.Group("/users"), userService, chatService)

// Init Socket Server
//socket.NewSocketService(app, userService, chatService, messageRepository)
socket.NewSocketService(app, userService, chatService, messageRepository)

// Everything almost done!
err := app.Run(configs.App.HTTP.Address)
Expand Down
6 changes: 6 additions & 0 deletions database/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func GetMongoDBInstance(mongoConfigs config.Mongo) (*mongo.Database, error) {
return nil, err
}

// Send a ping to confirm a successful connection
var result bson.M
if err := client.Database("test").RunCommand(context.TODO(), bson.D{{Key: "ping", Value: 1}}).Decode(&result); err != nil {
return nil, err
}

mongoInstance = client.Database(mongoConfigs.DBName)

collectionsConfigurations(mongoInstance)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/go-playground/validator/v10 v10.15.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/graph-gophers/graphql-go v1.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand Down
Loading

0 comments on commit 5bb6fb9

Please sign in to comment.