-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
196 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/hammer-code/lms-be/app/middlewares" | ||
users "github.com/hammer-code/lms-be/app/users" | ||
"github.com/hammer-code/lms-be/config" | ||
"github.com/hammer-code/lms-be/domain" | ||
pkgDB "github.com/hammer-code/lms-be/pkg/db" | ||
"github.com/hammer-code/lms-be/pkg/jwt" | ||
"gorm.io/driver/postgres" | ||
) | ||
|
||
type App struct { | ||
UserHandler domain.UserHandler | ||
Middleware domain.Middleware | ||
} | ||
|
||
func InitApp( | ||
cfg config.Config, | ||
) App { | ||
|
||
db := config.GetDatabase(postgres.Dialector{ | ||
Config: &postgres.Config{ | ||
DSN: cfg.DB_POSTGRES_DSN, | ||
}}) | ||
|
||
dbTx := pkgDB.NewDBTransaction(db) | ||
jwtInstance := jwt.NewJwt(cfg.JWT_SECRET_KEY) | ||
|
||
// repository | ||
userRepo := users.InitRepository(dbTx) | ||
|
||
// Middlewares | ||
middleware := middlewares.InitMiddleware(jwtInstance, userRepo) | ||
|
||
userHandler := users.InitHandler(dbTx, jwtInstance) | ||
|
||
return App{ | ||
UserHandler: userHandler, | ||
Middleware: middleware, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
package middlewares | ||
|
||
import ( | ||
"github.com/hammer-code/lms-be/app/users" | ||
"github.com/hammer-code/lms-be/domain" | ||
"github.com/hammer-code/lms-be/pkg/jwt" | ||
) | ||
|
||
type Middleware struct { | ||
Jwt jwt.JWT | ||
UserRepo users.UserRepository | ||
UserRepo domain.UserRepository | ||
} | ||
|
||
func InitMiddleware(jwt jwt.JWT, userRepo domain.UserRepository) domain.Middleware { | ||
return &Middleware{ | ||
Jwt: jwt, | ||
UserRepo: userRepo, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
package http | ||
|
||
import ( | ||
"github.com/hammer-code/lms-be/app/middlewares" | ||
"github.com/hammer-code/lms-be/app/users" | ||
"github.com/hammer-code/lms-be/domain" | ||
) | ||
|
||
type Handler struct { | ||
usecase users.UserUsecase | ||
Middleware middlewares.Middleware | ||
usecase domain.UserUsecase | ||
} | ||
|
||
func NewHandler(userUsecase users.UserUsecase, middleware *middlewares.Middleware) Handler { | ||
return Handler{ | ||
usecase: userUsecase, | ||
Middleware: *middleware, | ||
var ( | ||
handlr *Handler | ||
) | ||
|
||
func NewHandler(userUsecase domain.UserUsecase) domain.UserHandler { | ||
if handlr == nil { | ||
handlr = &Handler{ | ||
usecase: userUsecase, | ||
} | ||
} | ||
|
||
return *handlr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
package usecase | ||
|
||
import ( | ||
"github.com/hammer-code/lms-be/app/users" | ||
"github.com/hammer-code/lms-be/domain" | ||
"github.com/hammer-code/lms-be/pkg/db" | ||
"github.com/hammer-code/lms-be/pkg/jwt" | ||
) | ||
|
||
type usecase struct { | ||
userRepo users.UserRepository | ||
userRepo domain.UserRepository | ||
dbTX db.DatabaseTransaction | ||
jwt jwt.JWT | ||
jwt jwt.JWT | ||
} | ||
|
||
func NewUsecase( userRepo users.UserRepository, dbTX db.DatabaseTransaction, jwt jwt.JWT) users.UserUsecase { | ||
return &usecase{ | ||
userRepo: userRepo, | ||
dbTX: dbTX, | ||
jwt: jwt, | ||
var ( | ||
usec *usecase | ||
) | ||
|
||
func NewUsecase(userRepo domain.UserRepository, dbTX db.DatabaseTransaction, jwt jwt.JWT) domain.UserUsecase { | ||
if usec == nil { | ||
usec = &usecase{ | ||
userRepo: userRepo, | ||
dbTX: dbTX, | ||
jwt: jwt, | ||
} | ||
} | ||
return usec | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,23 @@ | ||
package users | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
users_handler "github.com/hammer-code/lms-be/app/users/delivery/http" | ||
users_repo "github.com/hammer-code/lms-be/app/users/repository" | ||
users_usecase "github.com/hammer-code/lms-be/app/users/usecase" | ||
"github.com/hammer-code/lms-be/domain" | ||
"github.com/hammer-code/lms-be/pkg/db" | ||
"github.com/hammer-code/lms-be/pkg/jwt" | ||
) | ||
|
||
type ( | ||
UserRepository interface { | ||
GetUsers(ctx context.Context) (users []domain.User, err error) | ||
CreateUser(ctx context.Context, userReq domain.User) (user domain.User, err error) | ||
FindById(ctx context.Context, id int8) (user domain.User, err error) | ||
FindByEmail(ctx context.Context, email string) (user domain.User, err error) | ||
UpdateProfileUser(ctx context.Context, userReq domain.UserUpdateProfile, id int) error | ||
DeleteUser(ctx context.Context, id int8) error | ||
LogoutUser(ctx context.Context, token string, expiredAt time.Time) error | ||
ExpiredToken(ctx context.Context, token string) error | ||
GetUsersGenericConditions(ctx context.Context, filter domain.GetUserBy) (users []domain.User, err error) | ||
} | ||
UserUsecase interface { | ||
GetUsers(ctx context.Context) (users []domain.User, err error) | ||
GetUserById(ctx context.Context, id int8) (users domain.User, err error) | ||
Register(ctx context.Context, userReq domain.User) (user domain.User, err error) | ||
Login(ctx context.Context, userReq domain.Login) (user domain.User, token string, err error) | ||
UpdateProfileUser(ctx context.Context, userReq domain.UserUpdateProfile, id int) error | ||
DeleteUser(ctx context.Context, id int8) error | ||
Logout(ctx context.Context, token string) error | ||
} | ||
) | ||
// alias | ||
func InitRepository(db db.DatabaseTransaction) domain.UserRepository { | ||
return users_repo.NewRepository(db) | ||
} | ||
|
||
func InitUsecase(dbTX db.DatabaseTransaction, jwt jwt.JWT) domain.UserUsecase { | ||
return users_usecase.NewUsecase(InitRepository(dbTX), dbTX, jwt) | ||
} | ||
|
||
func InitHandler(dbTX db.DatabaseTransaction, jwt jwt.JWT) domain.UserHandler { | ||
return users_handler.NewHandler(InitUsecase(dbTX, jwt)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package domain | ||
|
||
import ( | ||
"time" | ||
|
||
"gopkg.in/guregu/null.v4" | ||
) | ||
|
||
type Event struct { | ||
ID int `json:"id"` | ||
Title string `json:"id"` | ||
Description string `json:"description"` | ||
Author string `json:"author"` | ||
ImageEvent string `json:"image_event"` | ||
DateEvent null.Time `json:"date_event"` | ||
Type string `json:"type"` | ||
Location string `json:"location"` | ||
Duration string `json:"duration"` | ||
Capacity int `json:"capacity"` | ||
RegistrationLink string `json:"registration_link"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt null.Time `json:"updated_at"` | ||
} | ||
|
||
func (Event) TableName() string { | ||
return "events" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package domain | ||
|
||
import "net/http" | ||
|
||
type Middleware interface { | ||
AuthMiddleware(next http.Handler) http.Handler | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.